当前位置: 动力学知识库 > 问答 > 编程问答 >

batch file - Compress only first subFolder in mainFolder to a new folder

问题描述:

Using the WinRAR command line (C:\Program Files\WinRAR\Rar.exe), what I'm trying to do is compress a single folder in a main folder (C:\Users\%username%\desktop\mainFolder) to a new folder (C:\Users\%username%\desktop\newFolder) and delete the single folder after compression in the main folder.

So that ONLY the first subfolder is compressed every time I start the .bat

  • C:\Users\%username%\mainFolder

    • singleFolder1
    • singleFolder2
    • singleFolder3

So far that does only work for all folders which are in the main folder

c:

cd \Users\%username%\Desktop\newFolder

"C:\Program Files\WinRAR\Rar.exe" a -ep1 -mt5 -m1 -v50M -r "!_RndAlphaNum!" C:\Users\%username%\Desktop\mainFolder\

The !_RndAlphaNum! is because I use a code at batch start that generates random names for the .rar archives.

网友答案:

That is similar to Using a loop to rar multiple subfolders in a main folder and can be therefore easily achieved with

@echo off
for /D %%F in ("%USERPROFILE%\mainFolder\*") do (
    "%ProgramFiles%\WinRAR\Rar.exe" m -ep1 -mt5 -m1 -v50M -r "%USERPROFILE%\desktop\newFolder\%_RndAlphaNum%" "%%~F"
    goto Done
)
:Done

The command goto Done results in breaking FOR loop after processing first directory and continue the batch job below label Done.

Again command m is used instead of a to archive and then delete all files and folders packed into the archive file created directly in the destination folder.

分享给朋友:
您可能感兴趣的文章:
随机阅读: