7zip Batch-file, Wildcard Compress contents of subfolders - batch-file

I have 5000 uniquely named parent folders. Each parent folder contains 5-10 subfolders. I need each subfolder within the 5000 folders compressed then deleted, leaving me with one .7z file per subfolder while maintaining the 5000 parent folder's structure.
I have always used the below script for compressing my parent folders. However I am unsure of how to change it to compress all folders within the 5000 folders. I assume it will involve some wildcards but extensive google searches have yielded nothing.
for /D %%A in (*) do "C:\Program Files\7-Zip\7z.exe" a -t7z -m9=lzma2 -mx -mmt2 "%%A.7z" -xr!*.bat "%%A"
The easiest solution would be to create a bat file to copy the above bat file into each folder, run it, then delete it. However I am assuming 7z has this functionality and I can just not find it.
Edit: Posted the wrong example

Add an inner loop which will enumerate the subfolders and rd to delete them:
for /D %%O in (*) do (
for /D %%I in ("%%O\*") do (
"C:\Program Files\7-Zip\7z.exe" a -t7z -m9=lzma2 -mx -mmt2 "%%I.7z" -r -xr!*.bat "%%I"
rd /s /q "%%I"
)
)
It will preserve absolute paths in the archives, so if you want to exclude the base folder use cd/d:
for /D %%O in (*) do (
for /D %%I in ("%%O\*") do (
pushd "%%~dpnxI"
"C:\Program Files\7-Zip\7z.exe" a -t7z -m9=lzma2 -mx -mmt2 "..\%%~nxI.7z" -r -xr!*.bat .
popd
rd /s /q "%%~dpnxI"
)
)
Be careful with rd, maybe you'll want to do a test run without it.

Related

Windows rmdir batch to delete FOLDERS but skip files

So i have a dir called c:\user\jdoe\desktop\Folder1\
Inside folder1 i have many folders with subfolder and files.
I have a 7-Zip batch that zips all the folders in place.
So now i have subfolders and subfolders.zip inside folder1.
I need a batch that will detele all the non zipped folders but skip the zip files and the .bat file that lives in c:\user\jdoe\desktop\Folder1\
Any ideas?
If i can get a 7-zip batch that will zip fodlers (example subfolder1.zip subfolder2.zip) and then afterwards delete all non zipped folders that would be great.
Been all over the internet and after deleting half my data by testing my own scripts i have now decided to come here.
So after a long collaboration with a friend we came up with this.
Tested this and it is working 100%
REM create list of directories
SET cwd="C:\Users\user\Desktop\New folder"
SET date=date /T
CD %cwd%
DIR /AD /B >dirlist.txt
REM Zip contents of each directory
for /f "tokens=*" %%a in (dirlist.txt) do (
"c:\program files\7-zip\7z.exe" a "%%a.zip" "%%a"
del /s /q "%%a"
rmdir /s /q "%%a"
)
del dirlist.txt
pause

Batch file to zip files inside a folder with a specific file type

I need a batch file to zip files inside a folder with a specific file type. The file type is (.asc).
Could anyone help me correct the code I have below?
Thanks.
for /r "C:\Test_Results\Report_%Date:~-10,2%_%Date:~-7,2%_%Date:~-4,4%" %%G in (.asc*)do (
if /i "%%~nxa"==".asc" (
pushd "%%a"
REM zip all files in the backup directory
FOR %%A IN (*.asc* *.cpi*) DO "C:\Program Files\WinRAR\WinRAR.exe" a -r "%%~nA.rar" "%%A"
FOR %%A IN (*.asc *.cpi) DO DEL "%%A"
popd
)
)
The code below zips files inside any folder called Logfile. This works well but I now need to do something a little different (as mentioned in the first post).
#J.Baoby, I have changed the .asc* to *.asc, but I'm still having issues with zipping the files. I'm actually not sure what the IF statement after the FOR loop is doing. Is it needed here?
#echo 1. This step zips all files inside any folder with the name Logfile under C:\TestReports\Report_today's date.
for /d /r "C:\TestReports\Report_%Date:~-10,2%_%Date:~-7,2%_%Date:~-4,4%" %%a in (Logfile*) do (
if /i "%%~nxa"=="Logfile" (
pushd "%%a"
REM zip all files in the backup directory
FOR %%A IN (*.blf* *.cpi*) DO "C:\Program Files\WinRAR\WinRAR.exe" a -r "%%~nA.zip" "%%A"
FOR %%A IN (*.blf *.cpi) DO DEL "%%A"
popd
)
)

Zipping and Deleting All Source Files With Command Line

Basically what I'm trying to do is to create a program that will zip all the folders where the file is run, check the integrity of the archive, and then delete the source files. Ideally I'd like to all be done using windows/7zip command line.
This is what I have so far:
for /d %%X in (*) do "c:\Program Files\7-Zip\7z.exe" a "%%X.zip" "%%X\" -mx1 -r testarchive *.zip t testarchive.zip -sdel
pause
This zips and checks the files just fine, but it deletes all the newly created archives and their source files except for the last archive created.
Any suggestions are greatly appreciated.
Edit: This looks like a step in the right direction, but it's still not working.
for /d %%X in (*) do "c:\Program Files\7-Zip\7z.exe" a "%%X.zip" "%%X\" -mx1 -r testarchive *.zip t testarchive.zip
for /d %%i in (*) do del "%%~fi\*.*" /q
pause

Recursively find and delete a folder using batch file

I am trying to write a simple batch file which will recursively find and delete a folder. But the following script is not looking under sub folder. Wondering how to do that?
#echo off
cd /d "C:\"
for /d %%i in (temp) do rd /s "%%i"
pause
Thanks!
for /d /r "c:\" %%a in (temp\) do if exist "%%a" echo rmdir /s /q "%%a"
For each folder (/d), recursively (/r) under c:\ test for the presence of a temp folder and if it exist, remove it
directory removal command is only echoed to console. If the output is correct, remove the echo command
The /S switch to rd means
/S Removes all directories and files in the specified directory
in addition to the directory itself. Used to remove a directory
tree.
It does not mean it will search all directories looking for one with the specified name and delete them.
In other words, if you run rd /S Test from the C:\Temp folder, it will delete C:\Temp\Test\*.*, including all subdirectories (of any name) of C:\Temp\Test. It does not mean it will delete C:\Temp\AnotherDir\Test, because that isn't a subfolder of C:\Temp\Test.

How can I batch copy certain files from multiple folders into multiple folders?

I am looking to copy files that are like "MAN" that reside inside multiple folders in one directory into all the folders in another directory.
This is what I have now, it copies the files correctly but not into all the folders inside the directory. just into the directory itself.
for /d %%a in ("C:\test123\*") do #copy "%%~Fa\MAN*" /d "c:\Test Destination\*" 2>NUL
to reiterate the problem, the files are copied into c:\Test Destination* but I want it to copy to every folder in that folder. It would be great if the file already exists there, to not copy it at all.
Thanks!
This may work, but is untested. Test it on some sample files.
#echo off
for /r "C:\test123" %%a in (man*) do (
pushd "c:\Test Destination"
for /d /r %%b in (*) do if not exist "%%b\%%~nxa" copy "%%a" "%%b"
popd
)
pause

Resources