Moving files from one dynamic directory to another using batch script - batch-file

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!

Related

Delete duplicate files of different extensions recursively

I'm trying to create a batch file that can delete .xmlb files is there is a .engb file of the same name in the same folder. I want it to work so that it can check the folder where the batch file is placed as well as all of the sub-folders. I found this question where someone pointed out how to do this in a single folder, but I'm trying to do it in every sub folder. Here's what I have so far:
#echo off
for /r %%I in (*.xmlb) do (
if exist %%~pnI.engb del %%I
)
pause
It gives me a "the system cannot find the path specified" error. I tried changing del with echo, and when I do that it does manage to display the names of the .xmlb files (with .engb extensions), so I'm assuming it can actually find them and my syntax is off somewhere. If I try to use %%~nI like the original code, it won't do anything (because the .engb files are in the same folders as the .xmlb files). I'm sure I'm probably missing some small thing that's preventing it from deleting the right files. Any help is appreciated! I'm attaching two screenshots: one with the batch file in a folder, and then one of the sub-folders that contains .xmlb and .engb files. So in the jungle1, jungle2, and jungle3 folders I want to keep the .engb files and remove any .xmlb files with the same names as .engb files

Batch File to zip files - apply to sub directories

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.

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

Batch script to delete one subfolder and keep another while deleting all but one file in it?

I have something that I could use help with. Let me give you the folder layout as an example:
C:...\Logs\SubfolderA
C:...\Logs\SubfolderB
Each folder contains multiple logs as text files. What I would like to be able to do is to have a batch script which deletes the folder SubfolderB completely, but clears out the files in that folder SubfolderA except for ONE file, while preserving SubfolderA.
Alternately, I'll need another one that deletes the folder SubfolderB completely, then deletes everything in SubfolderA while keeping the folder. Once I have an idea of how to do the one that I first mentioned, however, the next one should be easy enough for me to do.
Currently, I am just running a script from each folder that either deletes all the files in its perspective folder except for the batch file, or deletes all files except for the batch file AND the one file that I want to preserve (the SubfolderA batch script does this), however it would be a lot easier to kill two birds with one stone and only have to execcute one file, if that is possible.
Thanks!
You can delete a directory (including its contents) with rd /s /q foo. You can delete all files in a directory using del /f /q foo\*.
To delete all but one file in a directory you need some criterion that applies to the one file you are keeping, and then it requires a loop over that directory's files:
for %%F in (foo\*) do if not "%%F"=="bar" del "%%F"

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