Batch File to zip files - apply to sub directories - batch-file

I'm looking to get a batch file to apply to all sub directories.
I have a number of folders. Each folder will contain file pairs
xxx1.mp3 and xxx1.cdg,
xxx2.mp3 and xxx2.cdg,
etc.
I have a 7zip batch file that will look for file pairs and create a single zip file xxx1.zip, xxx2.zip and delete the (now) redundant cdg/mp3 files.
However, this will only work if the bat file is run in each individual folder. What I'm really looking for is a switch to add to the bat file that if I run in the root directory, will run through all sub directories also.
The code I currently run is:
FOR %F IN (*.cdg) DO "C:\Program Files\7-Zip\7Z.exe" a "%~nF.zip" "%~nF.cdg" "%~nF.mp3" -sdel
Any help?

FOR /R %%F IN (*.cdg) DO IF EXIST "%%~dpnF.mp3" pushd "%%~dpF"&ECHO("C:\Program Files\7-Zip\7Z.exe" a "%%~nF.zip" "%%~nF.cdg" "%%~nF.mp3" -sdel&POPD
Note that this is a batch line - to execute from the prompt, reduce each %% in metavariable to % (but it's a whole lot easier to put it into a batch by cut-and-paste;saves all those typos...)
The required commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, delete the string ECHO( which appears before your command to actually execute the commands.
Using for /r will iterate through the filenames matching the supplied mask (it's possible to also add a starting directory - see for /?|more from the prompt.)
Since we know the selected full filename exists, first check that the paired file exists, then switch to the required directory, archive, and switch back.

Related

Making a batch file to zip contents of a folder in seperate zips

I need a batch file to zip the contents of the current directory into individual zip files (each file has its own zip file). I'm interested in learning more about making batch files so help in breaking down what the script means would be appreciated. Below is a batch to zip subdirectories in the current directory, I was going to try to edit it to zip individual files of a directory but I don't really know how to and I can't find a reference sheet or anything. So if you have a link for something similar that would be great.
for /d %%X in (*) do "C:\Program Files\7-Zip\7z.exe" a "%%X.zip" "%%X\"
What I think I understand here is:
'for /d' will loop the batch through the directory
'in (*)' is designating the current directory
'do "executable location"' is designating the process
'a "%%X.zip"' is designating the file type?
I don't really understand what "%%X" is
Thank you in advance.
If you want to change what you have from zipping subfolders to zipping files, just remove the /d, then it will look for files instead of directories.
for %%X in (*) do "C:\Program Files\7-Zip\7z.exe" a "%%X.zip" "%%X"
To answer your question about what %%X is: That's the variable. When you go into the loop using for, you need some variable to represent whatever it is you're looping through.
As you go through the loop, %%X represents whatever file you're working on.
So if the first file was File1.txt, then the first time through the loop, %%X would equal File1.txt.
So the command would be: "C:\Program Files\7-Zip\7z.exe" a "File1.txt.zip" "File1.txt"
Which if I recall the 7-zip arguments, you're saying add File.txt to a zip file named File1.txt.zip. The next time through the loop, %%X represents the next file name.
By the way, if you're running this in the CLI ('command prompt' essentially), then you'd use a single %, for batch files you have to use two (%%).

Moving files from one dynamic directory to another using batch script

I need to write a batch script to move files from one sub-directory to another in Windows 7. These directories have the same structure, and always start with the same characters but end in a random seven digit number.
For example, I need to move all files and folders within \EMAIL_XXXXXXX\ to \MAGMA_XXXXXXX\. These two sub-directories both exist within the same parent directory PROJECT_XXXXXXX. So the MAGMA directory is actually PROJECT_XXXXXXX\MAGMA_XXXXXXX\. Again, the folder numbers will always be random, I just need to run the batch file from within the start of the PROJECT_XXXXXXX directory.
I have successfully been able to delete text files from these directories by placing the below batch file within the parent PROJECT_XXXXXXX directory:
FOR /R %%Y IN (EMAIL*) DO del *.txt /S /Q "%%Y"
As you can see above, using wildcards allows me to use batch to find all the directories starting with EMAIL and delete all files with the extension .txt.
Now I just need to move the remaining files from the EMAIL_XXXXXX sub-directory to the MAGMA_XXXXXXX sub-directory. I understand wildcards can be tricky when manipulating directories. Is there any way I can move these files in a similar way I was able to delete files, running the batch script within the parent directory?
Thanks in advance for your help!

How to run a batch file on all sub directories within a directory?

I have a batch file (ReduceFLACPadding.bat) to reduce padding in all FLAC files within a directory using metaflac.exe
These FLAC files are stored in subdirectories (one per album) within the directory E:\FLAC Library
At the moment I am processing my FLAC files one album at a time, moving the batch file to the targeted subdirectory each time. (The batch file is set to process all FLAC files within a directory)
My question is; is there a way to run this batch file on all the *.FLAC files in all the subdirectories of E:\FLAC in one go?
Please let me know if you need any more information
Windows 7
I would change the command that finds the *.flac files to find it recursively rather than running the batch file itself on every directory.
for /r "e:\flac" %%a in (*.flac) do echo metaflac "%%~fa" "%%~da" "%%~pa" "%%~na" "%%~xa" "%%~nxa"
should provide at least a clue. Without an example metaflac command, a more precise response would take excessive research.
From the prompt, try for /? for details. I've just shown with an echo how to construct some possible source/destination components - the rest is a matter of judicious adhesion of strings. Note the use of quotes however to properly cater for spaces in file/directorynames

Running batch file depending on other files from batch file

I'm writing a batch file which is supposed (among other things) to call another batch file.
The second batch file is depending on files located in the same directory, so, when I try running it from the first batch file, it fails.
Example:
Batch file #1: (Located on C:)
#echo OFF
call C:\Tests\Tests.bat
Output: "Could Not Find C:\Tests\Tests.txt
Reason: Both Tests.bat and Tests.txt are located under: C:\Tests while I'm calling it from C:.
Any ideas?
Thanks a lot,
Idan.
You can use %-dp0 within a batch file to get the path of the currently executing batch file. You can use that path every time you need to access a file from that directory. For example:
call "%~dp0\Tests.bat"
for /f "usebackq ..." ... in ("%~dp0\someFile") do ...
Another option is to temporarily set your current directory to the same path at the top of a batch script using PUSHD. From that point on you don't have to worry about providing the path to the other commands. Just be sure to use POPD before the script exits. That way the script will not impact any script that may have called it.
pushd "%~dp0"
rem Do you work
popd

Batch File to Delete Specific Subfolders

I have three directories, and each of these directories contains 8 subdirectories, and each of those subdirectories contains 2 folders that I want to delete.
The structure:
[Edited the path-name]
C:/Process/CIM
/AB_CIM
/LOG
/DATA
/CC_CIM
/LOG
/DATA
/CIM_RD
...
...
C:/Process/DIM
/AB_DIM
/LOG
/DATA
/CC_DIM
/LOG
/DATA
/DIM_RD
...
...
C:/Process/RRS
...
So basically I want to remove LOG and DATA in all of those subdirectories. I thought of doing this using two separate batch files***:
The first batch file would contain:
call secondBatchFile.cmd CIM
call secondBatchFile.cmd DIM
call secondBatchFile.cmd RRS
Now, I'm not sure how to write the second batch file since those subdirectories have different names (but a common part: AB_*, CC_*, *_RD, etc). I was wondering if someone could help me with it.
Thanks for your help in advance.
*** The reason for doing this using two separate batch files is that I might sometimes need to keep LOG and DATA for one of the parent directories (e.g. CIM), so this way I can just comment out only one line of the first batch file and then run it.
You could do something like this if you're confident that LOG and DATA folders in other directories won't be picked up. Comment out the actual delete in the code below and review the .dat file output before executing.
REM Output all LOG and DATA sub-directories into corresponding DAT files
dir /ad/s/b log* > log_directories.dat
dir /ad/s/b data* > data_directories.dat
REM remove every entry listed in log_directories.dat
for /f %%i in (log_directories.dat) do rd/s %%i
REM remove every entry listed in data_directories.dat
for /f %%i in (data_directories.dat) do rd/s %%i
If you run this from C:, you're probably going to get directories you don't want. But assuming all of your targets are grouped under a dedicated sub-directory (and assuming you run the .bat from that dedicated directory), this won't be a problem.
And by default, this solution gives you your desired log of which directories it will be deleting (log will be overwritten for each run though).
If the 8 sub-folders are always the same pattern, i.e. AB_*, CC_*, *_RD, etc, the second batch file could be something like:
cd C:\%1%
rmdir AB_%1%\LOG
rmdir AB_%1%\DATA
rmdir CC_%1%\LOG
rmdir CC_%1%\DATA
rmdir %1%_RD\LOG
rmdir %1%_RD\DATA
...
cd c:\

Resources