资源:
- 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)
- 当前目录:
%CD%
- 当前batch文件:
%~dp0
- 当前batch文件的参数: %1 - %9
解压 (Win 10 1903 (Jan 2018) or later): tar -xf "myfile.zip"
解压 (can be run in cmd): powershell Expand-Archive myfile.zip output/ -Force
压缩(can be run in cmd): powershell Compress-Archive -path mypath -destinationpath -mydestpath
Win 10 1903 以后版本: curl "https://.../file.zip" -o myfile.zip
下载 (can be run in a cmd): powershell Invoke-WebRequest -Uri "http://host.com/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
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
ping -n 9 127.0.0.1 > NUL
pause
[command] >NUL
- pipe stdout to null
[command] 2>&1
- pipe stderr to stdout