Delete all files including files with spaces BATCH - batch-file

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.

Related

Batch Delete excluding files under specific folder (or file names with specific pattern /wildcard e.g. Test123.csv, Test623.csv, Test854.csv)

FORFILES /P "C:\Temp\" /D -3 /S /C "cmd /c if #isdir==FALSE del /F /Q #path"
above script is working fine and delete all files under Temp and its subdirectory older than 3 days.
I want to exclude all files from specific folder say all files from folder XYZ or full path-> C:\Temp\ABC\XYZ
Note : all files under XYZ folders are having pattern say Test*.*.csv
forfiles does not have an exclude option. You have to use something like findstr /V to exclude the results, but that will not form part of the forfiles command in itself. We simply incorporate a for loop and ecxlude using findstr /V, then delete:
#echo off
for /f "delims=" %%i in ('FORFILES /P "%temp%" /D -3 /S /C "cmd /c if #isdir==FALSE echo #path"^| findstr /VI "C:\Temp\ABC\XYZ"') do del /F /Q "%%~i"

Forfile command to delete all except a directory

I want to use the forfile command to delete all .frm files in some directories. But, I don't want to delete the *.frm files under a specific directory.
I have this, but i don't know how to put the name of the dir where i don't want to delete files:
forfiles /p D:\myfolder\ /s /m *.frm /c "cmd /c IF NOT DESIRE_DIR del #PATH"
Some help, please !
#for /f "delims=" %A in ('dir /ad /b^|findstr /l /v /i "Dirname1 Dirname2"') do #echo %A
This is also a lot quicker than forfiles. Forfiles is only really useful when listing files by date.
Put your folder names in Dirname1, Dirname2 to exclude them. Spaces mean or.
Remember in a batch file %A becomes %%A.
forfiles /p D:\myfolder\ /s /d -7 /m *.frm /c "cmd /c echo #path >> %temp%\temp.txt"
for /f "delims=" %A in ('findstr /i /v "list of excluded directories" ^< "%temp%\temp.txt"') do echo %A
Your /m was also in the wrong place and you would have seen a message telling you that.

spaces in for /f forfiles batch not working

I'm trying to make an automated old file deleting script, this is what I've come up with so far.
First I make an input file for user home dirs.
Command:
dir downloads /s /b echo > input.txt
It fills the .txt file like this:
F:\HomeDirs\a.Durge\Mijn Documenten\Downloads
F:\HomeDirs\a.eimers\Mijn Documenten\Downloads
F:\HomeDirs\a.eimers\system\Downloads
F:\HomeDirs\a.gacem\system\Downloads
After the input file has completed to fill up i want this as input to delete files older than 30 days in folders named downloads
FOR /f %%G in (input.txt) DO forfiles /p %%G /C "cmd /c del %%G\*.* /q" /D -30
This works to a certain extent, the filepaths that contain spaces aren't affected proccessed.
I tried adding quotes around the paths but I think I'm goofing up somewhere.
This is the error the batch gives me:
D:\Scripts>forfiles /p "F:\HomeDirs\a.Durge\Mijn /C "cmd /c del "F:\HomeDirs\a.Durge\Mijn\*.* /q" /D -30
ERROR: Invalid argument/option - 'F:\HomeDirs\a.Durge\Mijn\*.* /q'.
Type "FORFILES /?" for usage.
Filepaths that dont contain the spaces are processed properly.
How can I resolve this?
Your for /f command is using space as a delimiter (space and tab are the default delimiters).
So F:\HomeDirs\a.Durge\Mijn Documenten\Downloads will return two tokens.
F:\HomeDirs\a.Durge\Mijn
Documenten\Downloads
To stop this happening add "delims=" (use no delimiters at all) to your command:
FOR /f "delims=" %%G in (input.txt) DO forfiles /p %%G /C "cmd /c del %%G\*.* /q" /D -30
You can further simplify things by using a different type of for which loops against the output of another command (removing the need for an intermediate file), combining both commands in one line:
FOR /f "delims=" %%G in ('dir downloads /s /b') DO forfiles /p %%G /C "cmd /c del %%G\*.* /q" /D -30
See FOR /F Loop command: against the results of another command. for more information.
Put quotes around %%G.
FOR /f %%G in (input.txt) DO forfiles /p "%%G" /C "cmd /c del %%G\*.* /q" /D -30
Put "tokens=*" in the "for /f"
For /f "tokens=*"
So you get the whole line not just the first "token" up to the space

How to delete *.* excluding some extensions?

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.

Forfiles - spaces in folder path

I am running a batch file and I have one forfiles command in it
FORFILES -p%spinputarchrootpath% -m*.csv -d-365 -c"CMD /C DEL #FILE"
%spinputarchrootpath% variable maps to a folder location (Y:\Temp Documents\testfolder).
Now the above command is throwing an error because of the space in the folder name (Temp Documents).
How to handle this space? I have tried putting quotes around %spinputarchrootpath% variable but it is not working.
I'd the same problem and found the solution.
I think your folder-variable of the folder you wish to empty has a backslash at the end.
This will NOT work:
echo J|forfiles /P "C:\temp files\" /S /M * /D -7 /C "cmd /c del /F /S /Q #path"
... but this works (without backslash)
echo J|forfiles /P "C:\temp files" /S /M * /D -7 /C "cmd /c del /F /S /Q #path"
Regards
Tino
Enclose the path in quotes:
FORFILES -p "%spinputarchrootpath%" -m *.csv -d -365 -c "CMD /C DEL #FILE"
Note, there's a space between -p and "%spinputarchrootpath%". Without a space in this case it won't work.
As a work around first change directories to the folder you want, and then execute forfiles without the /p parameter.
CD %spinputarchrootpath%
FORFILES -m*.csv -d-365 -c"CMD /C DEL #FILE"
Check post:
How to Tell FORFILES to Execute Command on Path?
The problem lies in the part:
-c"CMD /C DEL #FILE"
Use:
-c"CMD /C DEL ^0x22#FILE^0x22"
to put extra double quotes around the file

Resources