I found a sample batch file I am trying to use to delete files after they are 5 days old. I've been testing it using the "echo" command and it works. The only problem I am having is that I want it to delete every file inside of the folder but not the folder.
forfiles -p "E:\Programs\SickBeard\SickBeard\lib" -s -m *.* -d -5 -c "cmd /c echo #file"
I've tried changing the path to "...\lib\" and I've tried changing #file to #path but none of these seem to work.
How can I specify all files inside the folder to be deleted but not the folder itself? I am going to be using task scheduler to run this file once a day. Thank you in advance.
EDIT: Oh and for the record, that is just a test folder. Also, will this work on Windows 8.1? I am currently using Windows 7 and it seems to be working fine, but I will be using this on a new Windows 8.1 build.
By default forfiles command deals only with the files and not with folders. So your command is absolutely fine (should be on Win8 too..).
If you want to use the forfiles command for deleting the folders, below is one such example.
forfiles -p "C:\Sample" -d -5 -c "cmd /c IF #isdir == TRUE rd /S /Q #path"
Cheers, G
Here you go. Delete those files you want and avoid the pesky No files found "error" and the corresponding errorlevel of 1--while retaining other more valuable errors from the FORFILES command:
cmd /e:on /c "forfiles /p "<insert your directory here>" /s /m *.* /d -5 /c "cmd /c del #path" 2>&1 | findstr /V /O /C:"ERROR: No files found with the specified search criteria."2>&1 | findstr ERROR&&EXIT 1||EXIT 0"&EXIT %errorlevel%
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.
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've created a simple batch file to delete files over 14 days which is a simple command as most of you probably know so it's doing the below
forfiles /p "C:\%userprofile%\Downloads" /s /m *.* /c "cmd /c Del #path" /d -14
but I keep receiving could not find C:\User\%userprofile%\downloads\desktop.ini
So I assume it's searching for the desktop.ini file but I have all folders and files unhidden. Is there a way to prevent it looking for that file and just doing as a I ask it?
Any help would be appreciated.
As Mike Nakis suggests, del is probably failing on desktop.ini because that file is typically set +ash (archive, system, and hidden). The easiest solution would be just to ignore it. It's harmless anyway.
forfiles /p "%userprofile%\Downloads" /s /m *.* /c "cmd /c Del #path 2>NUL" /d -14
If it really bothers you and you insist on deleting it, then remove the system attribute.
attrib -r -s -h -a "%userprofile%\Downloads\*"
forfiles /p "%userprofile%\Downloads" /s /m *.* /c "cmd /c del #path" /d -14
... but it'll probably just get re-created eventually anyway. I'd just ignore it.
What do you mean when you say that you have all folders and files unhidden? You have probably instructed the windows explorer to also show hidden files, but that does not mean that the files are not hidden.
You have two options: for each file that you are about to delete, either use the attrib command to make sure it is not hidden prior to deleting it, or play with the /A option of the del command to make it delete everything, even hidden files.
So what I want to do is use the batch script to delete off files on my data directory on my server(I have already created a archive copy just need to delete the files now). Problem is we use roaming profiles and if I ran this on the data directory it would go though and delete all kinds of ini's and application files that are needed but not changed very often.
forfiles -s -m *.* /D -01/01/2014 /C "cmd /c del #path"
If possible I would like to run a script like this but exclude any folder that has profile or profile.V2 in it and anything under it.
My directory structure looks something like this.
C:/Data/Users/srodgers/profile/Desktop
-or-
C:/Data/Users/ssmith/profile.V2/Desktop
and I would like to run the script from the root of data
Test this and remove the echo (it currently just displays the del commands) to acually delete the files.
In limited testing it's working fine.
#echo off
forfiles -s -m *.* /D -01/01/2014 /C "cmd /c echo #path |find /i /v 0x22\profile\0x22|find /i /v 0x22\profile.V2\0x22 >nul && echo del #path"
pause
I am trying to use forfiles to delete files that are older than 7 days. The files are in a UNC path. Below is the script that I am using.
Forfiles -p \\devexpress\C$\FULL\ -s -m *.* -d -7 -c "cmd /c del /q #path"
But I get an error mentioning that UNC paths (\\machine\share) are not supported.
There appears to be workarounds available but cannot get a clear answer googling.
Enhanced solution to the PA's first answer is:
PushD "\\devexpress\C$\FULL\" &&(
forfiles -s -m *.* -d -7 -c "cmd /c del /q #path"
) & PopD
The PushD command maps the UNC path to free drive letter automatically, so this is portable approach.
Found in http://www.petri.co.il/forums/showthread.php?t=24241.
The error I get when trying to reproduce the problem says that the problem is not with FORFILES not suporting UNC Path, but with CMD not being able to start with an UNC path as default directory. In case that this is also your problem, there are three approaches to solve it.
you might assign the UNC path to a disk letter, via NET USE
NET USE V: \\devexpress\C$
Forfiles -p V:\FULL\ -s -m *.* -d -7 -c "cmd /c del /q #path"
You may bypass CMD and directly use some ERASEFILE executable utility directly in the -C option of the FORFILES
You may bypass FORFILES and use FOR command with some date checking logic instead. See my answer to this Stack overflow question How can I check the time stamp creation of a file in a Windows batch script?
I got this to work:
PushD "\\DS\Tajana\Arhiva\Arhive po danima" &&("forfiles.exe" /s /m "*.*" /d -7 /c "cmd /c del #path") & PopD
although I get a message about the error in cmd window "not supporting UNC Path" but it still deletes files older than 7 days