Hunter的大杂烩 技术学习笔记

2024-12-02

window bat批处理编程技巧

Filed under: 技术话题 — hunter @ 9:38 pm
  1. for 循环中使用管道符,要加 ^ 符号,例如:
    • for /f “delims=” %%a in (‘dir /b^|findstr /l “.avi .mp4 .mkv”‘)
    • for /f “delims=” %%a in (‘certutil -hashfile “%%i” md5 ^| findstr /X “[a-z0-9]*”‘)
  2. 使用双重for来提取字符串或合并打印输出,如:
    • for /f “delims=” %%i in (‘dir/s/b *.mp3 *.m4a’) do ( for /f “delims=” %%a in (‘certutil -hashfile “%%i” md5 ^| findstr /X “[a-z0-9]*”‘) do ( echo %%a %%i ) )
  3. 如果使用for循环处理文件的内容,以双引号隔断,则for参数去掉双引号,参数间使用 ^ 符号连接,比如:
    • for /f tokens^=^1^,2^ delims^=^” %%b in (“%%L”) do (
  4. for 中 usebackq用途为指明后续 () 中参数为文件名,比如
    • for /f “usebackq” %%a in (“C:\Documents and Settings\Administrator\桌面\note.txt”) 可读取文件note.txt
    • for /f “delims=” %%a in (“C:\Documents and Settings\Administrator\桌面\note.txt”) ,则把()中当作是字符串处理
  5. if 的字符串不等于使用方法 if not str1 == str2,比如
    • if not !last_key! == %%b () …

No Comments

No comments yet.

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

Powered by WordPress