Batch file to zip folders with original names - batch-file

I saw the other links for some solutions but, I need to run the script for every folder under a main one but to give it its original name.
Example:
Main folder
Folder 1
Folder 2
Folder 3
To output (in the same main folder)
Main folder
Folder 1.zip
Folder 2.zip
Folder 3.zip
Thank you in advanced

You'll need zipjs.bat in the same directory as this piece of code:
#echo off
::Alter the line bellow with your parent directory
set "parent_dir=C:\Parrent\"
for /d %%a in ("%parent_dir%\*") do (
call zipjs.bat zipItem -source "%%~fa" -destination "%%~fa.zip" -keep yes -force no
)

I have found a simple solution for this. Suppose you have multiple sub folders to be zipped in a folder. In that case,
1. Download and lnstall 7zip software. Then copy 7z.exe to the particular folder where you want to zip your subfolders.
in a notepad, copy and paste the following and save as .bat file. :
cd "your_MainFolder_path"
for /d %%f in do (7z a -t7z %%f.7z "your_mainFolder_path\%%f")
In case you want to keep the zip files and remove the original folders, add this after the for statement :
rd /s /q "your_mainFolder_path\%%f"

Related

7zip bat / command compress multiple folders to multiple corresponding archives without folder in archive

I'm often compressing folders of images or other project related documents into separate zip files the current command in a bat file I'm using is.
for /d %%X in (*) do "D:\Program Files\7-Zip\7z.exe" a "%%X.7z" "%%X\"
It automatically places the full contents of all folders separate archive files.
My question is if you double click on any of the archives it first navigates to a a folder of the same name as the archive. Is there a way to make it so it doesn't put have to folder, it just has the contents?
Right now I get
D:\User\1501.7z\1501\ contents
I just want
D:\User\1501.7z\ contents
Second if this is possible can it be set up so that if one of the folders has multiple folders in it all the contents of each folder is placed into just the on directory instead of having a multiple folders in the archive.
Thanks,
Tony
This will set the working directory to the folder that you want to zip up and save the zip file one level up from that directory.
#echo off
for /d %%X in (*) do (
PUSHD "%%X"
"D:\Program Files\7-Zip\7z.exe" a "..\%%X.7z" "*"
POPD
)

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.

Trying to create a Bath file that will copy a specific directory path (no lower) and zip the directories in that path

So I have a directory structure that contains log files. The path is something like the following:
C:\Path\TenantName\AppName\other\stuff\I\dont\care\about
What I need to do is to ZIP the AppName directory and everything under it into a ZIP file for each AppName under each Tenant folder.
So example is:
C:\Path\TenantName1\AppName1\
C:\Path\TenantName1\AppName2\
C:\Path\TenantName2\AppName1\
C:\Path\TenantName2\AppName2\
Would be (after the zip):
C:\Path\TenantName1\yyyy.mm.dd_AppName1.zip
C:\Path\TenantName1\yyyy.mm.dd_AppName2.zip
C:\Path\TenantName2\yyyy.mm.dd_AppName1.zip
C:\Path\TenantName2\yyyy.mm.dd_AppName2.zip
We are then moving the files to a Temp Directory that needs to have the following structure as well:
C:\Temp\TenantName\<ZipFiles>
The real issue for me is that I need to dynamically create the TenantName directory within C:\Temp as these will change. Once the files are in the C:\Temp directory, I have Rsync moving them to a centralized server for long term storage.
Any help would be appreciated.
I already for the zip files being created. Using the following command:
for /d %%X in (*) do "c:\Program Files\7-Zip\7z.exe" a "C:\Temp-Log\%DATE:~-4%.%DATE:~7,2%.%DATE:~4,2%_%%X.zip" "%%X\"
you are almost there, you can repurpouse your FOR command to create the folder structure
try this oneliner in the command line....
for /d %d in (*) do #echo md c:\temp\%d
Here is what I developed at the end.
for /d %%d in (*) do (
md c:\temp\%%d
cd %%d
for /d %%X in (*) do "c:\Program Files\7-Zip\7z.exe" a "C:\Temp\%%d\%DATE:~-4%.%DATE:~7,2%.%DATE:~4,2%_%%X.zip" "%%X\"
cd ..
)
This loops through a directory and makes a temporary directory structure copy and zips everything underneath their parent directory.

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"
)

copy all files recursively into a single folder (without recreating folders)

With a batch (.bat), I want to copy all mp3 files that are in 1 subdirectory of D:\TEMP
D:\TEMP\\(anyfolder)\\(anyfile.mp3)
to
E:\MYFOLDER\
I tried with xcopy but
I don't know how to tell "just recurse subfolders of D:\TEMP and not subsubfolders, subsubsubfolders, etc."
When using xcopy, folders are created in the destination (in order to replicate source's folder tree), I don't want this : files should be copied in just 1 single folder.
for command is your friend. Read help for and then try this in the command prompt
for /d %a in (*) do #echo %a
as you see, it follows all subfolders in the current directory.
thus,
for /d %a in (*) do #copy %a\*.mp3 e:\myfolder
will copy all your mp3 to the destination folder.

Resources