How to delete a file from multiple zip archives using 7-Zip - batch-file

I have a thousand zip archives that all contain a file I want to remove. I can get 7Zip to remove them one file at a time from the command line:
7z d -r archive.zip *.pdf
but how would I apply that across all the files, which are themselves grouped in sub-directories?

Try this:
for /r %v in (*.zip) do 7z d -r "%v" *.pdf
But no idea if it's working, just wrote out of my head :P

FOR /F "tokens=*" %%G IN ('dir /b *.zip') DO 7z.exe d -r %%G *.pdf
This works almost in the same way as the accepted answer. Only the way how the files are gathered is different. Whereas the answer above uses for /r to run through all directories and subdirectories, this one parses the output of the command dir /b *.zip to get all the files relevant.
The 7zip command remains the same and only the parameters are changed.
Note: To run this outside of a batchfile replace %%G with %G

Sometimes simple things can solve the problems...
do the following to delete any file / all file from selected .zip files>
Move the .zip files to a new folder
Select all the files from which files to be deleted
right click and select option "extract each archive to separate folder"
all the zip files shall be converted to folder now.
use and file search tool like SearchMyFiles and find required files, select and delete.
convert the folders back to .zip

Related

How to rename files when using 7zip for recursive extract

I have about 120,000 zip files that I am using the following command to extract:
FOR /F "usebackq" %a in (`DIR /s /b *.zip`) do 7z.exe e %a
It extracts the contents of the files just fine but I created the zip files with a naming system and was wondering how to be able to apply the name of the zip file to the files within each zip.
For example
DR80005.zip contains Moo_Cow_Serenade.pdf
I'd like it to extract as DR80005.pdf
Is there any way to do this?
I can think of a batch-based solution:
Do the same loop you have but redirect the (7z) extraction to another folder/directory
"extract" the name of the zip file to a variable
list the contents of what is in the extraction directory (the last extracted file) and rename/move it (with the name of the zip file you have stored in 2. and the same extension) to the final folder/directory
Just make sure the extraction folder/directory is empty to start with.
EDIT: Here is the code (already "optimized", from the above pseudo-code), for completion:
FOR /F "usebackq" %%a in (`DIR /s /b *.7z`) do (
7za.exe e %%a -otempfolder
ren tempfolder\*.* %%~na.*
move tempfolder\*.* finalfolder
)
Notes: tempfolder is the temporary folder (needs to exist and be empty, prior to execution). In much the same way finalfolder is where all the extracted files will end up, with the new name.
Plus. This code assumes the zip files only have one compressed file inside.
Also. 7za.exe is the command-line executable (of 7zip) .

Batch script to archive subfolder contents using 7zip with FOR /R

I have a folder structure like this:
C:\\\Logs\logs1\tracelogXXXX.log
C:\\\Logs\logs2\tracelogXXXX.log
C:\\\Logs\logs3\tracelogXXXX.log
Each folder has a bunch of tracelogXXXX's, and I've got the pseudocode for a script that loops through each folder, archives each log into its own .zip, and then delete the tracelog left outside the archive (because 7zip doesnt have move functionality).
But I have no batch experience really, and I can't even get the zipping to work properly.
I can't access the documentation for 7zip from where I am currently, so I've tried this:
CD C:\Logs
FOR /R %%i IN ("*.log") DO "C:\...\7za.exe" a -tzip "%%i.zip"
And also this:
CD C:\Logs
FOR /R %%i IN ("*.log") DO "C:\...\7za.exe" a -tzip "%%i.zip" "%%i\"
The first one goes and zips all of \Logs for each instance of a .log file, making many zips each bigger than the last. And the second makes zips for each instanceof a .log file, with nothing in them.
How do I just zip each log file, in its own zip, named after itself, while operating from the parent directory? Deleting the outer files afterwards doesn't seem hard to accomplish once I figure out whats wrong with this syntax, but this is the important part!
You can do this from the command line with no batch file needed:
FOR /F "usebackq tokens=* delims=" %A IN (`DIR "C:\Logs\*.log" /B /S`) DO "C:\Path\To\7za.exe" a "%~dpnA.zip" "%~fA" & DEL "%~fA"
To use in a batch file, just replace each % with %%.

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
)

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.

batch script to rename files across many folders

I had a batch script that rename files inside the folder which looked like this:
ren B:\Backups\*.bc_ *.bc
Now I have files between many folders, the back up creates a new folder with a new name every day, and i need to rename files across several folders.
How can I do it? How to correctly use wild card in this case?
You cannot use a wildcard in the path in your REN statement. You will have to use some form of the FOR command.
Suppose you want to rename all *.bc_ files in the entire folder hierarchy rooted at B:\Backups.
You could use FOR /R to iterate all .bc_ files within the hierarchy and rename each file individually.
for /r "B:\Backups" %%F in (*.bc_) do ren "%%F" "%%~nF.bc"
Or you could use FOR /D /R to iterate all the folders under the root and run your wildcard REN against each of the folders
for /d /r "B:\Backups" %%F in (.) do ren "%%F\*.bc_" *.bc
Both of the commands above are designed to be used in a batch script. Change each double percent into a single percent if you want to run the command from the command line instead of from within a batch file.

Resources