如何使用批处理脚本对目录中的每个文件执行某些操作 [英] How to do something to each file in a directory with a batch script

查看:145
本文介绍了如何使用批处理脚本对目录中的每个文件执行某些操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在.bat或.cmd文件的目录中迭代每个文件?

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

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

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

推荐答案

命令行用法:

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

批处理文件使用:

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

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

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 "delims=|" %%f in ('dir /b c:\') do echo %%f

更新2 :(原始答案后一年半以上:-))如果目录名称本身在名称中有空格,可以使用 usebackq 中的$ c>选项:

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:

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

这篇关于如何使用批处理脚本对目录中的每个文件执行某些操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆