Mar*_*son 57 windows command-line
我正在尝试为特定目录及其任何子目录(即递归)中的所有文件生成一个包含文件名(包括完整路径)和文件大小的文本文件。理想情况下,我不希望文件大小中包含逗号(但我会满足于它们!)
以下命令执行此操作,但没有文件大小:
dir /b /s /a:-D > results.txt
Run Code Online (Sandbox Code Playgroud)
示例输出如下所示:
C:\Users\Martin\Pictures\Table\original\PC120013.JPG 227298
C:\Users\Martin\Pictures\Table\original\PC120014.JPG 123234
C:\Users\Martin\Pictures\Table\original\PC120015.JPG 932344
Run Code Online (Sandbox Code Playgroud)
我不认为dir
单独使用这是可能的,尽管我很想被证明是错误的。是否有另一种方法可以做到这一点,仅使用命令提示符中可用的命令?
Bri*_*ins 33
这应该这样做:
@echo off & for /f %a in ('dir /s /b') do echo %~fa %~za
Run Code Online (Sandbox Code Playgroud)
它不是很有效......为包含大量文件的文件夹运行它可能是粗略的,所以先在小文件夹上尝试它。
从“为/?” 帮助文本(我使用了“a”而不是“I”)
In addition, substitution of FOR variable references has been enhanced.
You can now use the following optional syntax:
%~I - expands %I removing any surrounding quotes (")
%~fI - expands %I to a fully qualified path name
%~dI - expands %I to a drive letter only
%~pI - expands %I to a path only
%~nI - expands %I to a file name only
%~xI - expands %I to a file extension only
%~sI - expanded path contains short names only
%~aI - expands %I to file attributes of file
%~tI - expands %I to date/time of file
%~zI - expands %I to size of file
%~$PATH:I - searches the directories listed in the PATH
environment variable and expands %I 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
The modifiers can be combined to get compound results:
%~dpI - expands %I to a drive letter and path only
%~nxI - expands %I to a file name and extension only
%~fsI - expands %I to a full path name with short names only
%~dp$PATH:I - searches the directories listed in the PATH
environment variable for %I and expands to the
drive letter and path of the first one found.
%~ftzaI - expands %I to a DIR like output line
Run Code Online (Sandbox Code Playgroud)
Bil*_*llR 18
替代方案#1:对我来说,FOR /R 比#2 更直观。
备选方案#2:FOR /F 修复了 BrianAdkins 建议中“名称中的空格”的问题。
替代方案 #3: FORFILES 将是我的选择,除了路径是双引号。
布赖恩或其他大师可能有更优雅的解决方案,或者能够提出十几种其他解决方案,但这三个解决方案有效。我尝试使用 FOR TOKENS 但后来不得不去除页眉和页脚,所以我又回到了 #1。我还考虑创建一个小的 .bat 文件并调用它,但这会添加另一个文件(尽管它确实提供了更大的灵活性,就像函数一样)。
我用带有嵌入空格的目录和文件名、一个 200 多个字符的文件名、一个没有扩展名的文件名以及一个小驱动器的根目录测试了所有替代方案(只是为了时间;有点慢——正如布赖恩建议的那样——但后来正在 Windows 资源管理器中搜索;这就是我安装 Everything 搜索应用程序的原因)。
备选方案 1:FOR /R
Best(?)在试图弄清楚为什么 Brian 的解决方案对我不起作用时,我查看了 HELP FOR 并决定尝试 /R 方法。(创建文件与备选方案 2 中的相同。)
@echo off & for /R "c:\deletelater\folder with spaces" %A in (*.*) do echo %~fA %~zA
Run Code Online (Sandbox Code Playgroud)
示例 - 作品(与上面不同的目录以演示递归)
@echo off & for /R "c:\deletelater" %A in (*.*) do echo %~fA %~zA
c:\DeleteLater\Name with Spaces.txt 19800676
c:\DeleteLater\NoSpacesLongName.txt 21745440
c:\DeleteLater\Folder with Spaces\2nd Name with Spaces.txt 5805492
c:\DeleteLater\Folder with Spaces\2ndNoSpacesLongName.txt 3870322
c:\DeleteLater\FolderNoSpaces\3rd Name with Spaces.txt 27874695
c:\DeleteLater\FolderNoSpaces\3rdNoSpacesLongName.txt 28726032
Run Code Online (Sandbox Code Playgroud)
备选方案#2:FOR /F
BrianAdkins 的建议: @echo off & for /f %a in ('dir /s /b') do echo %~fa %~za
一个修正的回答是:
@echo off & for /f "delims=*" %A in ('dir /s /b') do echo %~fA %~zA
Run Code Online (Sandbox Code Playgroud)
抑制目录并输出(附加)到文件的更完整答案是:
@echo Results on %DATE% for %CD% >> YourDirFile.txt & echo off & for /f "delims=*" %A in ('dir /s /b /a:-d') do echo %~fA %~zA >> YourDirFile.txt
Run Code Online (Sandbox Code Playgroud)
注意:“delims=*”指定文件名中不允许的字符。
注意:第二个命令还通过 /a:-d 抑制目录。
注意:如果有人选择不同的变量名,则将 FOR 变量名设为大写以澄清变量和变量参数之间的区别。
注意:附加到文件只是为了咧嘴笑,因为 OP 要求输出到文件。
我想我真的应该检查 ECHO 的状态并重置它。
问题 - 名称中的空格
Brian 提出的解决方案不处理包含空格的文件和文件夹名称(至少在我的 Vista 配置中不是这样)。
示例 - 错误 (没有分隔符;包括每个 OP 的抑制目录,但在文件名之前和之后都有大小以供强调)
截断的名称和大小(6 个文件中的 4 个不正确):
@echo off & for /f %A in ('dir /s /b /a:-d') do echo %~zA %~fA %~zA
C:\DeleteLater\Name
21745440 C:\DeleteLater\NoSpacesLongName.txt 21745440
C:\DeleteLater\Folder
C:\DeleteLater\Folder
C:\DeleteLater\FolderNoSpaces\3rd
28726032 C:\DeleteLater\FolderNoSpaces\3rdNoSpacesLongName.txt 28726032
Run Code Online (Sandbox Code Playgroud)
示例 - 正确 (注意输出到屏幕,未附加到文件)
@echo off & for /f "delims=*" %A in ('dir /s /b /a:-d') do echo %~fA %~zA
C:\DeleteLater\Name with Spaces.txt 19800676
C:\DeleteLater\NoSpacesLongName.txt 21745440
C:\DeleteLater\Folder with Spaces\2nd Name with Spaces.txt 5805492
C:\DeleteLater\Folder with Spaces\2ndNoSpacesLongName.txt 3870322
C:\DeleteLater\FolderNoSpaces\3rd Name with Spaces.txt 27874695
C:\DeleteLater\FolderNoSpaces\3rdNoSpacesLongName.txt 28726032
Run Code Online (Sandbox Code Playgroud)
替代方案 #3:FORFILES (报价问题)
该解决方案直接来自 FORFILES 文档 ( forfiles /?
) 中的最后两个示例。
FORFILES /S /M *.doc /C "cmd /c echo @fsize"
FORFILES /M *.txt /C "cmd /c if @isdir==FALSE notepad.exe @file"
Run Code Online (Sandbox Code Playgroud)
结合这些示例并写入文件会产生答案(几乎):
forfiles /s /c "cmd /c if @isdir==FALSE echo @path @fsize" >>ForfilesOut.txt
Run Code Online (Sandbox Code Playgroud)
请注意,路径在输出中用引号引起来。
是否echo on
或echo off
被切换都没有关系。
添加一个空行分隔每个目录将是 IF 的一个微不足道的扩展。
注意:使用掩码/m *.*
不会返回没有扩展名的文件(如示例中的最后一个文件)!
旁白:这会在每个目录中写入一个文件,其中仅包含该目录的内容:
forfiles /s /c "cmd /c if @isdir==FALSE echo @path @fsize >>ForfilesSubOut.txt"
这不是 OP 想要的,但有时很方便。
示例 - 有效(但在引号中包含完整路径)
forfiles /s /c "cmd /c if @isdir==FALSE echo @path @fsize"
"c:\DeleteLater\Name with Spaces.txt" 19800676
"c:\DeleteLater\NoSpacesLongName.txt" 21745440
"c:\DeleteLater\Folder with Spaces\2nd Name with Spaces.txt" 5805492
"c:\DeleteLater\Folder with Spaces\2ndNoSpacesLongName.txt" 3870322
"c:\DeleteLater\FolderNoSpaces\3rd Name with Spaces.txt" 27874695
"c:\DeleteLater\FolderNoSpaces\3rdNoSpacesLongName.txt" 28726032
"c:\DeleteLater\MoreFiles\A really really long file name that goes on and on 123456789 asdfghjkl zxcvnm qwertyuiop and still A really really long file name that goes on and on 123456789 qwertyuiop and still further roughly 225 characters by now.txt" 447
"c:\DeleteLater\MoreFiles\New Text Document no extension" 0
Run Code Online (Sandbox Code Playgroud)
此示例包括一个具有超长文件名和没有扩展名的文件名的额外目录。
问题:引用路径
那么,是否有一种简单的方法可以删除每个 OP 示例中不需要的(?)引号并保存替代方法 #3:FORFILES。(反问:引号是特征还是缺陷?)
归档时间: |
|
查看次数: |
198522 次 |
最近记录: |