Some other resources:
- https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/windows-commands
- http://steve-jansen.github.io/guides/windows-batch-scripting/part-10-advanced-tricks.html
- https://www.tutorialspoint.com/batch_script/batch_script_quick_guide.htm
- https://ss64.com/nt/
SET foo="bar"
if "%foo%"=="bar" (goto isbar) else (goto isnotbar)
echo.
:isbar
echo foo is bar
goto endbar
:isnotbar
echo foo is not bar
:endbar
:choicestart
SET /p choice="Enter 1: "
if NOT %choice%==1 (goto choicestart)
- Current working directory:
%cd%
- Path to the batch file:
%~dp0
- Command line parameters of the batch file: %1 - %9
%cd%
and %~dp0
start the same when not running the script as administrator. If running the script as administrator, %cd%
gives the System32 diretory while %~dp0
always gives the location of the batch file.
pushd "%~dp0"
Use pushd
because it accepts network paths (UNC) while cd
does not.
This command is necessary in general because when .bat files are run as admin, the path is set to C:\windows\system32\
.
%0
accesses the first parameter which is the full path of the bath file, ~
is for parameter extensions, d
is the drive letter only, and p
is the path only, so combining them into %~dp0
gives the drive and path but not the file/script name.
See: https://ss64.com/nt/syntax-args.html
Unzip: tar -xf "myfile.zip"
Zip: tar -caf "myfile.zip" a.txt b.txt
Create a zip of all .txt files: tar -caf "myfile.zip" *.txt
Unzip: powershell Expand-Archive myfile.zip output/ -Force
Zip: powershell Compress-Archive -path mypath -destinationpath -mydestpath
Win 10 1903 or later: curl "https://.../file.zip" -o myfile.zip
Download (can be run in a cmd): powershell Invoke-WebRequest -Uri "https://file-examples-com.github.io/uploads/2017/02/file_example_JSON_1kb.json" -OutFile c:\test.json
robocopy .\currentFolder\ .\ /E /MOVE /NFL /NDL /NJH /NJS /NC /NS /NP
start "" "%~f0"
for %%X in (wt.exe) do (set FOUNDWT=%%~$PATH:X)
if defined FOUNDWT (goto found) else (goto notfound)
start https://www.google.com
start "" /wait /b ping www.google.com
certutil -hashfile myfile.zip MD5
MD4, MD5, SHA1, SHA256, and SHA512 are allowed
To get only the hash returned: certutil -hashfile myfile.zip MD5 | findstr /V ":"
as the only line without a colon is the hash
If certutil is not available: powershell Get-FileHash ./myfile.zip -Algorithm MD5
MD5, SHA1, SHA256, SHA384, and SHA512 are allowed
ren *.rar *.cbr
takeown /f %1 /r /d y
icacls %1 /grant administrators:F /t
for /R %%X in (*.mp3) do (echo %%X >> playlist.m3u)
@echo off
The @ makes the current command also silent; echo off
turns off all subsequent echoes, but not itself
echo. >> out.txt
Note the period after echo - echo.
append to the end or create if it doesn't exist: type "filename.txt" >> out.txt
overwrite or create if it doesn't exist: type "filename.txt" > out.txt
:: this is a comment
Alternatively, rem this is a comment
Vista or later: timeout /t 9 /nobreak
XP: ping 127.0.0.1 -n 9 > NUL
ping 127.0.0.1 -n 1 -w 500 > NUL
This is the lowest amount of time you can wait without a third party application
pause
[command] >NUL
- pipe stdout to null
[command] 2>&1
- pipe stderr to stdout