Answers:
这将列出当前目录中的所有文件(并且仅列出文件):
for /r %i in (*) do echo %i
另外,如果您在批处理文件中运行该命令,则需要将%符号加倍。
for /r %%i in (*) do echo %%i
(感谢@agnul)
for /r %i in (*) do ( echo %~nxi )
。这个线程也真的很有用:stackoverflow.com/questions/112055/…。
遍历...
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
不幸的是,我没有找到任何方法可以同时遍历文件和子目录。
只需将cygwin及其bash用于更多功能即可。
除此之外:您是否注意到,MS Windows的内置帮助是描述cmd命令行语法的重要资源?
也可以在这里查看:http : //technet.microsoft.com/zh-cn/library/bb490890.aspx
%file
并且%subdir
只能是一个字符长,即%f
,%s
。
要遍历每个文件,一个for循环将起作用:
for %%f in (directory\path\*) do ( something_here )
就我而言,我还想要文件内容,名称等。
这导致了一些问题,我认为我的用例可能会有所帮助。这是一个循环,它从目录中的每个“ .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!"
)
*限制:val <= %% f将仅获得文件的第一行。
此for循环将列出目录中的所有文件。
pushd somedir
for /f "delims=" %%f in ('dir /b /a-d-h-s') do echo %%f
popd
“ delims =”可用于显示长文件名并带有空格...。
'/ b“仅显示名称,不显示大小日期等。
关于dir的/ a参数要了解的一些事情。
如果在命令行上使用此命令,请删除“%”。
希望这可以帮助。
要遍历所有文件和文件夹,您可以使用
for /F "delims=" %%a in ('dir /b /s') do echo %%a
要仅遍历所有文件夹而不是遍历所有文件,则可以使用
for /F "delims=" %%a in ('dir /a:d /b /s') do echo %%a
where /s
将无限深度地遍历整个目录树中的所有结果。/s
如果要遍历该文件夹的内容而不是其子文件夹,则可以跳过
要遍历特定的命名文件和文件夹,您可以搜索名称并使用for循环进行遍历
for /F "delims=" %%a in ('dir "file or folder name" /b /s') do echo %%a
要遍历特定的命名文件夹/目录而不是文件,然后/AD
在同一命令中使用
for /F "delims=" %%a in ('dir "folder name" /b /AD /s') do echo %%a
它也可以使用forfiles命令:
forfiles /s
并检查它是否是目录
forfiles /p c:\ /s /m *.* /c "cmd /c if @isdir==true echo @file is a directory"
@isdir==true
需要是@isdir==TRUE
我将使用vbscript(Windows脚本宿主),因为在批处理中,我确定您无法确定名称是文件还是目录。
在vbs中,可能是这样的:
Dim fileSystemObject
Set fileSystemObject = CreateObject("Scripting.FileSystemObject")
Dim mainFolder
Set mainFolder = fileSystemObject.GetFolder(myFolder)
Dim files
Set files = mainFolder.Files
For Each file in files
...
Next
Dim subFolders
Set subFolders = mainFolder.SubFolders
For Each folder in subFolders
...
Next
尝试这个:
::Example directory
set SetupDir=C:\Users
::Loop in the folder with "/r" to search in recursive folders, %%f being a loop ::variable
for /r "%SetupDir%" %%f in (*.msi *.exe) do set /a counter+=1
echo there are %counter% files in your folder
它计算目录(和子目录)中的.msi和.exe文件。因此,这也使文件夹和文件作为可执行文件有所不同。
如果您需要在循环中过滤其他文件,只需添加扩展名(.pptx .docx ..)
以我为例,我必须删除临时文件夹下的所有文件和文件夹。所以这就是我最终做的方式。我不得不运行两个循环,一个用于文件,一个用于文件夹。如果文件或文件夹的名称中包含空格,则必须使用“”
cd %USERPROFILE%\AppData\Local\Temp\
rem files only
for /r %%a in (*) do (
echo deleting file "%%a" ...
if exist "%%a" del /s /q "%%a"
)
rem folders only
for /D %%a in (*) do (
echo deleting folder "%%a" ...
if exist "%%a" rmdir /s /q "%%a"
)
这是我在代码中的注释。
我只是通过biatch技巧来复习,所以请原谅任何明显的错误。
我试图在用户需要的地方进行一些修改,以尽力而为地编写一个全解决方案。
一些重要的注意事项:只需将变量更改recursive
为FALSE
,如果你只是想处理的根目录下的文件和文件夹。否则,它将遍历所有文件夹和文件。
C&C最欢迎...
@echo off
title %~nx0
chcp 65001 >NUL
set "dir=c:\users\%username%\desktop"
::
:: Recursive Loop routine - First Written by Ste on - 2020.01.24 - Rev 1
::
setlocal EnableDelayedExpansion
rem THIS IS A RECURSIVE SOLUTION [ALBEIT IF YOU CHANGE THE RECURSIVE TO FALSE, NO]
rem By removing the /s switch from the first loop if you want to loop through
rem the base folder only.
set recursive=TRUE
if %recursive% equ TRUE ( set recursive=/s ) else ( set recursive= )
endlocal & set recursive=%recursive%
cd /d %dir%
echo Directory %cd%
for %%F in ("*") do (echo → %%F) %= Loop through the current directory. =%
for /f "delims==" %%D in ('dir "%dir%" /ad /b %recursive%') do ( %= Loop through the sub-directories only if the recursive variable is TRUE. =%
echo Directory %%D
echo %recursive% | find "/s" >NUL 2>NUL && (
pushd %%D
cd /d %%D
for /f "delims==" %%F in ('dir "*" /b') do ( %= Then loop through each pushd' folder and work on the files and folders =%
echo %%~aF | find /v "d" >NUL 2>NUL && ( %= This will weed out the directories by checking their attributes for the lack of 'd' with the /v switch therefore you can now work on the files only. =%
rem You can do stuff to your files here.
rem Below are some examples of the info you can get by expanding the %%F variable.
rem Uncomment one at a time to see the results.
echo → %%~F &rem expands %%F removing any surrounding quotes (")
rem echo → %%~dF &rem expands %%F to a drive letter only
rem echo → %%~fF &rem expands %%F to a fully qualified path name
rem echo → %%~pF &rem expands %%A to a path only
rem echo → %%~nF &rem expands %%F to a file name only
rem echo → %%~xF &rem expands %%F to a file extension only
rem echo → %%~sF &rem expanded path contains short names only
rem echo → %%~aF &rem expands %%F to file attributes of file
rem echo → %%~tF &rem expands %%F to date/time of file
rem echo → %%~zF &rem expands %%F to size of file
rem echo → %%~dpF &rem expands %%F to a drive letter and path only
rem echo → %%~nxF &rem expands %%F to a file name and extension only
rem echo → %%~fsF &rem expands %%F to a full path name with short names only
rem echo → %%~dp$dir:F &rem searches the directories listed in the 'dir' environment variable and expands %%F to the fully qualified name of the first one found. If the environment variable name is not defined or the file is not found by the search, then this modifier expands to the empty string
rem echo → %%~ftzaF &rem expands %%F to a DIR like output line
)
)
popd
)
)
echo/ & pause & cls