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

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
)

Related

7zip batch multiple files in directory without folder in archive

I have a series of file in a series of folders that need to be zipped individually.
C:\folder1\file1-1.txt
C:\folder1\file1-2.txt
C:\folder1\file1-3.txt
C:\folder2\file2-1.txt
C:\folder2\file2-2.txt
C:\folder2\file2-3.txt
C:\folder2\file2-4.txt
C:\folder3\file3-1.txt
C:\folder3\file3-2.txt
C:\folder3\file3-3.txt
C:\folder3\file3-4.txt
C:\folder3\file3-5.txt
I usee the below code to zip each folder, no problem:
for /d %%X in (fol*) do "c:\Program Files\7-Zip\7z.exe" a "%cd%\ZIPS\%%X.zip" "%%X"
And I'm left with 3 zip files in a directory "ZIPS" with the contents of each folder in their own ZIP, as is desired.
C:\ZIPS\folder1.zip
C:\ZIPS\folder2.zip
C:\ZIPS\folder3.zip
However within each file, I'm left with the folder as well as the files.
e.g. C:\ZIPS\folder1.zip\folder1\<files here>
What I'm after is the files being saved in the root of the directory:
e.g. C:\ZIPS\folder1.zip\<files here>
I've tried removing the "%%X" at the end of the line, however all that does is save all files in all directories for each folder.
Any assistance would be appreciated.
Change to the folder where the files to compress are stored
for /d %%X in (fol*) do (
pushd "%%~fX" & (
"c:\Program Files\7-Zip\7z.exe" a "%cd%\ZIPS\%%~nX.zip" *
popd
)
)

How do I copy all files in the source folder and in the subfolders in batch

I have a folder with many subfolders inside them, the subfolders have random numbers/characters as their name, and they all do have an important file in them. But how do i copy all the files in the source folder and in the subfolders to one folder without copy the folders, in a batchfile?
I use Windows7
This should be all you need.
#echo off
for /r "d:\base\folder" %%a in (*) do copy "%%a" "c:\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
)

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.

Create batch file to unzip files in multiple folders

I am trying to create a batch file that searches a folder for zipped files, and unzip them. I want the script to search through all the subfolders in the main folder, and unzip everything it finds. It might be a main folder with several subfolders. Some of the subfolders will contain zipped files, but some will not.
The zipped files will look like "filename.r00", "filename.r01", "filename.r02", and so on. One file will have the name "filename.rar", and it is this file that will need to extracted using 7-zip.
Is it possible to write a batch file that does this, and then deletes all the zip files? I have already installed 7-zip, so if it is possible I would like to use it. If anyone could help me write the batch file, it would be much appreciated!
Thanks!
Here you go
for /r C:\Mainfolder %%a in (filename.r*) do (
7z e %%a -o%%a_Extracted
del %%a /f /q
)
After looking at the switches for 7-Zip this might be faster (untested)
7z x filename.r* -o*_Extracted -r
del filename.r* /f /q
Either way they are extracted to a folder with _Extracted at the end, otherwise it will extract them to a folder with the same name as the archive, and when it deletes the files, it may try and delete the folders too.

Resources