I would like to make a batch script that can delete multiple directories at once. I've searched the web, but only seem to find methods for deleting multiple sub-directories or files. So far I can make a script to delete one specific folder using this structure:
rmdir "C:\Users\%USERNAME%\AppData\Roaming\Microsoft\Windows\Cookies" /S /Q
I then tried a multitude of methods to declare other folders in different combinations and sequences but none seem to work. First I tried putting all other directories in a single line followed by /S /Q, then placing rmdir and /S /Q at the start and end of each new path, then putting each path on a new line by pressing enter. Am I using the wrong commands? Any help is appreciated.
for %%a in ("dirname 1" "dirname2" "as many as you want") do rd /s /q "%%~a"
should do what you want - %%a is set to each [optionally-quoted] argument in turn. You Must use the quotes if the directoryname contains separators like spaces.
You can just use a for loop to iterate over a list of directories, as per:
pax> for %d in (c:\dir1 c:\other\dir2) do echo %d
c:\dir1
c:\other\dir2
In your particular case, it would be something akin to (in a cmd file):
for %%d in (c:\dir1 c:\other\dir2) do rmdir "%%~d" /s /q
You can find subdirectories by name automaticly and delete them
for /f %%d in ('dir /b "C:\exampledir\searched_dir_name*"') do rd /s /q "C:\exampledir\%%d"
this command will delete all directories with name "searched_dir_name" in directory C:\exampledir\
Related
I have one directory with around 100 subdirectories, which include images. I then have a .txt list of images I need to delete from these folders.
Since there are a few thousands, I'm looking for a way to batch delete these using bat file.
But the problem is that my files are in subfolders and subfolders and also filenames include spaces.
Example:
MainFolder/Subfolder One/Image Sunshine.jpg<br>
MainFolder/Subfolder One/Image Cloudy.jpg
I've tried multiple options of del, also by putting all paths in txt file inside double quotes. But nothing deletes them.
Any ideas how to delete only the selected ones from all subfolders?
Renaming or deleting spaces is not an option, since I would have to reupload the remaining images back in the same form as current.
An easy way to do this is by using an FOR statement to read each line of the text document as a string.
For the removal of the files we can use the DEL command along with the /s switch to search all sub-directories.
/P - Prompts for confirmation before deleting each file.
/F - Force deleting of read-only files.
/S - Delete specified files from all subdirectories.
/Q - Quiet mode, do not ask if ok to delete on global wildcard
/A - Selects files to delete based on attributes
The following script will remove all file.extension's from the list.txt file from all sub-directories in the directory tree including the execution directory. This will also remove them if they have spaces in the name as "%%G" is quoted.
Batch File:
for /f "delims== tokens=1,2" %%G in (list.txt) do (del /s /q /f "%%G")
Command Prompt: (Be sure to CD to the main directory!)
for /f "delims== tokens=1,2" %G in (list.txt) do (del /s /q /f "%G")
I wish to copy a file with extension .dyn which is located in each subfolder of main folder(T15_finished). I wish to copy it into respective subfolder at other location(T15). I have created that location using xcopy command. Here, .dyn file is being copied successfully in respective subfolder in T15 folder(see below code). Now, I have a file which has extension as .dynain which is located in the same subfolder as .dyn. And .dynain file is also getting copied which i don't want.
Please see following code which i have created. Can anyone tell me whats wrong ?
#echo off
xcopy D:\Master\SPRINGBACK\CPW\T15_finished D:\Master\SPRINGBACK\FRESH_SPRINGBACK\CPW\T15 /t
xcopy /e D:\Master\SPRINGBACK\CPW\T15_finished\*.dyn D:\Master\SPRINGBACK\FRESH_SPRINGBACK\CPW\T15
pause
Short file names. If you do a dir /x in the folder containing the .dynain file, you will see the 8.3 filename generated for the file, and it will have .dyn extension.
If you know the extensions of the conflicting files, you can use robocopy with /xf switch to indicate the files (*.dynain) to exclude, or you can generate a exclude file to use with xcopy /exclude:file (see xcopy /? for a explanation)
Or, you can generate the list of files to exclude
(for /f "tokens=" %%a in (
'dir /s /b "D:\Master\SPRINGBACK\CPW\T15_finished\*.dyn" ^| findstr /v /i /e /l ".dyn"'
) do #echo(%%~nxa)>excludedFiles.txt
xcopy /exclude:excludedFiles.txt /e D:\Master\SPRINGBACK\CPW\T15_finished\*.dyn D:\Master\SPRINGBACK\FRESH_SPRINGBACK\CPW\T15
Or (as posted by foxidrive), copy all and then delete the non needed files.
The short filename is being matched as well as the long filename. That is the reason.
A solution is to use another command to delete the files:
del /s "D:\Master\SPRINGBACK\FRESH_SPRINGBACK\CPW\T15\*.dynain"
Hello I need a batch for the following process:
A ton of files from sub folders must use the converter to convert filetype from .m2 to .m2_lk
without copying the files in a new folder or something like this.
For example file "tree.m2" is in an sub folder of an sub folder must use the "converter.exe"
I have tried this code but it doesn't work:
FOR /D "X:\XYZ\M2\ToLkM2\" %%G IN (*.m2) DO converter.exe %%G
It looks to me like you simply used the wrong FOR option. You want /R, not /D. Also, The file spec should be quoted on your conversion command line, just in case there are spaces or special characters.
FOR /R "X:\XYZ\M2\ToLkM2\" %%G IN (*.m2) DO converter.exe "%%G"
for /f "tokens=*" %%i in ('dir X:\XYZ\M2\ToLkM2\ /b /s^|find ".m2" /i') do converter.exe %%i
You can try using FORFILES command:
FORFILES /s /M *.m2 /C "cmd /c converter.exe #file #file_lk "
Option /s enables recursing to subdirectories. Read here about many file related substitutions that can be used in command section.
I have a follow bat command which will delete unnecessary files when i give respective file extension
#ECHO OFF
REM Change the path and the extension...
DEL /s /f /q "C:\Users\dell\Desktop\test\*.pdf"
DEL /s /f /q "C:\Users\dell\Desktop\test\*.csv"
ECHO Are you sure ? Press a key to continue.
PAUSE > NUL
ECHO Done
PAUSE
I am in need to insert specific sub-folder to delete along with files. Assume specific sub-folder name may be abc
Can any body help me on this how to insert "delete specific sub-folder"
Use the RD command to remove a (sub)directory. Since a directory may not be empty, you may want to add the /S switch to remove the entire directory tree. And since you are also going to use the command in a batch script, you may need the /Q switch as well, to avoid the confirmation prompt.
So add a command like this to your script:
RD /S /Q "C:\Users\dell\Desktop\test\Specific\Subfolder"
I'm trying to copy the contents of a directory using a DOS batch file that begins with the computer name followed by an underscore and a date stamp. My first impulse was some variation of:
copy D:\%Computername%_\*\\*.* C:\WhateverPath
Of course I could not get this to work. Seems like a simple problem but I don't have much experience with batch files or DOS.
Try:
FOR /d %d IN (D:\%COMPUTERNAME%_*) DO xcopy %d C:\WhateverPath /E
This iterates over all directories (hence the /d) with the pattern %COMPUTERNAME%_* under D:\, and copies the contents of these directories into C:\WhateverPath. /Eis for copying all files and directories, also the empty ones.
For documentation of xcopy, type xcopy /? in a DOS shell (cmd).
Note: If you put this in a batch-file (something.bat), you must replace %d with %%d in the code above.
If you have multiple folders labeled C:\%computername%_%random_time_stamp%\ and you need to access each of them then move all of their contents to a single folder, you can do this:
Given the only underscore in the path is the one between %computername% and your timestamp
FOR /F "USEBACKQ tokens=*" %%F IN (`DIR /b /a:d "C:\" ^| FIND /I "%computername%_"`) DO (
COPY /y "%%~fF\*" "C:\WhateverPath\"
)
That states for every result that comes from the command DIR, /b switch meaning no header information, /a:d meaning only returning directories, I want to find only folders with the computername_ in it, and I want to copy the contents of each of those folders to C:\WhateverPath\ folder.