
디렉터리의 모든 폴더를 검색하고 해당 폴더 내에 첫 번째/유일한 하위 폴더의 내용이 포함된 zip 파일을 생성하는 솔루션을 찾는 데 어려움을 겪고 있습니다.
여기에 폴더/구조/계층의 예가 있습니다.
I:\Unsorted\Sony - PlayStation Vita\NoNpDrm Physical\SENRAN KAGURA SV [PCSE00398] [NTSC]\PCSE00398
PCSE00398
내부 내용을 동일한 이름의 zip으로 압축하여 폴더에 남겨두고 싶습니다 I:\Unsorted\Sony - PlayStation Vita\NoNpDrm Physical\SENRAN KAGURA SV [PCSE00398] [NTSC]
.
답변1
@echo off
set "_flag=a -ep1 -m5 -cfg- -y -o+"
cd /d "%~dp0" && for /d /r "I:\test\." %%i in (*
)do 2>nul tree.com /a "%%~dpnxi"|findstr /bl \\--- >nul || (
"%ProgramFiles%\WinRAR\Rar.exe" %_flag% "%%~i" "%%~dpni" |find/i ".rar" )
하위 폴더의 마지막 수준에 도달하려면 각 하위 폴더에 다른 폴더가 있는지 확인하기만 하면 됩니다. 그렇지 않으면 이 마지막 수준 폴더를 압축합니다.
for /d /r
모든 폴더를 통과하는 루프를 사용할 수 있으며 루프 내부의 각 폴더에서 tree
명령을 와 함께 사용하면 findstr
현재 폴더에 하위 폴더가 더 있는지 여부를 확인할 수 있습니다.
FOR /R - Loop through files (recursively) FOR /D - Loop through several folders/directories
The option /D /R is undocumented, but can be a useful combination, while it will recurse through all subfolders the wildcard will only match against Folder/Directory names (not filenames) Note: Source linked to ss64.com
다음을 사용하여 모든 하위 폴더에서 재귀적으로 사용할 수 있습니다 .tree "current_looping_folder" /a
for /d /r
, 각 출력을 확인하여findstr "문자열"("\---"
) 교환원에게 리디렉션||
조치를 취합니다(Rar
) 만약에찾을 수 없음\---
명령 tree
출력 의 문자열은 다음과 같습니다 \---Last Folder
.
Folder PATH listing
Volume serial number is A0AD-DC56
F:\SUPER_USER\Q1599429
\---Last Folder
위의 출력은 내가 있는 폴더에서 나온 것입니다. F:\SUPERUSER\Q1599429
여기에는 하위 폴더가 있지만 Last Folder
, 하위 폴더에 있으면 F:\SUPER_USER\Q1599429\Last Folder
아래 출력이 표시됩니다.
Folder PATH listing
Volume serial number is A0AD-DC56
F:\SUPER_USER\Q1599429\Last Folder
No subfolders exist
findstr
문자열을 찾지 못하면 현재 "\---"
폴더에 하위 폴더가 없습니다. 이것이 마지막 폴더입니다.
F:\SUPER_USER\Q1599429\Last Folder
이 경우 하위 폴더가 없으면 명령 명령이 성공하지 못하므로 운영자가 실제 폴더 수준의 마지막 폴더에서 명령을 정확하게 실행하도록 합니다...tree "Actual_Loop_Folder" /a | findstr "\---"
||
Rar.exe
- 관찰: 1
\
문자를 이스케이프하려면 추가 가 필요합니다\
.findstr
... tree.com /a "%%~dpnxi"|findstr /bl \\--- ...
- 관찰: 2이것은 운영자가 기계적으로 어떻게 작동하는지 설명하려고 시도합니다
||
.
command1 || command2
execute command1 || only execute command2 (if) command1 fails
if tree folder /a fails || there is no subfolder in it
there is no subfolder in it || this is the the last subfolder
this is the last subfolder || run rar flags in the \Last Folder
tree /a "%~fi"|findstr "\---">nul || Rar "I:\Unsorted\...\PCSE00398"
- 관찰: 3함께 실행해야 하는 경우
WinRar.exe
대신Rar.exe
다음을 제거/교체/편집하세요.
"%ProgramFiles%\WinRAR\Rar.exe" %_flag% "%%~i" "%%~dpni" |find/i ".rar"
"%ProgramFiles%\WinRAR\WinRar.exe" %_flag% "%%~i" "%%~dpni"
- 관찰: 4을 편집하여 사용자 정의 플래그/스위치를 사용할 수 있습니다.
Rar/WinRar
명령:
<Commands>
a == Add files to archive
<Switches>
cfg- == Disable read configuration
ep1 == Exclude base directory from names
m<0..5> == Set compression level (0-store...3-default...5-maximal)
o[+|-] == Set the overwrite mode
y == Assume Yes on all queries
-----------------------------------------------------------------------
set "_flag=a -ep1 -m5 -cfg- -y -o+"
- 추가 내용:
[√]For 루프
[√]/D 루프의 경우
[√]/R 루프의 경우