2017-04-06 134 views
1

我有一個包含名爲「Images」的文件夾的子文件夾。我想用7zip壓縮所有這些圖像文件夾,並將其存儲爲每個原始文件夾放在一邊7s archieve。到目前爲止,我寫了一個.bat文件,並且似乎能夠工作......但出人意料的是,所有創建的archie都是EMPTY。 annyone能否說明爲什麼以及如何解決這個問題? .bat在包含所有子文件夾的頂層文件夾中打開。在Windows中爲子文件夾提供7zip壓縮批處理

@Echo off 
    call:myCompressFunction 
    goto :eof 

:myCompressFunction 
FOR /D %%G IN (*) DO (

IF "%%G" == "Images" (
"C:\Program Files\7-Zip\7z.exe"^
a^
"%%G.7z"^
"%%G\"^
-m0=BZip2^
-mx=9) 

cd %%G\ 
call:myCompressFunction 
cd.. 
) 
exit /b 

當我將代碼粘貼在我運行結束那裏的CMD一個文件夾,它的工作原理:

for /d %G in (*) do "C:\Program Files\7-Zip\7z.exe" a "%G.7z" "%G\" -m0=BZip2 -mx=9 

回答

0

好吧,我發現的故障。應該有%%左右和G.7s %% G無痕跡\

這裏是.BAT文件的完整代碼添加了一些註釋:

REM Compresses all folders and subfolders in the folder it is opened but 
REM The if-function restricts the folders to be compresses to those named in the if-condition 
REM 7z-files are stored aside the original folder 

@Echo off 
call:myCompressFunction 
goto :eof 

REM function executed in all subfolders 
:myCompressFunction 
FOR /D %%G IN (*) DO (

REM compresses all folders named Images 
IF "%%G" == "Images" (
"C:\Program Files\7-Zip\7z.exe"^
a^
%%G.7z^
%%G\^
-m0=BZip2^
-mx=9) 

cd %%G\ 
call:myCompressFunction 
cd.. 
) 
exit /b 
0

我相信這個問題是其實不是quotation marks,但是你使用^做了續行,這實際上逃避了下一行的第一個字符;所以代碼部分...:

(
"C:\Program Files\7-Zip\7z.exe"^
a^
"%%G.7z"^
"%%G\"^
-m0=BZip2^
-mx=9) 

...實際上是由命令解釋器讀取...:

("C:\Program Files\7-Zip\7z.exe" ^a ^"%G.7z" ^"%G\" ^-m0=BZip2 ^-mx=9) 

...這導致了一些逃脫引號^"。特殊字符如SPACE和插入符號^如果它們出現在一對""中,則不會被檢測到。因此有解決的逃逸和tokenising命令行後,下面的令牌收到:

(         // the opening parenthesis is detected 
"C:\Program Files\7-Zip\7z.exe" // a single token as the spaces are in between `""` 
a         // `^a` becomes `a` 
^"%G.7z" ^"%G\" ^-m0=BZip2 ^-mx=9) /* the first `^"` is just a literal character `"` 
             without any function, opposed to a non-escaped `"`; 
             the next `^` appears in between two functional `""`, 
             hence it is kept; the fourth `"` is another opening 
             one, so the rest of the line appears quoted, leading 
             to the next `^`s being kept too and the `)` not to 
             be recognised as the closing parenthesis, so there 
             appears one `)` missing in the code */ 

如果你之前有一個或多個的空格每續行,你不再是無意逃避一些功能特徵,但第一SPACE,...:

(
    "C:\Program Files\7-Zip\7z.exe"^
    a^
    "%%G.7z"^
    "%%G\"^
    -m0=BZip2^
    -mx=9) 

...所以它成爲解釋爲...:

( "C:\Program Files\7-Zip\7z.exe"^a^"%G.7z"^"%G\"^-m0=BZip2^-mx=9) 

...這裏逃脫的空格請勿打擾,所以線標記化到:

(         // the opening parenthesis is detected 
"C:\Program Files\7-Zip\7z.exe" // a single token as the spaces are in between `""` 
a         // `a` is kept unaltered 
"%G.7z"       // this is a correctly quoted token 
"%G\"        // this too 
-m0=BZip2       // this is an unquoted token 
-mx=9        // and this too 
)         /* the closing parenthesis is detected now properly, 
             because it does no longer appear quoted */ 
相關問題