
특정 디렉터리와 해당 하위 디렉터리에 있는 모든 파일의 파일 이름(전체 경로 포함)과 파일 크기가 포함된 텍스트 파일을 생성하려고 합니다(즉, 재귀적으로). 이상적으로는 파일 크기에 쉼표를 사용하는 것을 원하지 않습니다(하지만 만족하겠습니다!).
다음 명령은 이를 수행하지만 파일 크기는 표시하지 않습니다.
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
그다지 효율적이지 않습니다. 수많은 파일이 포함된 폴더에 대해 실행하는 것은 다소 어려울 수 있으므로 먼저 작은 폴더에서 시도해 보세요.
"for /?"에서 도움말 텍스트('I' 대신 'a'를 사용했습니다)
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
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
ㅏ더 완전한디렉토리가 억제되고 파일에 출력(추가)된 답변은 다음과 같습니다.
@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 구성에서는 처리하지 않음).
예 - 틀렸음 (구분 없음; 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 on
echo 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 웹 서버로 많은 데이터를 전송할 때 모든 파일이 복사되었는지 확인하는 데 사용됨).
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++가 아닌 메모장에서만 특수 문자를 올바르게 볼 수 있었습니다(일부 ANSI/UTF-8 인코딩이어야 하며 지금은 정확하게 분석할 시간이 없습니다). 또한 파일 이름 뒤에 크기를 표시하는 솔루션을 찾지 못했습니다. 크기를 바이트 또는 킬로바이트 단위로 표시하려면 "h" 대신 "b" 또는 "k" 옵션을 사용하십시오. 분석하려는 폴더 외부에서 명령을 실행할 때 문제가 발생했습니다. 따라서 올바른 디렉터리(이 예에서는 "C:\your_dir")에 명령을 입력하고 명령에 이를 지정하는 것이 가장 좋습니다.
답변4
나는 이것이 추가 유틸리티가 필요하다는 것을 알고 있습니다, 그러나 다음을 사용하면 매우 간결하게 이 작업을 수행할 수 있습니다.SFK 유틸리티:
sfk cwd +run -yes "sfk list -size -tab $qfile +toclip"
sfk fromclip > C:\_temp\sfk_list_output.txt
(나는 다음을 사용하여 접근 방식을 얻었습니다.sfk 명령 연결은 여기에 있습니다.)