web-dev-qa-db-ja.com

7-zipを使用してフォルダ内のファイル(各ファイル)を個別に圧縮する方法は?

Windows 7で7-Zipを使用していて、フォルダー内に自動化されたZipファイルのバッチを作成しようとしています。

フォルダ内の各ファイルを個別に圧縮したい。

私のコードを見て、それを最善に行う方法を提案できますか?

すべてをあるフォルダから別のフォルダに圧縮することに成功しましたが、各ファイルを個別に実行する必要もあります。

@ECHO OFF
SET hr=%time:~0,2%
IF %hr% lss 10 SET hr=0%hr:~1,1%

Set TODAY=%date:~4,2%-%date:~7,2%-%date:~10,4%-%hr%%time:~3,2%%time:~6,2%%time:~9,2%

ECHO.

ECHO Compressing files and folders in C:\zipush drive and moving to C:\new
ECHO.
7za a -tzip "C:\new-drive-%TODAY%.Zip" "C:\zipush*" -mx5
ECHO.

ECHO Delete the files in orginal folder 
DEL "C:\zipush\*.*"
PAUSE
1

以下は、<orignalfilename>.<extension>.Zipの命名規則で各ファイルを独自のZipファイルに圧縮するループを完了し、元のファイルを削除するために必要なことです 前のファイルの成功 。必要に応じて、%%~NXA.Zip%%~NA.Zipに変更して、元のファイル名拡張子を新しいZipファイルに含めないようにすることができます。

また、宛先ディレクトリがまだ存在しない場合は、アーカイブコマンドがループ内で期待どおりに機能するように作成する必要があるため、それをスクリプトロジックに追加し、ソースディレクトリと宛先ディレクトリを変数として設定しました。

また、 [〜#〜] for [〜#〜] ループを介したバッチ置換を使用して、個々のZip名とファイル名を処理しました。このトピックに関するいくつかの学習資料からその他のリソースを調べてください(FOR /?)。


スクリプトの例

@ECHO OFF
SET hr=%time:~0,2%
IF %hr% lss 10 SET hr=0%hr:~1,1%

SET TODAY=%date:~4,2%-%date:~7,2%-%date:~10,4%-%hr%%time:~3,2%%time:~6,2%%time:~9,2%

SET SrcDir=C:\zipush
SET DestDir=C:\new-drive-%TODAY%
IF NOT EXIST "%DestDir%" MD "%DestDir%"

ECHO.

ECHO Compressing files and folders in C:\zipush drive and moving to C:\new
ECHO.

ECHO Compressing files and folders in C:\zipush drive and moving to C:\new and then delete from C:\zipush
ECHO.
FOR %%A IN ("%SrcDir%\*.*") DO 7za a -tzip "%DestDir%\%%~NXA.Zip" "%%~A" -mx5 && DEL /Q /F "%%~A"
ECHO.
PAUSE

その他のリソース

  • [〜#〜] for [〜#〜]
  • 条件付き実行
  • [〜#〜] md [〜#〜]

  • FOR /?コマンドプロンプトから

    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
    
0
Pimp Juice IT