從一個目錄建立多個存檔,而不將目錄根名稱加入到存檔中

從一個目錄建立多個存檔,而不將目錄根名稱加入到存檔中

此批次適用於在資料夾中建立多個存檔,但會將根資料夾新增至存檔。我希望它只添加根目錄中的文件。這是我的程式碼:

for /d %%X in (*) do "C:\Program Files\7-Zip\7z.exe" a "%%X.zip" "%%X"

答案1

你只需要使用一個為了循環省略參數/d,它將像您描述的建立 zip 存檔檔案一樣工作,並且不包含其父資料夾。

命令

筆記: 這將為目錄中的每個文件添加一個 zip 存檔文件,並且 zip 中僅包含該文件。

for %%X in (*) do "C:\Program Files\7-Zip\7z.exe" a "%%~X.zip" "%%~X"

筆記: 這只會將目錄中的所有檔案新增到您指定的一個 zip 檔案中。

for %%X in (*) do "C:\Program Files\7-Zip\7z.exe" a "<MyZipFileName>.zip" "%%~X"

嵌套循環命令

筆記: 這只會將批次檔所在目錄下的目錄中的檔案加入到與目錄名稱相符的 zip 檔案中。

@ECHO ON

FOR /F "TOKENS=*" %%A in ('DIR /S /B /AD "*"') DO (
  FOR %%B IN (*) DO (
      "C:\Program Files\7-Zip\7z.exe" a "%%~fA.zip" "%%~fA\*")
)
EXIT

更多資源

  • 對於/F
  • FOR /?

        tokens=x,y,m-n  - specifies which tokens from each line are to
                          be passed to the for body for each iteration.
                          This will cause additional variable names to
                          be allocated.  The m-n form is a range,
                          specifying the mth through the nth tokens.  If
                          the last character in the tokens= string is an
                          asterisk, then an additional variable is
                          allocated and receives the remaining text on
                          the line after the last token parsed.
    
  • 目錄
  • 為了
  • (添加)命令

相關內容