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

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.

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 extract all multi-volume RAR archives from subfolders of a folder?

I search for a way to unpack multi-volume archives after download via batch.
I download folders with .r?? files in it over a FTP monitoring program and want that WinRAR goes in the first subfolder in the source folder and start unpacking .r00, delete the archive and move the folder with the unpacked files to a new location.
Then the batch script should start this process again with the next subfolder.
Let's say the source folder C:\Users\unpack contains following subfolders with files:
source folder
subfolder1
Archive1.r00
Archive1.r01
Archive1.r02
xxx.txt
subfolder2
Archive2.r00
Archive2.r01
yyy.txt
subfolder3
Archive3.r00
Archive3.r01
Archive3.r02
Archive3.r04
Archive3.r05
zzz.txt
I had start to do this with the script in the link below, but that script can't do what I want, so I have started a new question.
How to unpack all rar archives in all subfolders of a folder and then delete the archives?
The script in the link above unrars all files in all subfolders and then moves the folder with its files to a new location. I want that the script unrars and moves subfolder for subfolder in the source folder.
Edit.1
If winrar is ready with the first subfolder the structure in the source folder should look like this:
source folder
subfolder2
Archive2.r00
Archive2.r01
yyy.txt
subfolder3
Archive3.r00
Archive3.r01
Archive3.r02
Archive3.r04
Archive3.r05
zzz.txt
The files and folders in C:\Users\new-location should look like this:
source folder
subfolder1
xxx.mp4
xxx.txt
subfolder2
yyy.mp4
yyy.txt
subfolder3
zzz.mp4
zzz.txt
A possible batch code for this task is:
#echo off
setlocal EnableDelayedExpansion
set "BaseSourceFolder=C:\Users\Unpack"
set "BaseTargetFolder=C:\Users\New-Location"
for /D %%D in ("%BaseSourceFolder%\*") do (
set "TargetFolder=%BaseTargetFolder%\%%~nxD"
if not exist "!TargetFolder!" md "!TargetFolder!"
"%ProgramFiles%\WinRAR\Rar.exe" x -cfg- -idq -y "%%~fD\*.r??" "!TargetFolder!"
if not errorlevel 1 (
del /F /Q "%%~fD\*.r??"
move /Y "%%~fD\*" "!TargetFolder!">nul 2>nul
rd "%%~fD" 2>nul
)
)
rem rd "%BaseSourceFolder%" 2>nul
endlocal
for /? executed in a command prompt window displays help for command for with parameter /D which means for each directory matched by * in base source folder.
In the loop first the target folder name is defined based on name of the subfolder to process. %%~fD and %%~nxD are also explained by for /? whereby folders usually do not have an extension and therefore %%~nD is often also enough.
Next this target folder is created if not already existing.
Then Rar.exe is executed to extract the multi-volume archive in the current subfolder directly to the defined target folder.
*.r?? is used to make this batch file work for multi-volume archives with old naming scheme ArchiveName.r00, ArchiveName.r01, ... as well as better naming scheme ArchiveName.part01.rar, ArchiveName.part02.rar, ... which is used by default by WinRAR version 5.21. RAR automatically skips the archive files processed already during extraction of a multi-volume archive from the list matching *.r??.
Exit code of Rar.exe is evaluated to determine if any error occurred. If exit code assigned to errorlevel is lower than 1, there was no error and the 3 commands of the if branch are executed resulting in deleting first all RAR archive files.
The remaining files in current subfolder are also moved to the current target folder which is the *.txt file in the folder structure example.
As the current subfolder should be empty now, the command rd should be able to remove the directory. In case of an error because subfolder is still not empty, the subfolder remains in base source folder.
The base source folder is empty if everything worked without an error. The commented line after for loop could be used to remove the empty base source folder as well, but keep the folder if anything failed.

Using a loop to rar multiple subfolders in a main folder

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.

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.

Batch file for creating rar

I am really new to batch files so please bear with me. I want to make a batch file that can zip folders inside the main folder to one individual win-rar file.
I have the following line which is making the main folder rar but i need only the folders inside the main folder to be archived.
SET WINRAR="F:\Program Files\WinRAR"
%WINRAR%\WinRAR.exe a -ep1 "F:\Documents and Settings\sys\Desktop\test.rar" "F:\Documents and Settings\sys\Desktop\test"
What I need is to make the folders inside the test folder as individual rar files.
Iterate over the folders to create individual rar files
SET WINRAR="F:\Program Files\WinRAR"
for /D %%f in ("F:\Documents and Settings\sys\Desktop\test\*") do (
%WINRAR%\RAR.exe a -ep1 -r0 "F:\Documents and Settings\sys\Desktop\test\%%~nxf.rar" "%%f"
)

Resources