I'm trying to make a batch file on Windows for deleting all the files in the current directory but excluding 4 file extensions (log, sdb, SDK, bat).
I have tried the Forfiles command on Windows but this delete everything on my current folder (even the bat file). My command is:
#ECHO OFF
FORFILES /M *.* /C "cmd /c IF NOT #ext=="sdb" (IF NOT #ext=="sbk" (IF NOT #ext=="log" (IF NOT #ext=="bat" DEL #FILE)))" /Q
How can I make it work?
internal quotes must be escaped with \
you probably want IF /I (case insensitive) option
you should use #ISDIR to exclude directories
DEL /Q option was after last quote, should be before last quote, but it isn't needed
parentheses are not needed
FORFILES /M option isn't needed since your mask is "all files"
This should work
#echo off
forfiles /c "cmd /c if #isdir equ FALSE if /i not #ext==\"sdb\" if /i not #ext==\"sbk\" if /i not #ext==\"log\" if /i not #ext==\"bat\" del #file"
But the above is very slow, and it sure is a lot to type.
The following is much simpler and faster.
#echo off
for /f "delims=" %%F in ('dir /b /a-d ^| findstr /vile ".sdb .sbk .log .bat"') do del "%%F"
If ROBOCOPY is available to you:
#ECHO OFF
MKDIR temporary_pit
ROBOCOPY . temporary_pit /XF *.sdb *.sbk *.log *.bat /MOV >NUL
RMDIR /S /Q temporary_pit
That is, you are creating a temporary subdirectory, moving the files that are to be deleted to it (which is fast because, as the destination directory is on the same drive, only file names are relocated, not the files' contents), then deleting the subdirectory.
Also, you can do something like this:
#echo off
attrib -r -s *.*
attrib +r +s *.sdb
attrib +r +s *.sbk
attrib +r +s *.log
attrib +r +s *.bat
del *.* /S /Q
attrib -r -s *.sdb
attrib -r -s *.sbk
attrib -r -s *.log
attrib -r -s *.bat
-- Mario
#echo off
setlocal EnableDelayedExpansion
set exclude=.log.sdb.sdk.bat.
for %%f in (*.*) do (
if /I "%exclude%" == "!exclude:%%~Xf.=!" del "%%f"
)
forfiles /s /c "cmd /c (if NOT #ext==\"dqy\" del /s /q #path)" /D -14
This is about as simple as I could get this script. They wanted to keep the macro files (.dqy) but recursively delete everything else older than 14 days.
Runs in the current directory (be careful when testing).
I ran across this topic searching for a way to delete hundres of files created by a virus.
Non of the solutions really worked for me, so I figured out how to do it from a command line. I needed only to keep 2 extensions (mail archive).
This did the trick:
for /R %f in (*) do if not %~xf==.ex1 if not %~xf==*.ex2 del "%f"
I use the /R to work recursive: look in all subfolders.
The %~xf looks at the extension only (for some reason it didn't work without it).
I use the quotes "%f" at the delete command to cover the windows long names with spaces (especially in folder names).
Also for some reason, adding spaces before and behing the == gave errors.
Related
I was recently messing with automating tasks on my computer and i wanted to make a batch file that automatically deletes content inside 2 certain folders.
I tried to delete the content of 2 folders with this batch file:
forfiles -p "C:Test1" -s -m *.* /C "cmd /c del #path"
forfiles -p "C:Test2" -s -m *.* /C "cmd /c del #path"
That seemed to work, but it only deleted files, not folders, and i was looking to delete everything inside them. I tried an alternative, but still the same outcome:
del /s /q "C:Test1\*.*"
del /s /q "C:Test2\*.*"
Can someone give me an explanation on how to also delete folders, not only files?
To delete a folder you have to use the rd command
forfiles -p "C:\Test2" -s -m *.* /C "cmd /c IF #isdir == TRUE rd /S /Q #path"
Here's my suggestion:
#(For %%G In ("C:\Test1" "C:\Test2") Do #PushD "%%~G" && (RD /S /Q . & PopD)) 2> NUL
It uses the RD command only, and does it by asking to quietly remove the current directory and all of its subdirectories. You cannot remove a directory if it is the current directory, so the code will make the directory its current directory, using PushD, if that is successful, &&, the RD command is run, using the relative current directory ., then it returns to the previously current directory, PopD. The RD command, whilst it cannot remove the current directory, can still remove everything in it, and does, although it does generate an error message. I have supressed the error messages by redirecting StdErr, 2> to the NUL device.
Hello this is my first post so here goes nothing. I'm currently working on a task with a supervisor where he wants me to create a Batch script that will whip out all the contents in a student (G) drive. When I run this command it only deletes files. Folders and applications do not get removed at all.
This is what I put in my .bat script
forfiles -p "G:\" -s -m *.* /D -0 /C "cmd /c del #path"
How about just doing:
rd G:\. /S /Q
You could also pushd to the dir and then do the delete simply by using && operator to ensure the pushd command completes before executing the rd command.
pushd G:\ && rd . /S /Q
Panchu.
In CMD you should use RD (RMDIR) command to completely erase a folder and all files underneath it.
However, you will still need to delete the FIles themselves that are in the G:\ Drive since you can not delete the folder there.
This should do the needful:
#(SETLOCAL
ECHO OFF
SET "_Path=G:\"
)
REM Delete all Subdirectories and their File Contents
FOR /F "delims=:" %%_ IN ('
dir /B /A:D "%_Path%*" ') do (
RD /S /Q "%_Path%%%_\")
REM Delete all files in Root Folder:
DEL /F /Q "%_Path%*" & DEL /F /Q /A:H "%_Path%*"
As Mofi noted, Hidden Directories are not shown by default, so I either had to run two loops or use a For Loop, since he brought up a concern about directories with leading or trailing spaced I amended touse DIR instead and parse it with a FOR /F Loop, instead of a FOR /D However if you don't have those requirements, this is all fluff.
Note to delete files with trailing spaces DEL gets the job done.
Example of Trying to Delete Hidden/System/Read Only directories - these work ( so long as you are in an elevated command prompt of course)
C:\Admin>MD D:\Hidden
C:\Admin>MD D:\System
C:\Admin>MD D:\ReadOnly
C:\Admin>Attrib +H D:\Hidden
C:\Admin>Attrib +S D:\System
C:\Admin>Attrib +R D:\ReadOnly
C:\Admin>for /D %A IN (D:\*) DO #(ECHO.%A)
D:\DCIM
D:\temp
D:\srtFtpLogs
D:\srtFtpData
D:\Bkp
D:\System
D:\ReadOnly
C:\Admin>attrib D:\Hidden
H D:\Hidden
C:\Admin>attrib D:\System
S D:\System
C:\Admin>attrib D:\ReadOnly
R D:\ReadOnly
C:\Admin>RD /S /Q D:\ReadOnly
C:\Admin>RD /S /Q D:\System
C:\Admin>RD /S /Q D:\Hidden
C:\Admin>attrib D:\ReadOnly
File not found - D:\ReadOnly
C:\Admin>attrib D:\System
File not found - D:\System
C:\Admin>attrib D:\ReadOnly
File not found - D:\ReadOnly
I have made a batch file to robocopy /MOVE files with xxxx* in the file name to a temp folder, then a second command using forfiles to delete any files in the original directory older than -xx days old, then using the robocopy /move to move all the other files back to the original directory. Is there a way I can run the forfiles command to delete all files older than -xx days EXCEPT files with xxxx* in the name from the original director without moving files back and forth?
My original code is:
forfiles -p "%USERPROFILE%\Documents\Media\TV" -s -m . -d -45 -c "cmd /c del #path"
Here is one option using FORFILES with some added help of the FOR and FINDSTR commands. But I believe you could do this all with ROBOCOPY if you wanted to .
FORFILES /S /D -45 /C "cmd /q /c FOR /F %%G IN (#file) do echo %%~G|findstr /v /b xxxx >nul && del #path"
I'm looking for a way to delete all files in a directory that are older than x days and that are not of certain extension, I found this command line for the first:
forfiles -p "C:\Test" -s -m *.* /D -5 /C "cmd /c del #path"
and this for the second:
for /f %%F in ('dir /b /a-d ^| findstr /vile ".bat"') do del "%%F"
but could not do them in one command line, and for the second it ignores files containing spaces in their names.
Note that I am doing this in batch files (DeleteOldFiles.bat).
Thanks to everyone in advance.
The FORFILES command does have an option to see what the file extension is. So all you have to do is use that variable to compare it with the extension you are trying to exclude.
forfiles -p "C:\Test" -s -m *.* /D -5 /C "cmd /c if /I not #ext==0x22bat0x22 del #path"
The 0x22 is the hex representation of the double quote. Forfiles variables are always quoted.
When using a for loop with /f certain special characters are default delimeters. One of those being a space, so a name with a space will be split by space. So we need to ad "delims=" to override default delims.
I however suspect that you want to run both deletion of old files and excluding certain bat extensions purely because you batch file might be in the sam directory as the files you want to delete. You can overcome this simply by placing the file elsewhere and specifying path to the directory where files needs deleting. If so then simply add this to the batch file and place the file in a seperate dir:
forfiles -p "C:\Test" -s -m *.* /D -5 /C "cmd /c del #path"
If you want to delete all other files from a dir which containsa batch files and seperately delete files older than N date in another then these will do.
Batch file version:
#echo off
forfiles -p "C:\Test" -s -m *.* /D -5 /C "cmd /c del #path"
for /f "delims=" %%F in ('dir /b /a-d ^| findstr /vile ".bat"') do echo del "%%F"
pause
Cmdline version:
forfiles -p "C:\Test" -s -m *.* /D -5 /C "cmd /c del #path" & for /f "delims=" %%F in ('dir /b /a-d ^| findstr /vile ".bat"') do echo del "%%F"
Be careful though where you place the batch file as there is no specific path in the command for the for loop. For that very reason, I placed echo before del once you are sure that your query echo's the result you expect, you can remove the echo to perform the actual task.
I know we can write programs to do it.
I know we can write other scripts (perl/vbscript/ etc.) to do it.
I am looking for a command prompt/batch file solution to delete all folders matching sub_* (like sub_1, sub_2 ... ) to be deleted. rmdir or rd doesn't support wildcards, and I'm not able to figure out how to pipe the output of dir sub_*/ad command to delete command one by one as well. Any loop syntax etc. I can use?
for /d %x in (sub_*) do rd /s /q "%x"
You need to double the % if used in a batch file:
for /d %%x in (sub_*) do rd /s /q "%%x"
Untested, make sure to first use echo or something else that doesn't immediately wipe the directories ;)
forfiles /P C:\where\my\dirs\at /M sub_* /C "cmd /c if #isdir==TRUE rmdir /s /q #file"