I need to run 7-Zip from a batch file and perform a few tasks, I was wondering if it was possible. Here is my situation:
I have a folder "X:/Archived Backups/" that contains archives and sub-directories with archives.
Some of these archives also contain further archives.
I need to recursively scan the directory and sub-directories and 7-Zip to extract each archive to a folder by the same name (archive name).
I also need it to extract archives within archives within archives etc.
Finally, I need it to delete the archives when extracted (this includes the archives within archives) and only leave the extracted folders.
Is this possible? If so is it possible from the command line? How would I do it?
Many Thanks
:)
Test this to see if it does what you need - it should extract them to x:\extracted\path\filename folders.
It doesn't delete the archives because you have to test this first.
Check the path to 7z.exe first.
#echo off
set "location=x:\extracted"
md "%location%" 2>nul
for /r "X:\Archived Backups" %%a in (*.7z) do (
md "%location%\%%~pna"
pushd "%location%\%%~pna" && ("c:\program files\7-zip\7z.exe" x "%%a" & popd)
)
pause
You can use this post and this as starting points.
For iteration and testing directory:
FOR %%i IN (%VAR%) DO IF EXIST %%~si\NUL ECHO It's a directory
For files, check file extension and extract using 7-zip command :
Related
I have a script to extract all the archives in a folder to folders with names of the archives, I want to modify that so it will extract only a single file from each archive (doesn't matter which one), it will behave as if there's a counter that will count 1 file and move to the next archive.
For example say we have the following files in the folder, each archive contains what's in the curly braces:
a.7z {folder{1.txt, 2.txt, 3.txt}}
b.7z {folder2{4.txt, 5.txt, 6.txt}}
c.7z {7.txt}
So running the script will result in having the following extracted (those are folders):
a{folder{1.txt}}
b{folder2{4.txt}}
c{7.txt}
Is this possible?
This is the script I currently have:
PATH %PATH%;"C:\Program Files\7-Zip";
7z x *.* -o*
You can list the files of the archive using
7z l a.7z
If all the files end with txt, you can use
7z l a.7z | find "txt"
Use this answer to get the first line:
Windows batch command(s) to read first line from text file
Then use this answer to get the file name (last word) from the line and save it in a batch variable FILE_NAME: https://superuser.com/questions/667494/batch-to-extract-a-word-from-a-string-inside-a-text-file
Then use the following command to extract the file:
7z e -i!%FILE_NAME% a.7z -o*
You can use this example to iterate all the *.7z files
Iterate all files in a directory using a 'for' loop
for /r %%i in (*.7z) do echo %%i
All the parts together:
#echo off
for %%i in (*.7z) do call :extract_file %%i
:extract_file
"c:\Program Files\7-Zip\7z.exe" l %1 > temp.txt
for /f "skip=15 tokens=1-10" %%a in (temp.txt) do (
echo %%f
"c:\Program Files\7-Zip\7z.exe" e -i!%%f %1 -o*
goto :end
)
:end
EXIT /B 0
:eof
The following works as desired. The file is "Target.zip". The top-level folder inside the ZIP file is "Target". The desired file, "Renewal.xlsm", is at a deeper level. The directory tree in the zip should be duplicated for the uncompressed file. Unlike Scripting.FileSystemObject (scrrun.dll), Windows Script Host (wshom.ocx), and Microsoft Shell Controls (shell32.dll), this solution runs asynchronously.
This will extract the single file Renewal.xlsm in Target.zip to a folder called Target.
unzip "Target.zip" "Target\NMD\2021-2022\Renewal.xlsm" -d "Target"
https://unix.stackexchange.com/a/57522/20731
unzip is installed on my corporate computer, but not my personal off-the-shelf Windows. By their own report, Unzip has vulnerabilities, so I wouldn't recommend it.
UnZip
Here's a method using 7-Zip (command-line executable 7z), which is a widely trusted tool. The original relative filepath is restored, meaning the original tree of folders leading to that file is recreated.
7z x -i!"Relative\Path\To\YourDesiredFile.ftw" "YourArchive.zip"
If you want all loose files extracted into a single directory, use e instead of x. Check out the -o switch.
Usually I extract .zip and .rar files in a directory maned as the file without the extension.
E.g.:
test.zip
test\
Now I'd like to write a Windows batch script that browses all hard drive to delete all .zip or .rar files whenever in the same directory is present a subdirectory with same name of the file without the extension.
But I really don't know where to start.
Thank you in advance.
This simple script is all you need to delete all zip files within C: drive appropriately.
#for /r c:\ %%F in (*.zip) do if exist "%%~dpnF\*" del "%%F"
You don't even need the script. You can simply run the following from the command line:
for /r c:\ %F in (*.zip) do if exist "%~dpnF\*" del "%F"
Im trying to move a picture in every zip folder in current directory. I searched online for some guidance on how to do it using batch (.bat) and the only solution I figured out is to make a macro of it but it takes too long for it to complete.
Edit:
I have 50 zip folders which I want to add a picture inside each one in a faster way other than draging the picture each time inside each of the 50 zip folders.
I would appreciate any other ideas and help you could offer.
Test this on some sample zip files in a test folder. It assumes 7-zip is in the folder shown.
It's not so much faster as it is easier, without manually manipulating the files.
The actual zipping speed will be pretty much the same.
#echo off
for /f "delims=" %%a in ('dir *.zip /b /a-d') do (
"C:\Program Files\7-Zip\7z.exe" a "%%a" "my-picture.jpg"
)
pause
Adds (or updates) README.txt in the zipfiles foo.zip and bar.zip from README.txt in the current directory:
$ for f in foo.zip bar.zip ; do zip -u $f README.txt ; done
You can package files into a zip file using the Winzip utility. It must be installed on your computer. Follow these steps:
Select the files you need and click on any of the selected ones.
Select Add to Zip
In the Add window, in the Add to Archive field, specify the path and name of the archive to be created.
The ZIP file is saved at the path you specified.
I have Approx 300 zip files that will all have a file called SP_OUT.db contained withing one of the folders in the zip. These folders themselves may or may not be be zipped.
I want to unzipped the file SP_OUT.db and place in a new folder. However as all the files are the same name i want to rename them to include the name of the ZIP in which it came from.
For example
A11_21156_AHDW1_1.zip extract SP_OUT.db and rename it to A11_21156_AHDW1_1SP_OUT.db
A06_21047_APERCLASH1_1.zip extract SP_OUT.db and rename it to A06_21047_APERCLASH1_1.db
Any help with this would be great.
Thanks
What OS are you using?
It looks like there should be a way to do this if you don’t mind installing the program 7zip.
You could then incorporate a 7zip command like the one below to only extract the SP_OUT.db files.
7z e archive.zip -oc:\soft SP_OUT.db -r
extracts all SP_OUT.db files from archive archive.zip to c:\soft folder
May need to try using the x argument instead of e
Source: http://sevenzip.sourceforge.jp/chm/cmdline/commands/extract.htm
.
EDIT: Worked out a script to extract each file.
However I havent worked out yet how to add the original zip name to the start of the extracted SP_OUT.db filename.
I've marked the place in the script below where you woudl need to do this, and added 'REN' (Rename) after it to change the name of the extracted file.
#ECHO off
TITLE All your SP_OUT.db are belong to us
SETLOCAL ENABLEDELAYEDEXPANSION
REM Set your working directories below.
set targetFile=SP_OUT.db
set sourceDir=%CD%\source
set outputDir=%CD%\output
set 7ziplocation=C:\Progra~1\7zip\7z.exe
:start
FOR /f "delims=" %%a IN ('dir/s/b/a-d "%sourcedir%\*.zip"') DO (
%7ziplocation% e %%a -o%outputDir% SP_OUT.db -r
CALL :process2 %%a
)
GOTO :eof
:process2
SET "fdir=%1"
REM ********* %fdir% will be the full path including filename of original zip file.
REM ********* Work out how to get just zip file name and put save to variable here.
REN %outputDir%\SP_OUT.db %put_the_above_zip_name_here%_SP_OUT.db
GOTO :eof
just read the man page:
7-Zip [64] 9.20 Copyright (c) 1999-2010 Igor Pavlov 2010-11-18
Usage: 7z [...] [...]
[]
a: Add files to archive
b: Benchmark
d: Delete files from archive
e: Extract files from archive (without using directory names)
l: List contents of archive
t: Test integrity of archive
u: Update files to archive
x: eXtract files with full paths
-ai[r[-|0]]{#listfile|!wildcard}: Include archives
-ax[r[-|0]]{#listfile|!wildcard}: eXclude archives
-bd: Disable percentage indicator
-i[r[-|0]]{#listfile|!wildcard}: Include filenames
-m{Parameters}: set compression Method
-o{Directory}: set Output directory
-p{Password}: set Password
-r[-|0]: Recurse subdirectories
-scs{UTF-8 | WIN | DOS}: set charset for list files
-sfx[{name}]: Create SFX archive
-si[{name}]: read data from stdin
-slt: show technical information for l (List) command
-so: write data to stdout
-ssc[-]: set sensitive case mode
-ssw: compress shared files
-t{Type}: Set type of archive
-u[-][p#][q#][r#][x#][y#][z#][!newArchiveName]: Update options
-v{Size}[b|k|m|g]: Create volumes
-w[{path}]: assign Work directory. Empty path means a temporary directory
-x[r[-|0]]]{#listfile|!wildcard}: eXclude filenames
-y: assume Yes on all queries
There are many ways to unzip archives on Unix:
How do you recursively unzip archives in a directory and its subdirectories from the Unix command-line?
Unzip a bunch of zips into their own directories
The goal is to find all archives and uncompress them in their own directory (where each archive is found) on Windows.
Optional:
remove the archive after unzip.
record any error message if one archive has any issue during unzip.
I am looking for a plain DOS command line, using, for instance, 7z.exe (which is included in the portable version of PeaZip).
I took a command-line from this thread of the sevenzip project, with a small modification:
FOR /R %I IN (*src.zip) DO (C:\apps\peazip_portable-3.8.WINDOWS\res\7z\7z.exe x "%I" -aoa -o"%~dpI\*" |c:\windows\system32\find.exe "Everything is Ok" >nul &&DEL /F "%I" ||ECHO.%I : EXTRACT FAIL - ARC NOT DELETED >>ERR.TXT)
(multi-line for visibility)
FOR /R %I IN (*src.zip) DO ( \
C:\apps\peazip_portable-3.8.WINDOWS\res\7z\7z.exe x "%I" -aoa -o"%~dpI\*"
|c:\windows\system32\find.exe "Everything is Ok" >nul &&DEL /F "%I"
||ECHO.%I : EXTRACT FAIL - ARC NOT DELETED >>ERR.TXT)
Notes:
I prefer specifying "c:\windows\system32\find.exe" instead of just FIND, because I have other 'find.exe' on my PATH (from msysgit, or gow).
remove the '&&DEL /F "%I"' part if you want to keep the archives in place.
I just unzipped 470 "src.zip" from the Rational Team Concert SDK in two minutes with that one-liner!
This is an old question, but somebody may find this useful. This method worked for me using WinZip and the WinZip Command Line add-on, both available on the WinZip site for registered users.
FOR /R %I IN (*.zip) DO (C:\Program Files (x86)\winzip\wzunzip.exe -ye "%I" "%~pI")
-ye will name the folder after zip filename.
%I is the zip file name
%~pI is the zip file contents destination
Feel free to add the debugging messages in the previous answer. Run the command inside the folder where all your subfolders containing your zip files are stored.