Zipping and Deleting All Source Files With Command Line - batch-file

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

Related

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

7zip Batch-file, Wildcard Compress contents of subfolders

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.

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

batch script to find the directory where a file is located, copy all files located in that directory and sub-directories to another directory

So if I have C:\drivers\* with * indicating many sub-folders, I want to find out where my inf files are located, and then copy ALL files that are located in the same directory where my inf files are located and all sub-directories.
It has been easy to create a script that will copy all .inf files found to my directory:
FOR /R C:\drivers\ %%a in (*.inf *.cat *.sys) do xcopy /c /h /y %%a C:\test
But copying the other files that are located in the same directory and all sub-directories has been difficult.
Such as, if the inf file is located under C:\drivers\sbdrv\hseries\usb30\amdhub\w7 and the sys file is located in the sub-folder of x86, I need the sys file to be kept in the same sub-folder but under the destination of C:\test\x86.
Any ideas?
EDIT: Maybe this will make it easier. As soon as it finds one .inf file in a folder, it should copy the entire folder over to test as well as all sub-folders and move on to the next one. So if it sees the first .inf file located C:\drivers\sb3045\sb407.inf it should copy all files and folders under sb3045 without copying the folder sb3045 itself, and then move on to folder C:\drivers\sb4055\drivers\oem\intel\id6077.inf and copy all files and folders under the intel folder without copying the intel folder itself.
EDIT2:
It looks like this will work, but it is slow as it finds every .inf and is copying over any old files if there is more than one .inf file per folder
#ECHO ON
SETLOCAL ENABLEDELAYEDEXPANSION
CD\
CD drivers
FOR /f "tokens=* delims=" %%B IN ('DIR /b /s /o:gen .inf') DO (
XCOPY "%%~dpB.*" "C:\test\" /e /c /h /y
If anyone has a cleaner or quicker idea, let me know please. Until then, I'll have to work with this one.
try this:
FOR /R C:\drivers\ %%a in (*.inf *.cat *.sys) do xcopy /c /h /y "%%~dpa*" C:\test
for /R C:\WINDOWS\inf %%a in (*.inf*) do (
xcopy /qv /T %%a .\test\
xcopy /qv %%a .\test\
)
pause
this code worked for me.

Resources