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

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
)

Related

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
)

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.

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

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%

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