windows 使用“for”循环迭代目录中的所有文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/138497/
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:24:24  来源:igfitidea点击:

Iterate all files in a directory using a 'for' loop

windowsbatch-filefor-loopcmd

提问by Vhaerun

How can I iterate over each file in a directory using a forloop?

如何使用for循环遍历目录中的每个文件?

And how could I tell if a certain entry is a directory or if it's just a file?

我怎么知道某个条目是目录还是只是一个文件?

回答by jop

This lists all the files (and only the files) in the current directory:

这列出了当前目录中的所有文件(并且只有文件):

for /r %i in (*) do echo %i

Also if you run that command in a batch file you need to double the % signs.

此外,如果您在批处理文件中运行该命令,则需要将 % 符号加倍。

for /r %%i in (*) do echo %%i

(thanks @agnul)

(感谢@agnul)

回答by Marco

Iterate through...

遍历...

  • ...files in current dir: for %f in (.\*) do @echo %f
  • ...subdirs in current dir: for /D %s in (.\*) do @echo %s
  • ...files in current and all subdirs: for /R %f in (.\*) do @echo %f
  • ...subdirs in current and all subdirs: for /R /D %s in (.\*) do @echo %s
  • ...当前目录中的文件: for %f in (.\*) do @echo %f
  • ...当前目录中的子目录: for /D %s in (.\*) do @echo %s
  • ...当前和所有子目录中的文件: for /R %f in (.\*) do @echo %f
  • ...当前和所有子目录中的子目录: for /R /D %s in (.\*) do @echo %s

Unfortunately I did not find any way to iterate over files and subdirs at the same time.

不幸的是,我没有找到任何方法来同时迭代文件和子目录。

Just use cygwinwith its bash for much more functionality.

只需将cygwin与其 bash 结合使用即可获得更多功能。

Apart from this: Did you notice, that the buildin help of MS Windows is a great resource for descriptions of cmd's command line syntax?

除此之外:您是否注意到,MS Windows 的内置帮助是描述 cmd 命令行语法的绝佳资源?

Also have a look here: http://technet.microsoft.com/en-us/library/bb490890.aspx

也看看这里:http: //technet.microsoft.com/en-us/library/bb490890.aspx

回答by Aaron Votre

To iterate over each file a for loop will work:

要遍历每个文件,for 循环将起作用:

for %%f in (directory\path\*) do ( something_here )

for %%f in (directory\path\*) do ( something_here )

In my case I also wanted the file content, name, etc.

就我而言,我还想要文件内容、名称等。

This lead to a few issues and I thought my use case might help. Here is a loop that reads info from each '.txt' file in a directory and allows you do do something with it (setx for instance).

这导致了一些问题,我认为我的用例可能会有所帮助。这是一个循环,它从目录中的每个“.txt”文件中读取信息,并允许您使用它做一些事情(例如 setx)。

@ECHO OFF
setlocal enabledelayedexpansion
for %%f in (directory\path\*.txt) do (
  set /p val=<%%f
  echo "fullname: %%f"
  echo "name: %%~nf"
  echo "contents: !val!"
)

*Limitation: val<=%%f will only get the first line of the file.

*限制:val<=%%f 只会得到文件的第一行。

回答by aphoria

There is a subtle difference between running FORfrom the command line and from a batch file. In a batch file, you need to put two %characters in front of each variable reference.

FOR从命令行运行和从批处理文件运行之间存在细微差别。在批处理文件中,您需要%在每个变量引用前放置两个字符。

From a command line:

从命令行:

FOR %i IN (*) DO ECHO %i

From a batch file:

从批处理文件:

FOR %%i IN (*) DO ECHO %%i

回答by Jay

This for-loop will list all files in a directory.

这个 for 循环将列出目录中的所有文件。

pushd somedir
for /f "delims=" %%f in ('dir /b /a-d-h-s') do echo %%f
popd

"delims=" is useful to show long filenames with spaces in it....

“delims=”对于显示带有空格的长文件名很有用....

'/b" show only names, not size dates etc..

'/b" 只显示名称,不显示日期大小等。

Some things to know about dir's /a argument.

关于 dir 的 /a 参数需要了解的一些事情。

  • Any use of "/a" would list everything, including hidden and system attributes.
  • "/ad" would only show subdirectories, including hidden and system ones.
  • "/a-d" argument eliminates content with 'D'irectory attribute.
  • "/a-d-h-s" will show everything, but entries with 'D'irectory, 'H'idden 'S'ystem attribute.
  • 任何“/a”的使用都会列出所有内容,包括隐藏和系统属性。
  • “/ad”只会显示子目录,包括隐藏的和系统的。
  • “/ad”参数消除了带有 'D'ictory 属性的内容。
  • "/adhs" 将显示所有内容,但带有 'D'irectory, 'H'idden 'S'ystem 属性的条目。

If you use this on the commandline, remove a "%".

如果您在命令行上使用它,请删除“%”。

Hope this helps.

希望这可以帮助。

回答by Sam Meldrum

%1 refers to the first argument passed in and can't be used in an iterator.

%1 是指传入的第一个参数,不能在迭代器中使用。

Try this:

尝试这个:

@echo off
for %%i in (*.*) do echo %%i

回答by Axeman

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

Try "HELP FOR" in cmd for a full guide

在 cmd 中尝试“帮助”以获得完整指南

This is the guide for XP commands. http://www.ss64.com/nt/

这是 XP 命令的指南。http://www.ss64.com/nt/

回答by jafarbtech

To iterate through all files and foldersyou can use

通过所有文件和文件夹重复,您可以使用

for /F "delims=" %%a in ('dir /b /s') do echo %%a

To iterate through all folders onlynot with files, then you can use

仅遍历所有文件夹而不遍历文件,则可以使用

for /F "delims=" %%a in ('dir /a:d /b /s') do echo %%a

Where /swill give all results throughout the directory tree in unlimited depth. You can skip /sif you want to iterate through the content of that folder not their sub folder

哪里/s将给出整个目录树中无限深度的所有结果。/s如果要遍历该文件夹的内容而不是其子文件夹,则可以跳过

Implementing search in iteration

在迭代中实现搜索

To iterate through a particular named files and foldersyou can search for the name and iterate using for loop

遍历特定命名的文件和文件夹,您可以搜索名称并使用 for 循环进行迭代

for /F "delims=" %%a in ('dir "file or folder name" /b /s') do echo %%a

To iterate through a particular named folders/directories and not files, then use /ADin the same command

遍历特定命名的文件夹/目录而不是文件,然后/AD在同一命令中使用

for /F "delims=" %%a in ('dir "folder name" /b /AD /s') do echo %%a

回答by datchung

I had trouble getting jop's answer to work with an absolute path until I found this reference: https://ss64.com/nt/for_r.html

在找到此参考文献之前,我无法获得使用绝对路径的 jop 答案:https://ss64.com/nt/for_r.html

The following example loops through all files in a directory given by the absolute path.

以下示例循环遍历由绝对路径指定的目录中的所有文件。

For /R C:\absoulte\path\ %%G IN (*.*) do (
  Echo %%G
)

回答by Max

It could also use the forfilescommand:

它还可以使用forfiles命令:

forfiles /s 

and also check if it is a directory

并检查它是否是目录

forfiles /p c:\ /s /m *.* /c "cmd /c if @isdir==true echo @file is a directory"