问题
I am really new to batch file coding and need your help.
I've these directories:
c:\rar\temp1\xy.jpg
c:\rar\temp1\sd.jpg
c:\rar\temp1\dd.jpg
c:\rar\temp2\ss.jpg
c:\rar\temp2\aa.jpg
c:\rar\temp2\sd.jpg
c:\rar\temp3\pp.jpg
c:\rar\temp3\ll.jpg
c:\rar\temp3\kk.jpg
And I want to compress them to this
c:\rar\temp1\temp1.rar
c:\rar\temp2\temp2.rar
c:\rar\temp3\temp3.rar
How could this be done using WinRAR?
回答1:
This can be done also with WinRAR without using a batch file, not exactly as requested, but similar to what is wanted.
- Start WinRAR and navigate to folder
c:\rar\
. - Select the folders
temp1
,temp2
andtemp3
and click on button Add in the toolbar. - As archive name specify now the folder for the RAR archive, for example
c:\rar\
. - Switch to tab Files and check there the option Put each file to separate archive.
- Click on button OK.
WinRAR creates now 3 RAR archives with the file names temp1.rar
, temp2.rar
and temp3.rar
in folder c:\rar\
with each archive containing the appropriate folder with all files and subfolders.
The list of files to add can be changed also on tab Files by entering for example *.txt
in Files to exclude to ignore text files in those 3 folders on creating the archives.
And finally it makes sense to enter *.jpg
on tab Files in edit field below Files to store without compression as JPEG files usually contain already compressed data and therefore WinRAR cannot really compress the data of the files further.
回答2:
This should work it also checks if the files were compressed alright. You may need to change this part "cd Program Files\WinRAR" depending on where winrar is installed.
@echo Off
Cd\
cd Program Files\WinRAR
rar a -r c:\rar\temp1\temp1.rar c:\rar\temp1\*.jpg c:\rar\temp1\
if "%ERRORLEVEL%"=="0" ( Echo Files compressed
) Else Echo Failed
rar a -r c:\rar\temp2\temp2.rar c:\rar\temp2\*.jpg c:\rar\temp2\
if "%ERRORLEVEL%"=="0" ( Echo Files compressed
) Else Echo Failed
rar a -r c:\rar\temp3\temp3.rar c:\rar\temp3\*.jpg c:\rar\temp3\
if "%ERRORLEVEL%"=="0" ( Echo Files compressed
) Else Echo Failed
Pause
回答3:
This script can work as well:
@echo off
for %%a in ("C:\rar\temp1" "C:\rar\temp2" "C:\rar\temp3") do (
pushd "%%~a"
"C:\Program Files\WinRAR\rar.exe" a -r temp.rar *
popd
)
来源:https://stackoverflow.com/questions/24932604/how-to-compress-each-subfolder-in-a-folder-into-a-separate-rar-archive-using-win