Skip to content

Instantly share code, notes, and snippets.

@bantya
Last active October 31, 2022 17:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bantya/2993ff4f377f3bd563cb6d9e6f4a3459 to your computer and use it in GitHub Desktop.
Save bantya/2993ff4f377f3bd563cb6d9e6f4a3459 to your computer and use it in GitHub Desktop.
BAT: My custom bat commands to make life easier.
@echo off
:: Author: Rahul Thakare
:: Email: rahulthakare1504@gmail.com
:: Github: www.github.com/bantya
:: Purpose: Easy change directory command for cmd
:: Created: 23062017
set Dir=%1
if "%Dir%"=="" (
cd
) else (
cd /d %1
)
@echo off
:: Author: Rahul Thakare
:: Email: rahulthakare1504@gmail.com
:: Github: www.github.com/bantya
:: Purpose: Easily jump to the \CustomCMDCommands\
cd /d "%~dp0"
@echo off
:: Author: Rahul Thakare
:: Email: rahulthakare1504@gmail.com
:: Github: www.github.com/bantya
:: Purpose: Copy files / folders.
:: Created: 23062017
copy %1 %2 %3
@echo off
:: Author: Rahul Thakare
:: Email: rahulthakare1504@gmail.com
:: Github: www.github.com/bantya
:: Purpose: To copy the current cmd path or the executable path (e.g. php, mysql path)
:: -a => Copy all the lines
:: -f => Copy the result w/o ant bat or cmd file path.
:: This file requires another helper file wher-sup.bat
:: updated: 23062017
if "%1"=="" (
cd | clip
echo. Copied the current path!
) else (
if "%2"=="-a" (
where %1 | clip
echo. Copied the all paths!
) else if "%2"=="-f" (
where "%1" | findstr /v .bat | findstr /v .cmd | clip
echo. Copied the first path!
) else (
wher-sup %1
echo. Copied the specified path!
)
)
@echo off
:: Author: Rahul Thakare
:: Email: rahulthakare1504@gmail.com
:: Github: www.github.com/bantya
:: Purpose: Alias for cpath command (cp === copy path)
:: Created: 23062017
cpath %1 %2
@echo off
:: Author: Rahul Thakare
:: Email: rahulthakare1504@gmail.com
:: Github: www.github.com/bantya
:: Purpose: To enable Windows create and edit the files directly from cmd using Vim
vim %1
@echo off
:: Author: Rahul Thakare
:: Email: rahulthakare1504@gmail.com
:: Github: www.github.com/bantya
:: Purpose: Start a process/app elevated from command line
:: Helpful in cases where one has to edit system ( e.g. hosts) files.
set App=%1
set File=%2
if "%App%"=="" (
goto eof
)
if "%File%"=="hosts" (
set File="c:\windows\system32\drivers\etc\hosts"
)
powershell -Command "Start-Process -FilePath %App% %File% -Verb runAs"
:: powershell.exe -Command "Start-Process cmd \"/k cd /d %cd%\" -Verb RunAs"
:: powershell -Command "Start-Process cmd -ArgumentList '/K cd %cd%' -Verb RunAs "
:eof
@echo off
:: Author: Rahul Thakare
:: Email: rahulthakare1504@gmail.com
:: Github: www.github.com/bantya
:: Purpose: Easy git clone command
:: created 24042017
:: updated 29092017 Added facility to clone a repo using uername/reponame
:: http://stackoverflow.com/questions/7005951/batch-file-find-if-substring-is-in-string-not-in-a-file
set url=%1
set location=%2
echo %url% | find /I "https://github.com/">Nul
if errorlevel 1 (
set url=https://github.com/%url%.git
)
echo %url% | find /I ".git">Nul
if errorlevel 1 (
set url=%url%.git
)
git clone %url% %location%
@echo off
:: Author: Rahul Thakare
:: Email: rahulthakare1504@gmail.com
:: Github: www.github.com/bantya
:: Purpose: Alias for cdir command (here gf === go to folder)
:: Created: 23062017
cdir %1
@echo off
:: Author: Rahul Thakare
:: Email: rahulthakare1504@gmail.com
:: Github: www.github.com/bantya
:: Purpose: List directory content in bash fashion
set dir=%1
dir /d /og /on %dir%
@echo off
:: Author: Rahul Thakare
:: Email: rahulthakare1504@gmail.com
:: Github: www.github.com/bantya
:: Purpose: Alias for ldir command (lf === list folders)
:: Created: 23062017
set dir=%1
ldir %dir%
@echo off
:: Author: Rahul Thakare
:: Email: rahulthakare1504@gmail.com
:: Github: www.github.com/bantya
:: Purpose: Move files / folders.
:: Created: 23062017
move %1 %2 %3
@echo off
:: Author: Rahul Thakare
:: Email: rahulthakare1504@gmail.com
:: Github: www.github.com/bantya
:: Purpose: To open current directory directly from cmd
set Location=%1
if "%Location%"=="" set Location=.
start %Location%
@echo off
:: Author: Rahul Thakare
:: Email: rahulthakare1504@gmail.com
:: Github: www.github.com/bantya
:: Purpose: To get the parent folder from current file / folder path (helper file for cppath.bat)
set Loc=%1
echo %~dp1 | clip
@echo off
:: Author: Rahul Thakare
:: Email: rahulthakare1504@gmail.com
:: Github: www.github.com/bantya
:: Purpose: To emulate PHP console interactive mode on Windows
:: From: https://stackoverflow.com/a/26989684/5490303
echo Interactive mode enabled
echo.
:loop
set /p php="php > " %=%
php -r "%php%"
echo.
goto loop
@echo off
:: Author: Rahul Thakare
:: Email: rahulthakare1504@gmail.com
:: Github: www.github.com/bantya
:: Purpose: Open desired file with phpstorm easily
phpstorm64 %1
@echo off
:: Author: Rahul Thakare
:: Email: rahulthakare1504@gmail.com
:: Github: www.github.com/bantya
:: Purpose: Easy remove directory command
:: Created: 24042017
:: http://stackoverflow.com/questions/3732947/batch-to-remove-character-from-a-string
set Dir=%1
if %Dir:~-1%==\ set Dir=%Dir:~0,-1%
if %Dir:~-1%==/ set Dir=%Dir:~0,-1%
rd %Dir% /s
@echo off
:: Author: Rahul Thakare
:: Email: rahulthakare1504@gmail.com
:: Github: www.github.com/bantya
:: Purpose: Restart explorer.exe to prevent restarting computer.
:: Created: 19072017
taskkill /f /im explorer.exe
start explorer.exe
exit
@echo off
:: Author: Rahul Thakare
:: Email: rahulthakare1504@gmail.com
:: Github: www.github.com/bantya
:: Purpose: Alias for rdir command (rf === remove folder)
:: Created: 23062017
rdir %1
@echo off
:: rrurl: remote repo url opener.
:: Rahul Thakare <https://github.com/bantya> 11-02-2019
:: Ref. https://www.dostips.com/DtTipsStringManipulation.php
git remote get-url origin > tmpFile
set /p repo=<tmpFile
del tmpFile
echo %repo% | find /I "git@github.com:">Nul
set repo=%repo:git@github.com:=%
set repo=%repo:https://github.com/=%
set repo=%repo:.git=%
echo Opening: https://github.com/%repo%
start https://github.com/%repo%
set "repo="
@echo off
:: Author: Rahul Thakare
:: Email: rahulthakare1504@gmail.com
:: Github: www.github.com/bantya
:: Purpose: Open the local spotlight images folder
:: Created: 04092017
start %localappdata%\Packages\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\LocalState\Assets
@echo off
:: Author: Rahul Thakare
:: Email: rahulthakare1504@gmail.com
:: Github: www.github.com/bantya
:: Purpose: To open desired file with sublime text with desired attributes
set Location=%1
set Command=%2
if "%Location%"=="" set Location=.
if "%Command%"=="" set Command=-a
if %Location:~0,1%==- (
set Command=%Location%
set Location=.
)
subl %Location% %Command%
@echo off
:: Author: Rahul Thakare
:: Email: rahulthakare1504@gmail.com
:: Github: www.github.com/bantya
:: Purpose: To unzip the .zip archive
:: Updated: 29082017
set Source=%1
set Destination=%2
set CD=%cd%
if "%Destination%"=="" (
set Filename=%~nx1
set Destination=%CD%\%Filename:.zip=%
) else (
set "Destination=%Destination:\=/%"
set Destination=%CD%\%Destination%
)
set Expr=ExtractToDirectory('%Source%', '%Destination%')
powershell.exe -nologo -noprofile -command "& { Add-Type -A 'System.IO.Compression.FileSystem'; [IO.Compression.ZipFile]::%Expr%; }"
echo.
echo.Done unzipping!
@echo off
:: Author: Rahul Thakare
:: Email: rahulthakare1504@gmail.com
:: Github: www.github.com/bantya
:: Purpose: To download file natively in cmd as Windows did not have any alternative to wget.
setLocal EnableDelayedExpansion
set Url=%1
set Url=!Url:http://=!
set Url=!Url:/=,!
set Url=!Url:%%20=?!
set Url=!Url: =?!
call :LOOP !Url!
set FileName=%2
if "%2"=="" set FileName=!FN!
echo.
echo.Downloading: %1 to \!FileName!
powershell.exe -command wget %1 -OutFile !FileName!
goto :EOF
:LOOP
if "%1"=="" goto :EOF
set FN=%1
set FN=!FN:?= !
shift
goto :LOOP
@echo off
:: Author: Rahul Thakare
:: Email: rahulthakare1504@gmail.com
:: Github: www.github.com/bantya
:: Purpose: To get the filepath from where command as a variable (helper file for cppath.bat)
:: This file requires another helper file parent.bat
where %1 > tmpFile
set /p App= < tmpFile
del tmpFile
parent %App%
@echo off
:: Author: Rahul Thakare
:: Email: rahulthakare1504@gmail.com
:: Github: www.github.com/bantya
:: Purpose: Get the Windows user name or group information.
:: Updated: 23062017
set Args=%1
if "%Args%"=="-n" (
hostname
goto eof
)
if "%Args%"=="-a" set Args="/all"
if "%Args%"=="-u" set Args="/user"
if "%Args%"=="-p" set Args="/priv"
whoami %Args%
:eof
@echo off
:: Author: Rahul Thakare
:: Email: rahulthakare1504@gmail.com
:: Github: www.github.com/bantya
:: Purpose: Zip the entire content of the desired directory
set Source=%1
set Destination=%2
if "%Source%"=="" set Source=.
if "%Destination%"=="" set Destination=Zipped.zip
echo %Destination% | find /I ".zip">Nul
if errorlevel 1 (
set Destination=%Destination%.zip
)
set Expr=CreateFromDirectory('%Source%', '%Destination%')
powershell.exe -nologo -noprofile -command "& { Add-Type -A 'System.IO.Compression.FileSystem'; [IO.Compression.ZipFile]::%Expr%; }"
echo.
echo.Done zipping!
@bantya
Copy link
Author

bantya commented Jun 17, 2017

If anybody have such useful bat scripts then please add here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment