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

时间:2020-11-26 02:23:42

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.

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

6 个解决方案

#1


306  

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 /f command is using. for example, you can use the pipe char.

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

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 usebackq option on the for:

更新2:(快一年半后,原始答案:-))如果目录名称本身有一个名称空间,您可以使用usebackq选项:

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

#2


77  

Alternatively, use:

另外,使用:

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

The forfiles command is available in Windows Vista and Windows 7.

forfiles命令可以在Windows Vista和Windows 7中使用。

#3


42  

Easiest method:

最简单的方法:

From Command Line, use:

从命令行中,使用:

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

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

从批处理文件(double up %百分号):

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

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

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

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

#4


29  

Use

使用

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

with:

:

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

#5


2  

Another way:

另一种方法:

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

#6


-2  

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 ('forfiles'),做attrib -s -h -r %f。

#1


306  

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 /f command is using. for example, you can use the pipe char.

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

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 usebackq option on the for:

更新2:(快一年半后,原始答案:-))如果目录名称本身有一个名称空间,您可以使用usebackq选项:

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

#2


77  

Alternatively, use:

另外,使用:

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

The forfiles command is available in Windows Vista and Windows 7.

forfiles命令可以在Windows Vista和Windows 7中使用。

#3


42  

Easiest method:

最简单的方法:

From Command Line, use:

从命令行中,使用:

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

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

从批处理文件(double up %百分号):

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

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

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

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

#4


29  

Use

使用

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

with:

:

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

#5


2  

Another way:

另一种方法:

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

#6


-2  

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 ('forfiles'),做attrib -s -h -r %f。