如何做每个文件在目录中的批处理脚本 [英] How to do something to each file in a directory with a batch script

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

问题描述

你如何遍历每个文件目录中的一个.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

批处理文件用法:

Batch file usage:

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

更新:如果该目录包含了名字空间的文件,您需要更改分隔符 FOR / 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 "delims=|" %%f in ('dir /b c:\') do echo %%f

更新2 :(快一年,原来的答复:-)后半)如果目录名本身在名称中有空格,你可以使用有usebackq 在'为'选项:

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天全站免登陆