Using a loop to rar multiple subfolders in a main folder - loops

I have a main folder with 3 subfolders ...
C:\Users\Admin\Folder
Folder1
Folder2
Folder3
I want that the batch script grabs the first subdirectory Folder1, copy it to another location, for example C:\Temp\Folder and than WinRAR starts to archive Folder1. After copying Folder1 to another location it can be deleted in the main folder.
After WinRAR finished archiving Folder1 can also be deleted in C:\Temp\Folder. So only the .rar files remain.
Then the script starts from new with Folder2 and do the same as with Folder1.
So far I have only this and do not really know how to implement the above.
"C:\Program Files\WinRAR\Rar.exe" a -ep1 -mt5 -v50M -r -df "NAME-OF-THE-RAR-FILE" "C:\Users\Admin\Folder\*.*"

Open a command prompt window, type and execute for /? and read help output for this command. The option /D is explained already on first help page which is for executing commands on each subdirectory of a directory.
The batch file below archives each subfolder in C:\Users\Admin\Folder using console version of WinRAR with command m (move = archive and delete on success) instead of command a with switch -df.
#echo off
for /D %%F in ("C:\Users\Admin\Folder\*") do (
"%ProgramFiles%\WinRAR\Rar.exe" m -cfg- -ep1 -inul -m5 -mt5 -r -tl -v50M -y "%%~F.rar" "%%~F\"
rd "%%~F"
)
So the result is
C:\Users\Admin\Folder
Folder1.rar
Folder2.rar
Folder3.rar
The folder can contain even more files after archiving and deletion of the folders depending on size of all files of the folders and how much 50 MB volumes are necessary to archive each folder.
The folder name Folder1 is not included in file Folder1.rar because of the backslash in last parameter "%%~F\" at end of third line.
The batch file can be even easier if folder name Folder1 should be also included in the archive file Folder1.rar
#echo off
for /D %%F in ("C:\Users\Admin\Folder\*") do (
"%ProgramFiles%\WinRAR\Rar.exe" m -cfg- -ep1 -inul -m5 -mt5 -r -tl -v50M -y "%%~F.rar" "%%~F"
)
I don't think that copying each folder to a temporary folder just for archiving is necessary here.

Related

How to compress each subfolder in a folder into separate RAR archives with rar.exe using a batch script?

I have multiple folders inside N:\working.
N:\working\1
N:\working\2
N:\working\3
I want to RAR compress them all individually on 64-bit Windows.
Here is the code I have so far:
#ECHO OFF
CD N:\working
SET rar_executable=C:\Rar.exe
ECHO Using WinRAR executable: %rar_executable%
ECHO.
%rar_executable% a -r -ep1 -m0 "Test.rar"
pause
It produces the file Test.rar containing all three folders. But I want to archive the three folders into the archive files 1.rar, 2.rar and 3.rar.
That task can be done with a batch file containing only a single command line.
#for /D %%I in ("N:\working\*") do #"%ProgramFiles%\WinRAR\Rar.exe" a -cfg- -ep1 -inul -m5 -r -- "%%I.rar" "%%I\"
The same command line for usage in a command prompt window without using a batch file:
for /D %I in ("N:\working\*") do "%ProgramFiles%\WinRAR\Rar.exe" a -cfg- -ep1 -inul -m5 -r -- "%I.rar" "%I\"
There is no # left to for as that would be completely useless on running this command line in a command prompt window. # left to Rar.exe with full qualified file name is removed to see the command line output by cmd.exe when a new archive file creation starts.
FOR searches in specified directory N:\working for non-hidden subdirectories and assigns the full qualified directory name to loop variable I before executing Rar.exe for this subdirectory.
Rar.exe adds (a) recursively (-r) all directories and files in the subdirectory found by FOR to a RAR archive file with name of the current subdirectory and file extension .rar created in the specified directory N:\working using best compression (-m5) without any output to console window and ignoring all errors (-inul) which could occur during folder compression. The standard configuration is ignored (-cfg-) on compression. The name of the compressed subdirectory with its path is not added to the RAR archive file (-ep1 and folder to compress ends with a backslash).
Open a command prompt window, run for /? and read the output help explaining option /D (directory search).
Open text file Rar.rxt in program files folder of WinRAR with a double click which is the manual of Rar.exe explaining the command a and the switches used here.
This task could be also done with WinRAR.exe using a profile created before with starting WinRAR.exe with the profile name as only option on the command line stored in a shortcut file. This solution would not open a console window because of WinRAR.exe is a Windows GUI desktop application.
try this script as a .bat file which will compress each folder as a RAR file within the current directory and also will show some additional detail which is very useful while compressing a large size of data.
.Bat File Script
#echo off
#for /D %%I in (".\*") do echo started at %date% %time% compressing... "%%I" && #"%ProgramFiles%\WinRAR\Rar.exe" a -cfg- -ep1 -inul -m5 -r -- "%%I.rar" "%%I\"
echo "Completed Successfully !!!"
pause
Sample Outout

How to compress text files of N number sub folder which contains N number text files?

Below is a directory structure where I have to compress all *.txt files in the app* subdirectories into a RAR archive with current date in name with deletion of archived files and folders.
I am totally new on working on command line and writing batch scripts and know only some basics.
Are there any suggestions on how to solve this task using Rar or WinRAR?
E:
Data
Log
makeRar.bat
app1
1.txt
2.txt
3.txt
.
.
.
n.txt
app2
1.txt
2.txt
3.txt
.
.
.
n.txt
app3
1.txt
2.txt
3.txt
.
.
.
n.txt
.
.
.
appn
1.txt
2.txt
3.txt
.
.
.
n.txt
----------- After execution of makeRar.bat file -------------
E:
Data
Log
makeRar.bat
app1
4/1/2016_log.rar
app2
4/1/2016_log.rar
app3
4/1/2016_log.rar
I have an answer for this problem. But this solution is using a static path, and not a dynamic path. So when this batch file is executed, it produces the output exactly as wanted, but only for a specific app folder according to code below.
set loc="C:\Program Files\WinRAR"
cd /D %loc%
rar.exe a -r -agYYYY-MM-DD -df -ep %source%\app1\log_.rar %source%\app1\*.txt
source must be defined with path of source directory.
EDIT:
I found a better solution for this task for your directory structure by iterating two FOR loops where first is for getting current and subdirectories and second for getting files from subdirectories.
set "source=C:\Program Files\WinRAR"
for /R /D %%s in (.\*) do (
echo %%s
for %%F in (%%s\*.txt*) do (
"%source%\rar.exe" a -r -agYYYY-MM-DD -df -ep %%s\log_.rar %%F
)
)
pause
Based on your given information this should work. If RAR.exe is not in a folder in your PATH variable you will need to add that path to the PATH variable or provide the full path to the executable in the batch file.
#echo off
:: Get Date
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
SET "YYYY=%dt:~0,4%"
SET "MM=%dt:~4,2%"
SET "DD=%dt:~6,2%"
FOR /D %%G IN (app*) DO (
PUSHD "%%~G"
rar a %DD%%MM%%YYYY%_log.rar *.txt
del *.txt
POPD
)
There is the text file Rar.txt in program files folder of WinRAR which is the manual for console version Rar.exe. By viewing this file and first select the command to use – here it is a for add files to archive – and then reading from top to bottom about the switches and building the command line according to task requirements while reading makes it very easy to define the command line for compressing files into a RAR archive.
#echo off
for /D %%D in (app*) do (
echo Creating archive for %%D ...
"%ProgramFiles%\WinRAR\Rar.exe" a -agYYYY-MM-DD -cfg- -df -ep -idq -m5 -md4m -r- -s -y "%%D\log_.rar" "%%D\*.txt"
if errorlevel 1 pause
)
You can read about all the switches in text file Rar.txt.
Command PAUSE is only executed if an error occurred output by Rar.exe to console while all other messages are suppressed because of switch -idq.
The file name format for the RAR archives is log_YYYY-MM-DD.rar because this is much better than DD-MM-YYYY_log.rar once you have multiple such RAR archives in a directory because log_YYYY-MM-DD.rar displayed sorted alphabetically according to file name as by default on Windows results in getting the files also automatically sorted by date with oldest at top.
Rar deletes only the text files successfully added to the archive.
It is of course also possible to use WinRAR for compression:
#echo off
for /D %%D in (app*) do (
echo Creating archive for %%D ...
"%ProgramFiles%\WinRAR\WinRar.exe" a -agYYYY-MM-DD -cfg- -df -ep -ibck -m5 -md4m -r- -s -y "%%D\log_.rar" "%%D\*.txt"
)
WinRAR could create also ZIP archives instead of RAR archives which console version Rar does not support.
Rar switch -idq is replaced by WinRAR switch -ibck to run WinRAR minimized to system tray, i.e. in background. Error messages are displayed in a GUI window which is displayed automatically by WinRAR if an error occurs.
For help on WinRAR commands and switches which slightly differ from Rar switches click in WinRAR in menu Help on Help topics and click on tab Contents on item Command line mode and read the linked pages.
For understanding the used Windows commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
echo /?
for /?
Look how easy it is to archive files using Rar or WinRAR. All really needed is a look into manual respectively help and little knowledge about standard commands of Windows listed on executing help in a command prompt window.
EDIT:
Both batch files above require that the batch file is in directory containing the subdirectories app* and this directory is the current directory. For executing this batch file for example as scheduled task with current directory being %SystemRoot%\System32 the batch code below would be better:
#echo off
for /D %%D in ("%~dp0app*) do (
echo Creating archive for %%~fD ...
"%ProgramFiles%\WinRAR\Rar.exe" a -agYYYY-MM-DD -cfg- -df -ep -idq -m5 -md4m -r- -s -y "%%~fD\log_.rar" "%%~fD\*.txt"
if errorlevel 1 pause
)
The command FOR searches for subdirectories app* now in directory of the batch file independent on what is the current directory. And passed to Rar is now name of archive file with full path and the file name pattern *.txt also with full path instead of using a path relative to current directory.
Well, the line if errorlevel 1 pause should be removed on using this batch file as scheduled task.

How to unpack all rar archives in all subfolders of a folder and then delete the archives?

I want to unpack all files in some subfolders which are in a main folder, delete the xxx.rar files after unpacking and move the folder with the files to another location.
Main Folder
Sub Folder1 (with .rar files)
Sub Folder2 (with .rar files)
Sub Folder3 (with .rar files)
This my batch script and works so far.
SET "sourcefolder=C:\Users\Unpack"
FOR /R %sourcefolder% %%X in (*.rar) do (
pushd "%%~dpX"
"C:\Program Files\WinRAR\Rar.exe" x -y "%%X" "*.*" && del "*.rar"
popd
)
for /d /r %sourcefolder% %%x in (*) do move "%%x" "C:\Users\New-Location")
But I want that every subfolder whose files are unpacked immediately moved to the "New-Location" folder and not only after everything has been unpacked in the main folder.
Some ideas what I have to change in the code?
This little batch code hopefully does what you want.
#echo off
set "SourceFolder=C:\Users\Unpack"
set "TargetFolder=C:\Users\New-Location"
if not exist "%TargetFolder%" md "%TargetFolder%"
"%ProgramFiles%\WinRAR\Rar.exe" x -ad -cfg- -idq -r -y "%SourceFolder%\*.rar" "%TargetFolder%"
del /F /Q /S "%SourceFolder%\*.rar">nul
for /D %%D in ("%SourceFolder%\*") do rd "%%D" 2>nul
Console version Rar.exe is more powerful than most users never reading the manual Rar.txt stored in program files folder of WinRAR are aware of.
Unpacking all *.rar files in all subfolders of a source folder can be done directly with Rar.exe as it can be seen because no for loop is used in batch code. Rar.exe supports wildcards on decompressing RAR archive files and switch -r used on command x results in processing all RAR archive files also in all subfolders as the manual explains.
Option -ad meaning append archive name to destination path could be removed from RAR command line if all archives contain a unique folder name, or all archives should be unpacked into same directory with overwriting already existing files from a previous archive unpacked before. Usage of -ad depends on contents of the archive files.
Option -idq means quiet mode, i.e. output only error messages, but no progress information which is faster.
The deletion of all *.rar files after unpacking them is done also without a for loop as command del supports also deletion of all *.rar files in all subfolders of a folder.
Edit:
For deletion of all subfolders in source folder being empty after deleting all RAR files, but keeping the source folder, a for loop is finally necessary as added to code above.
Subfolders not being empty are ignored by command rd because the parameters /S /Q are not used which would delete a subfolder even if not already completely empty.
The error message of rd output to stderr if a subfolder to remove is not empty is redirected to device nul to suppress it.
To delete all subfolders of source folder independent on what those subfolders contain after unpacking all RAR archives, but keep the source folder, the last two lines of batch code above need to be replaced by the following line:
for /D %%D in ("%SourceFolder%\*") do rd /S /Q "%%D" 2>nul
And for deleting the source folder with all its subfolders, the last two lines of batch code above need to be replaced by the following line:
rd /S /Q "%SourceFolder%" 2>nul
Note: A folder can be removed by rd only if it is not the current working directory for any running process on Windows.
Help for each command used in the batch file can be read by opening a command prompt window and run there:
del /?
for /?
if /?
md /?
rd /?
set /?
"%ProgramFiles%\WinRAR\Rar.exe" /?

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.

7-Zip files in all folders (multiple archives)

How do I make 7-Zip recursively archive all files in a folder for all parent folders?
Currently I have this:
for /d %%X in (C:\Users\mikejoh\Desktop\Modst\Ziptest\*) do "c:\Program Files\7-Zip\7z.exe" a "%%X\*.7z" "%%X\win\*"
But that only takes the parent folders and zips them in the same location...
I believe I would be able to add something like a DEL "%%X% at the end to make it delete the files. However, in this case it would delete the whole folder :(
Example: I have these folders below:
These folders contain these folders:
Where I need to zip all files in the Win folder:
So for every folder it should make an archive inside the Win folder with all files inside the win folder.
If everything goes well, it should delete the archived files.
Would something like this be possible to create in a BAT file?
Let's say the folder Modst\ZipTest on your desktop contains following folders and files:
Test1
Win
File1.txt
File2.txt
File3.txt
xxx.txt
Test2
Win
File1.txt
File2.txt
File3.txt
yyy.txt
Test3
Win
File1.txt
File2.txt
File3.txt
zzz.txt
And the result should be following list of files and folder with all missing files in a Win subfolder added to the *.zip file in the same subfolder.
Test1
Win
Test1.zip
xxx.txt
Test2
Win
Test2.zip
yyy.txt
Test3
Win
Test3.zip
zzz.txt
Then this can be achieved with this batch code using WinRAR:
#echo off
for /D %%A in ("%USERPROFILE%\Desktop\Modst\ZipTest\*") do (
"%ProgramFiles%\WinRAR\WinRAR.exe" m -afzip -cfg- -ed -ep -ibck -inul -m5 -tl -u -y "%%~A\Win\%%~nA.zip" "%%~A\Win\*"
)
The advantage is that WinRAR deletes only files which are successfully added to the archive. All files being locked while WinRAR wants to read file contents for compression remain in the folders. Don't know if this batch file is executed ever while files to compress and delete are written currently in the folders by another application.
The 7-Zip solution requires more commands in a batch file:
#echo off
for /D %%A in ("%USERPROFILE%\Desktop\Modst\ZipTest\*") do (
"%ProgramFiles%\7-Zip\7z.exe" u -tzip -mx=9 -y "%%~A\%%~nA_tmp.zip" "%%~A\Win\*">nul
if not errorlevel 1 (
del /F /Q "%%~A\Win\*"
move "%%~A\%%~nA_tmp.zip" "%%~A\Win\%%~nA.zip"
) else (
if exist "%%~A\%%~nA_tmp.zip" del "%%~A\%%~nA_tmp.zip"
)
)
There is no test made if each file is really added to the archive before it is deleted. No file is deleted if any error occurred during compression of any file in a Win subfolder with exception of the ZIP file in parent folder if the ZIP file was created at all.
NOTE: The path to WinRAR.exe or 7z.exe can be different on your computer and must be adapted in this case in the batch code.

Resources