7-Zip files in all folders (multiple archives) - file

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.

Related

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.

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.

winrar compress folders using batch

In a folder, I have some folders. I want to compress all the folders separately to the foldername.rar and delete the original files. I want to perform this function in batch.
I tried the ones given in other answers but they only compress the files if present, or do nothing. Here , I have to compress only folders to their respective archive. Please help
WinRAR includes two command-line tools, rar.exe and unrar.exe, where rar.exe compresses and unrar.exe uncompresses files.
Both are located in the “C:\Program Files\WinRAR” folder in the installable version.
Assuming, if there are multiple folders under D:\test and you want each folder to get its own .rar file , in the parent folder, from a batch file, this works for you:
#echo off
setlocal
set zip="C:\Program Files\WinRAR\rar.exe" a -r -u -df
dir D:\test /ad /s /b > D:\test\folders.txt
for /f %%f in (D:\test\folders.txt) do if not exist D:\test\%%~nf.rar %zip% D:\test \%%~nf.rar %%f
endlocal
exit
Explanation....
It'll create .rar files of all the folders/subfolders under parent folder D:\test in the same parent folder.
Then, it'll delete all the original folders/subfolders under parent folder D:\test and thus you'll be left only with the archives at the same place.
“a” command adds to the archive
“-r” switch recurses subfolders
“-u” switch. Equivalent to the “u” command when combined with the “a” command. Adds new files and updates older versions of the files already in the archive
“-df” switch deletes files after they are moved to the archive
Maybe something like this, change C:\testfolder\ to you liking.
In my example I had 3 folders (with random files and subfolders inside em): one, two and three
#echo off
cd "C:\testfolder\"
for /d %%G in ("*") do (
"%programfiles%\WinRAR\Rar.exe" a -r %%G.rar %%G
rd /s /q %%G
)

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