Commpress a specific file extension into many subfolder with "FOR" and "7zip" - for-loop

On a server i have a main folder that contain many subfolder and file with some extension.
I would like to compress only file with extension *.bak and each file zip created in right subfolder path.
Example:
- Folder
-- Subfolder1
--- File.zip
--- File.bak
-- Subfolder2
--- File2.zip
--- File2.bak
Now I use this script to compress:
cd /Folder/Subfolder1
set extension=.bak
for %%a in (*%extension%) do 7za.exe a "%%~na.zip" "%%a"
cd /Backup/Subfolder2
set extension=.bak
for %%a in (*%extension%) do 7za.exe a "%%~na.zip" "%%a"
This script is right, but Ihave to remember to create new lines everytime that I have a new subfolder.
I there a solution that check every subfolder for *.bak files and compress it in each subfolder?
Thankyou very much and sorry for bad english.
Addendum:
I tried this command:
set extension=.bak
for **/R** %%a in (*%extension%) do 7za.exe a "%%~na.zip" "%%a"
this search correctly every *.bak in every subfolder, create a single zip for every bak that find BUT it create the n. zip file all in main folder...

Related

How to batch copy files based on a list (txt) in to another folder with same directory structure?

I have a root directory with over 25,000 files in it. These files are in loads of different subdirectories.
I also have a text file with 4300 lines in it, each line is an absolute path to one of the files in the directory. Like below,
c:\dir1\hat1.gif
c:\dir1\hat2.gif
c:\dir1\dir2\hat1.gif
c:\dir1\dir2\hat2.gif
c:\dir1\dir3\cat.zip
c:\dir1\dir3\banana.exe
I also have another root directory witch is a copy of the original root directory structure but all the directories are empty.
I would like to copy all the files listed in the text file to the directory which is empty and place all the copied files inn the respected subdirectories.
if I use the following batchfile I keep getting file overwrite prompts because it is not copying the files to the correct directories.
#echo off
set dst_folder=c:\DSTN2
for /f "tokens=*" %%i in (USEDFILES.txt) DO (
xcopy /S/E "%%i" "%dst_folder%"
)
How do I modify this so the files are copied to the correct directory?
Since you are copying specific files from a list, you need to make sure the directory structure exists in the destination if you want it in a similar folder structure. So using the power of the FOR command modifiers you can get the file path only from the file name in the file list. You will use that modifier to create the destination directory and also use it as the destination for the XCOPY command.
I have taken the liberty of providing best practices for all the code you are using.
#echo off
set "dst_folder=c:\DSTN2"
for /f "usebackq delims=" %%G in ("USEDFILES.txt") DO (
mkdir "%dst_folder%%%~pG" 2>NUL
xcopy "%%~G" "%dst_folder%%%~pG"
)

How to delete file only if subdirectory with same name exists in Windows

Usually I extract .zip and .rar files in a directory maned as the file without the extension.
E.g.:
test.zip
test\
Now I'd like to write a Windows batch script that browses all hard drive to delete all .zip or .rar files whenever in the same directory is present a subdirectory with same name of the file without the extension.
But I really don't know where to start.
Thank you in advance.
This simple script is all you need to delete all zip files within C: drive appropriately.
#for /r c:\ %%F in (*.zip) do if exist "%%~dpnF\*" del "%%F"
You don't even need the script. You can simply run the following from the command line:
for /r c:\ %F in (*.zip) do if exist "%~dpnF\*" del "%F"

Windows Command Copy all files recursively to a Main Folder

I was wondering if there is a quick and dirty command that can copy all files in a directory and it's subdirectory to another folder. The destination folder will have no subdirectories just all the source files.
Also as an added bit of fun, in case there is a file name conflict, not to overwrite but to rename the destination file with something unique, maybe append _1 to the filename?
This will copy the files and prompt if there is a filename conflict.
The third party tool XXcopy has the ability to flatten a directory tree and handle filename conflicts.
#echo off
for /r "d:\folder" %%a in (*) do copy "%%a" "E:\target folder"
To copy for instance all files from the current folder and its sub directories to the parent folder of the current folder you can use:
for /r . %a in (*) do copy %a ..

Batch file: How to save/use subdir-name when iterating thru directories with FOR /R {directory} in (.fileext)

I'm trying to unrar files with a batchscript. I want to iterate thru a folder and its subfolders and whenever there is a .rar file I want it to rar its content into the same folder as the .rar file is in.
I'm using unrar.exe to do this and it needs me to specify the output folder when raring.
If I dont specify any folder it rars it to the "current folder", which is the folder for the batfile.
this is my code:
FOR /R %dir_of_file% %%X in (*.rar) do (
"%unrarexe_path%\unrar.exe" x -y -r %%X {set unrarfolder path}
)
Is there some way to in evry iteration in the for loop, save the path to the current subdirectory that I'm looking thru?
Something like:
set unrar_to_here={haxcommand that will give me current subdirectory in for-loop}
Hope my question is understandable:)
Does this do what you want? Just make the current working directory the directory containing the rar file and extract to the current folder. I'm not sure about the syntax of the unrar command though. I leave that to you. :)
FOR /R %dir_of_file% %%X in (*.rar) do (
pushd "%%~dpX"
"%unrarexe_path%\unrar.exe" x -y -r %%X
popd
)
Try this. It will put the files in the same folder as the archive:
#echo off &setlocal
FOR /R %dir_of_file% %%X in (*.rar) do (
"%unrarexe_path%\unrar.exe" x -y -r "%%~X" "%%~dpX"
)
endlocal

To copy entire subfiles to other (only one) folder without path in windows platform

I did lots of search however i couldnt achieve what i want to do :
I have a folder which contains lots of folders and files under it. I just want to copy entire JPEG files in this folder to another folder without copying previous path tree.
I used this batch command like that :
for /r "C:\source" %%f in ("*.JPG") do #copy %%f "C:\dest" /Y
It works some files which dont contain any space in the name of files. However, it doesnt work files which contains space in file name like that :
it can find and copy "aabb.JPEG"
it can not copy "aa bb.JPEG"
any solutions?
The following works great for me
#ECHO OFF
FOR /R "c:\source" %%F IN (*.jpg) DO (
copy "%%F" "c:\dest\" /Y
)

Resources