windows 如何使用批处理脚本对目录中的每个文件执行某些操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/180741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 05:29:53  来源:igfitidea点击:

How to do something to each file in a directory with a batch script

windowsfileloopsbatch-filecmd

提问by Brian R. Bondy

How do you iterate over each file in a directory with a .bat or .cmd file?

如何使用 .bat 或 .cmd 文件遍历目录中的每个文件?

For simplicity please provide an answer that just echoes the filename or file path.

为简单起见,请提供一个仅与文件名或文件路径相呼应的答案。

回答by Franci Penov

Command line usage:

命令行用法:

for /f %f in ('dir /b c:\') do echo %f

Batch file usage:

批处理文件使用:

for /f %%f in ('dir /b c:\') do echo %%f

Update: if the directory contains files with space in the names, you need to change the delimiter the for /fcommand is using. for example, you can use the pipe char.

更新:如果目录包含名称中带有空格的文件,则需要更改for /f命令使用的分隔符。例如,您可以使用管道字符。

for /f "delims=|" %%f in ('dir /b c:\') do echo %%f

Update 2: (quick one year and a half after the original answer :-)) If the directory name itself has a space in the name, you can use the usebackqoption on the for:

更新 2:(在原始答案后一年半很快:-)) 如果目录名称本身在名称中有空格,则可以使用以下usebackq选项for

for /f "usebackq delims=|" %%f in (`dir /b "c:\program files"`) do echo %%f

And if you need to use output redirection or command piping, use the escape char (^):

如果您需要使用输出重定向或命令管道,请使用转义字符 ( ^):

for /f "usebackq delims=|" %%f in (`dir /b "c:\program files" ^| findstr /i microsoft`) do echo %%f

回答by Paul Houx

Alternatively, use:

或者,使用:

forfiles /s /m *.png /c "cmd /c echo @path"

The forfiles command is available in Windows Vista and up.

forfiles 命令在 Windows Vista 及更高版本中可用。

回答by Gordon Bell

Easiest method:

最简单的方法:

From Command Line, use:

从命令行,使用:

for %f in (*.*) do echo %f

From a Batch File (double up the % percent signs):

从批处理文件(将 % 百分号加倍):

for %%f in (*.*) do echo %%f

From a Batch File with folder specified as 1st parameter:

从文件夹指定为第一个参数的批处理文件:

for %%f in (%1\*.*) do echo %%f

回答by mstrobl

Use

for /r path %%var in (*.*) do some_command %%var

with:

和:

  • path being the starting path.
  • %%var being some identifier.
  • *.* being a filemask OR the contents of a variable.
  • some_command being the command to execute with the path and var concatenated as parameters.
  • path 是起始路径。
  • %%var 是一些标识符。
  • *.* 是文件掩码或变量的内容。
  • some_command 是将路径和 var 作为参数连接起来执行的命令。

回答by thistleknot

Another way:

其它的办法:

for %f in (*.mp4) do call ffmpeg -i "%~f" -vcodec copy -acodec copy "%~nf.avi"

回答by Anon

I had some malware that marked all files in a directory as hidden/system/readonly. If anyone else finds themselves in this situation, cd into the directory and run for /f "delims=|" %f in ('forfiles') do attrib -s -h -r %f.

我有一些恶意软件将目录中的所有文件标记为隐藏/系统/只读。如果其他人发现自己处于这种情况,请 cd 进入目录并运行for /f "delims=|" %f in ('forfiles') do attrib -s -h -r %f.