7zip batch multiple files in directory without folder in archive - batch-file

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

Related

Batch extract multiple archives that skip the first folder in the archive and overwrite all files into a single directory

I have about 150 folders with .7z files that I want to extract all the .7z files in order into a different directory, "Z:\master statistics" and automatically overwrite. Below is the batch I'm working with, but I am unsure how to extract the .7zfiles and skip the first parent folder in the .7z.
For example:
sales2015.7z file has the below directories
sales/engines/parts
sales2010.7z file has the below directories
sales/equipment/parts
When it extracts, I am trying to get it to extract from the second directory. So if the batch worked correctly, the output directory would only have the Engine & Equipment folders and all its subdirectories. If a file with the same name exists, it automatically overwrites.
FOR /D /r %%F in ("*") DO (
pushd %CD%
cd %%F
FOR %%X in (*.7z) DO (
"C:\Program Files\7-zip\7z.exe" x -o "%Z:\master statistics%"
)
popd
)

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
)

Zipping files with 7z.exe

I have a folder that contains a batch file named zip.bat with the following code inside:
for /d %%X in (*) do "C:\Program Files (x86)\7-Zip\7z.exe" a "%%X.zip" "%%X\"
The issue that i'm having with this bat is that it zips any folder inside the folder that contains the zip.bat but what I really want it to do is to zip text files not folders.
Can anyone guide me on what i'm doing wrong?
Thank you.
Your question is not very clear to me.
If you want to have each folder containing txt-files in a separate zip-file you can use:
for /d %%X in (*) do "C:\Program Files (x86)\7-Zip\7z.exe" a "%%X.zip" "%%X\*.txt"
If you want a single zip-file named containing all txt-files (with folder structure) you can use:
"C:\Program Files (x86)\7-Zip\7z.exe" a -r txtfiles-%date%.zip "*.txt"
If you want a single zip-file named containing txt-files of the current folder where your cmd/batch file is located you can use:
"C:\Program Files (x86)\7-Zip\7z.exe" a txtfiles-%date%.zip "*.txt"

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.

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
)

Resources