Batch File to Delete Specific Subfolders - batch-file

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:\

Related

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.

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 delete files matching a certain pattern in their name?

I'm creating a batch file that deletes all Rar$DIa0.??? folders in the %TEMP% directory.
Is this possible, and how to do it?
The ??? is three randomized numbers. And that's where I have trouble with - deleting all folders that have Rar$DIa0. in the name.
for /d is designed for just this type of use. Something like this should work (remove one of the % if you're testing from the command line):
for /d %%i in ("%TEMP%\Rar$DIa0.???") do rd "%TEMP%\%%i"
The /d makes it work on directory names instead of file names.
If you want to make it easier on yourself, change to the %TEMP% folder first:
pushdir
cd /d %TEMP%
for /d %%i in ("Rar$DIa0.???") do rd "%%i"
The ??? makes it only act on folders that have three letters after a .. If your folders don't have just a three letter extension, change .??? to .*. If you've got a typo, and there is no actual . in the foldername, just remove it and use Rar$DIa0??? or Rar$DIa0*
You may want to test it first by changing rd to echo to make sure you get the folders you want before actually deleting them.
For more information about for (pun intended) type for /? from a command prompt.
The command line to use in a batch file for this task is:
#for /D %%I in ("%TEMP%\Rar$DIa0.*") do #rd /Q /S "%%I"
Command FOR with option /D searches in folder defined by environment variable TEMP for subfolders with folder name starting with Rar$DIa0. not having hidden or system attribute set.
The loop variable I holds for each found subfolder matching this folder pattern the name of found folder with full path without double quotes although the path to temp folder very often contains 1 or more spaces.
For that reason just the command RD with the parameters /Q for quiet execution and /S for deleting also all subfolders in the specified folder must be called with referencing the current value of loop variable I enclosed in double quotes.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
for /?
rd /?
By the way: WinRAR deletes the temporary folders usually automatically, except a file is opened from within an archive for viewing/modifying it in another application and WinRAR is closed before the other application is exited with the opened file. In this case WinRAR can't delete the temporary folder with temporarily extracted file because the file is still opened in another application. Of course also command RD can't delete the temporary folder if this folder is still the current directory of another application or a file in this folder is still opened by another application with a read/write access lock.

Copy subfolders with exclusions

I have a messy situation on production server, there is a location on production which should only contain 3 folder however due to not following some process we have got more than 1000 folders and files and I am aiming to clean it via a batch file so that there is no chances of human error.
So I would like to copy all folders and files except 3 folders to a new location. can someone help in this as not able to put logic to exclude these 3 folders.
Create a file called ex.txt that includes 3 lines, each of which is the name of the folder that you would like to exclude from the copy, e.g.:
\folder1\
\folder2\
\folder3\
Now, go to the parent of the high-level directory (say, directory_to_copy), that you would like to copy, in which location exists the ex.txt file, and type
xcopy /e /i /exclude:ex.txt directory_to_copy destination_name
This will exclude the folders folder1, folder2, and folder3 from the copy.
Note: the backslashes \ are important to ensure that the other folders containing those strings (folder1, folder2, and folder3) are not excluded.
This writes tempmove.bat.txt in the same folder as the batch file that contains the move commands to move every folder except the three folders shown (testenv stageenv prodenv).
You can examine the text file before renaming it to .bat to actually use, if it shows the right commands.
Make sure "d:\wrong folders" folder already exists.
#echo off
cd /d "c:\production folder"
(
for /d %%a in (*) do (
if /i not "%%~nxa"=="testenv" if /i not "%%~nxa"=="stageenv" if /i not "%%~nxa"=="prodenv" echo move "%%~fa" "d:\wrong folders\%%~nxa"
)
)>"%~dp0\tempmove.bat.txt"
pause

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"

Resources