winrar compress folders using batch - batch-file

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
)

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
)

Batch file to delete specific files in multiple folders

I need to delete specific files from 28 folders on the same server.
e.g
C:/folder/DMP/app_x0
C:/folder/DMP/app_x1
C:/folder/DMP/app_x2
DeleteList.txt has a list of files names (with path).
C:/folder/DMP/app_x0/ABC1.txt
C:/folder/DMP/app_x0/ABC1.doc
The batch file needs to have a loop to go through each folder one by one and delete all files mentioned in a text file. Following worked ok for one folder only if I specify the full path before each file's names in DeleteList.txt file.
for /f "delims=" %%f in (DeleteList.txt) do del "%%f"
How to use above so that same code could run 28 times in batch file but each time replaces folder location path. DeleteList.txt will not change.
Any sample code/suggestion would help.
Thx.
One of these questions where we need to read between the lines.
Given we have on file of full filenames with the incorrect path separator, and a requirement to delete the files from 28 directories (but there's no list) then I conclude that since the required subdirectories shown are all subdirectories of C:\folder\DMP\ then the requirement actually is to delete all of the files that match the filenames in the file in all subdirectories of the parent directory of the full filename in the list.
So my suggestion would be
for /f "delims=" %%b in (DeleteList.txt) do del /s "%%~dpb..\%%~nxb"
Naturally, you should test this on a dummy tree before implementing it.
del /s deletes in the target directory and all subdirectories. The target subdirectory is the drive and path of %%b; its parent (..); the name and extension part of %%b.
I prefer to use metavariables that are not available as metavariable-modifiers.
To delete in subdirectories but not in the parent:
for /f "delims=" %%b in (DeleteList.txt) do FOR /d %%c IN ("%%~dpb..\*") DO del /s "%%c\%%~nxb">nul
This time, %%c is set to all subdirectorynames in turn, and the del/s applied to each subdirectory. The /s (...and subdirectories) probably isn't required. The >nul suppresses the deletion report if desired.
Not sure if the OP has need for this anymore, but you can use the following batch script to delete the specific files from multiple folders, starting from a parent folder.
#echo off
for /r "Drive:\path\to\parent_folder" %%d in (.) do (
del "%%~d\ABC1.txt"
del "%%~d\ABC1.doc"
)
The script will loop through the parent directory and its sub-folders, and delete only the specific files from the folders that they appear.
In the case of the post, the parent folder can be "C:\folder".

How to unpack all rar archives in all subfolders of a folder and then delete the archives?

I want to unpack all files in some subfolders which are in a main folder, delete the xxx.rar files after unpacking and move the folder with the files to another location.
Main Folder
Sub Folder1 (with .rar files)
Sub Folder2 (with .rar files)
Sub Folder3 (with .rar files)
This my batch script and works so far.
SET "sourcefolder=C:\Users\Unpack"
FOR /R %sourcefolder% %%X in (*.rar) do (
pushd "%%~dpX"
"C:\Program Files\WinRAR\Rar.exe" x -y "%%X" "*.*" && del "*.rar"
popd
)
for /d /r %sourcefolder% %%x in (*) do move "%%x" "C:\Users\New-Location")
But I want that every subfolder whose files are unpacked immediately moved to the "New-Location" folder and not only after everything has been unpacked in the main folder.
Some ideas what I have to change in the code?
This little batch code hopefully does what you want.
#echo off
set "SourceFolder=C:\Users\Unpack"
set "TargetFolder=C:\Users\New-Location"
if not exist "%TargetFolder%" md "%TargetFolder%"
"%ProgramFiles%\WinRAR\Rar.exe" x -ad -cfg- -idq -r -y "%SourceFolder%\*.rar" "%TargetFolder%"
del /F /Q /S "%SourceFolder%\*.rar">nul
for /D %%D in ("%SourceFolder%\*") do rd "%%D" 2>nul
Console version Rar.exe is more powerful than most users never reading the manual Rar.txt stored in program files folder of WinRAR are aware of.
Unpacking all *.rar files in all subfolders of a source folder can be done directly with Rar.exe as it can be seen because no for loop is used in batch code. Rar.exe supports wildcards on decompressing RAR archive files and switch -r used on command x results in processing all RAR archive files also in all subfolders as the manual explains.
Option -ad meaning append archive name to destination path could be removed from RAR command line if all archives contain a unique folder name, or all archives should be unpacked into same directory with overwriting already existing files from a previous archive unpacked before. Usage of -ad depends on contents of the archive files.
Option -idq means quiet mode, i.e. output only error messages, but no progress information which is faster.
The deletion of all *.rar files after unpacking them is done also without a for loop as command del supports also deletion of all *.rar files in all subfolders of a folder.
Edit:
For deletion of all subfolders in source folder being empty after deleting all RAR files, but keeping the source folder, a for loop is finally necessary as added to code above.
Subfolders not being empty are ignored by command rd because the parameters /S /Q are not used which would delete a subfolder even if not already completely empty.
The error message of rd output to stderr if a subfolder to remove is not empty is redirected to device nul to suppress it.
To delete all subfolders of source folder independent on what those subfolders contain after unpacking all RAR archives, but keep the source folder, the last two lines of batch code above need to be replaced by the following line:
for /D %%D in ("%SourceFolder%\*") do rd /S /Q "%%D" 2>nul
And for deleting the source folder with all its subfolders, the last two lines of batch code above need to be replaced by the following line:
rd /S /Q "%SourceFolder%" 2>nul
Note: A folder can be removed by rd only if it is not the current working directory for any running process on Windows.
Help for each command used in the batch file can be read by opening a command prompt window and run there:
del /?
for /?
if /?
md /?
rd /?
set /?
"%ProgramFiles%\WinRAR\Rar.exe" /?

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

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

Using WINRAR with batch script

I am new to scripting, can some please assist me,
I have batch file that
Looks at the first 8 characters in the file name, creates and
moves those files to new folder with first 8 characters as folder
name.
Then looks at folder created in step 1 for next four series
of character (9,10,11,12)and create and move to another subfolder
with next 4 characters as folder name.
Then looks at folder created in step 2, for extension of every file and create and move
to a new folder with extension as folder name.
For example, I have files that look like this
ABCEFGHI0703xyz.pdf
STUVWXYZ0805xyz.pptx
Move to folder
ABCEFGHI\0703\PDF
STUVWXYZ\0805\PPTX
Keeping in mind first 8 characters are random, next 4 character are year and month, and 9 types of extensions.
I am using this batch script to create these folders:-
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=C:\sourcedir"
SET "destdir=C:\sourcedir"
FOR /f "delims=" %%a IN (
'dir /b /a-d "%sourcedir%\*" '
) DO (
SET name=%%~na
SET ext=%%~xa
SET name=!name:~0,8!\!name:~8,4!\!ext:~1!
MD "!name!" 2>nul
MOVE "%sourcedir%\%%a" "!name!\" >nul
)
GOTO :EOF
Now I would like to add a WINRAR command to archive just the extension folders created in step 3, I am using this command to create the archives.
C:\ ABCEFGHI\0703\PDF>WINRAR A PDF C:\ ABCEFGHI\0703\PDF
Is it possible to add this command to the script?
Ok first you need to have rar.exe in a folder in %PATH%,
i'd suggest you put a link in your Windows\System32 folder like so:
mklink C:\Windows\System32\rar.exe "C:\Program Files\WinRAR\rar.exe"
then you can get to work.
As you already suggested, first create the desired directory tree and then just add the required files to your archive like so:
rar.exe a %ARCHIVE_NAME% MainFolder\*.pdf
rar.exe a %ARCHIVE_NAME% MainFolder\FolderA\*
rar.exe a %ARCHIVE_NAME% MainFolder\FolderB\*
Whereas %ARCHIVE_NAME% is the file name of your new target archive (such as foo.rar)
This will every *.pdf file in 'MainFolder' and everything in 'FolderA' and 'FolderB'. The directory tree will be preserved.
Also, you may want to check whether %ARCHIVE_NAME% already exists, since rar will just add the specified files to an existing archive (possibly overriding them)
Hope this clarifies some things for you.
Edit: doing this recursivly for unknown root directory
set ARCHIVE_NAME=%CD%\pdf_archive.rar
for /r %CD% %%d in ('PDF') do (
if exist "%%d" (
echo Archiving files in: %%d
rar a "%ARCHIVE_NAME% "%%d"\*
)
)
Now this will go into every subdirectory recursivly (starting from your current directory)
Then iw will look for folders called 'PDF' and if they exist it will archive every file in that folder to %ARCHIVE_NAME%

Resources