遞歸列出文件,僅顯示 Windows 命令提示字元中的完整路徑和文件大小

遞歸列出文件,僅顯示 Windows 命令提示字元中的完整路徑和文件大小

我正在嘗試生成一個文字文件,其中包含特定目錄及其任何子目錄中所有文件的文件名稱(包括完整路徑)和文件大小(即遞歸地)。理想情況下,我不想在檔案大小中使用逗號(但我會接受它們!)

以下命令可以執行此操作,但沒有檔案大小:

dir /b /s /a:-D > results.txt

範例輸出如下圖所示:

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

我認為dir單獨使用這是不可能的,儘管我希望被證明是錯誤的。是否有另一種方法可以僅使用命令提示字元中提供的命令來執行此操作?

答案1

這應該可以做到:

@echo off & for /f %a in ('dir /s /b') do echo %~fa %~za

它不是很有效......對包含大量文件的資料夾運行它可能很粗略,所以首先在小資料夾上嘗試它。

從“為/?”幫助文本(我使用“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

答案2

替代方案#1:FOR /R 對我來說比#2 更直覺。
替代方案#2:FOR /F 修正了 BrianAdkins 建議中的「名稱中的空格」問題。
替代方案#3:FORFILES 將是我的選擇,只不過路徑用雙引號引起來。

布萊恩或其他專家可能有一個更優雅的解決方案,或者可能能夠提出十幾個其他解決方案,但這三個解決方案有效。我嘗試使用 FOR TOKENS 但後來不得不刪除頁首和頁腳,所以我恢復到#1。我還考慮創建一個小的 .bat 檔案並調用它,但這會添加另一個檔案(儘管它確實提供了更大的靈活性,就像函數一樣)。

我測試了所有替代方案,包括帶有嵌入空格的目錄和文件名、200+字符的文件名、沒有擴展名的文件名以及小驅動器的根目錄(只是為了時間;有點慢——就像布萊恩建議的那樣——但隨後就這樣了)正在 Windows 資源管理器中搜索;這就是我安裝 Everything 搜尋應用程式的原因)。

替代方案#1:FOR /R

最好的(?)在試圖找出為什麼 Brian 的解決方案對我不起作用時,我查看了 HELP FOR 並決定嘗試 /R 方法。 (建立文件與替代方案 #2 中的相同。)

@echo off & for /R "c:\deletelater\folder with spaces" %A in (*.*) do echo %~fA %~zA

範例 - 作品(與上面不同的目錄來示範遞歸)

@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

替代方案#2:FOR /F

布萊恩·阿德金斯建議:@echo off & for /f %a in ('dir /s /b') do echo %~fa %~za

A正確答案是:

@echo off & for /f "delims=*" %A in ('dir /s /b') do echo %~fA %~zA 

A更完整抑制目錄並將輸出(附加)到文件的答案是:

@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

注意:「delims=*」指定檔案名稱中不允許使用的字元。
注意:第二個指令也透過 /a:-d 抑制目錄。
注意:如果有人選擇不同的變數名稱,則將 FOR 變數名稱設為大寫,以澄清變數和變數參數之間的差異。
注意:附加到文件只是為了笑,因為 OP 要求輸出到文件。

我想我真的應該檢查 ECHO 的狀態並重置它。

問題 - 名稱中的空格

Brian 提出的解決方案不處理包含空格的檔案和資料夾名稱(至少在我的 Vista 配置上不處理)。

範例 - 錯誤 (沒有 delims;包括抑制每個 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

範例 - 正確 (注意輸出到螢幕,而不是附加到文件)

@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

替代方案#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"

結合這些範例並寫入文件會產生答案(幾乎):

forfiles /s  /c "cmd /c if @isdir==FALSE echo @path @fsize" >>ForfilesOut.txt

請注意,輸出中的路徑用引號引起來。是否切換或
不重要。 新增一個空白行來分隔每個目錄將是 IF 的簡單擴充。echo onecho off

警告:使用遮罩/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

此範例包括一個具有超長檔案名稱和無副檔名的檔案名稱的額外目錄。

問題:引號中的路徑

那麼,有沒有一種簡單的方法可以刪除每個 OP 範例中不需要的(?)引號並保存替代方案#3:FORFILES。 (反問:引用是特徵還是缺陷?)

答案3

我發現“du”命令非常有用,它可以在 Windows 和 Unix 上運行(用它來檢查在將大量資料從 Windows PC 傳輸到 Linux Web 伺服器時是否複製了所有檔案):

du -ah "C:\your_dir" > "C:\file_list.txt"

輸出:

15  C:\your_dir/.htaccess
608 C:\your_dir/folder/hello.txt
1.0 C:\your_dir/folder/subfolder/test.txt
1.0 C:\your_dir/folder/subfolder
1.2M    C:\your_dir/folder/world.txt
1.2M    C:\your_dir/folder
9.0 C:\your_dir/no_file_ending
11  C:\your_dir/sample document 1.txt
684K    C:\your_dir/sample document 2 - äöüßÄÖÜáà.txt
1.9M    C:\your_dir

但是,我只能在 Notepad 中正確看到特殊字符,而不能在 Notepad++ 中正確看到特殊字符(一定是某種 ANSI/UTF-8 編碼的東西,現在沒有時間準確分析)。另外我沒有找到在檔案名稱後顯示大小的解決方案。使用“b”或“k”選項而不是“h”來顯示大小(以位元組或千位元組為單位)。在我想要分析的資料夾之外運行命令時遇到了一些問題。因此最好在正確的目錄(本例中為“C:\your_dir”)中輸入命令並在命令中指定它。

答案4

我意識到這需要額外的實用程序,但是您可以使用以下方法相當簡潔地實現這一點sfk實用程式

sfk cwd +run -yes "sfk list -size -tab $qfile +toclip"

sfk fromclip > C:\_temp\sfk_list_output.txt

(我使用的方法是sfk 命令連結在這裡

相關內容