forfiles error while deleting folders - windows

I am trying to write a script that deletes items in the TEMP folder(s) in Windows 7. I only want it to delete files that are 30 days or older. I'm doing testing in a folder which I have set up in the system's environmental variables as TESTTEMP.
I have the script as follows:
forfiles /p %TESTTEMP% /s /d -30 /c "cmd /c IF #ISDIR==FALSE del #FILE /q"
forfiles /p %TESTTEMP% /s /c "cmd /c IF #ISDIR==TRUE rmdir #FILE"
My logic behind this is that the process should first delete all files within the TESTTEMP directory if the file is older than 30 days, and to check in all the subdirectories. Then I check through the remaining files and if it is an empty directory, remove it.
This script works perfectly - all the files I want to delete are deleted, and those that are supposed to remain, remain. However, I noticed that when I run this batch file, I get the error The system cannot find the file specified. I believe it's having some problem with the rmdir command and not being able to find the directory it just deleted...
Is this something I should be worried about, since the script appears to do what I want it to do?
Better yet, is there a way to display which file is not being found so I can try to figure out what's happening on my own?
Thanks for any help!
(For reference, here is the folder structure before and after the batch file is run, assuming all files are older than 30 days:)
Before:
-TestTemp
-More Test
testfile1.txt
testfile2.txt
testfile3.txt
testfile1.txt
testfile2.txt
testfile3.txt
After:
-TestTemp

You can display files and folders:
forfiles /p "%TESTTEMP%" /s /c "cmd /c IF #ISDIR==TRUE echo rmdir #FILE"
forfiles /p "%TESTTEMP%" /s /d -30 /c "cmd /c IF #ISDIR==FALSE echo del #FILE /q"

If a folder is not empty then it will return a harmless error message. The 2>nul will remove the error message.
forfiles /p %TESTTEMP% /s /c "cmd /c IF #ISDIR==TRUE rmdir #FILE 2>nul"

I was getting this "The system cannot find the file specified" error too, but it went away when I removed the "/s" from the ForFiles call. I did not actually need the /s, but it looks like the poster here did. If you need the recursive delete and can't live with the error (or don't want to swallow it with 2>nul), maybe you could nest a non-recursive ForFiles within a recursive one? Just a thought.

Related

Delete a folder with file xy.z that is older then X days

I want to delete all folders with an old file.
I try to solve it with forfiles to find my old files and remove the whole folder.
The problem i get is, when i found the file forfiles is in the folder what i want to delete and i get the "acess denied, is being used by another process" error.
forfiles /p "C:\Users\abc\Desktop\tmp\test" /s /m xy.z /d -1 /C "cmd /V:on /C if #isdir==FALSE set tmpPath=#path&set tmpPath=!tmpPath:xy.z=!&cmd /c rd /s /q %tmpPath% "

Script for deleting old long-ago opened files

I want to delete files that were not opened for more than 60 days, but they may have been created long before that. I opened the information about last access with fsutil, but I don't know which parameter should I use in script.
I already know how to delete old files and I just need to change /d to "data of last access". Here is a standard script for deleting:
forfiles /p "C:\Users\...\" /s /m *.* /c "cmd /c Del #path" /d -30

Why is Forfiles recursing even though I'm not using the /S parameter?

In my Windows batch file on Windows Server 2008 R2 Standard, I'm trying to use the Forfiles command without recursing through my files, but it still recurses even though I am not using the /S parameter.
How can I get it to stop recursing
I've tried it with both #path and #file and even with the /s. When I use the /S, the amount of recursion is ridiculous!
ForFiles /p "C:\temp" /d -30 /c "cmd /c dir #path" >temp.txt
In the code above, I expect the temp.txt to only show files in the specified folder, not any of it's subfolders.
Let's consider what you are asking the system to do:
Forfiles /p "C:\temp" /d -30
The above command does pretty much what you intended, find any file/folder older than 30 days.
/c "cmd /c dir #path"
This does not do what you think. You are effectively asking cmd to dir each match found by forfiles and do the full dir of each #path, including folders. So let's say you have a dir older than 30 days:
C:\Temp\oldInstalldir\
You are telling cmd.exe to do:
dir "C:\Temp\oldinstalldir"
as well as dir for each file in c:\temp again. So what you really want is to list the files you found older than 30 days which forfiles already found for you, so a working solution is to just echo them.
ForFiles /p "C:\temp" /d -30 /c "cmd /c echo #path">temp.txt
Or by filename only (no path):
ForFiles /p "C:\temp" /d -30 /c "cmd /c echo #file">temp.txt
I have a temporary solution you could try. It is kinda hacky but it might work for what you are trying to do.
Add /M "\*.\*" to your command and it will skip the directories unless they have . in the name which most usually do not. I didn't see it recursing the subdirectories but there was no way to mask the directories with that command. I tried a few attempts using
dir /b /a-d
This gets a list of all the not directories but I couldn't pipe the rusults into the forfiles command to get it to use that as the input to do the date masking. Sorry for the
partial solution.

Old folders automatic deletion in Windows

I have a folder named backup in D: drive, under that folder there is a daily backup getting created for a day with folder names as the date of the backup.
I only want to keep the backup data for yesterday, (i.e. the latest one) and delete the old ones on daily basis automatically.
The problem I'm facing is, the data under the latest folder also is getting deleted.
Could you please help me with a script which only removes the old folders except the one recently modified or modified yesterday, in such a way that it doesn't delete any data from the recent folder.
Here is the code I am using:
forfiles -p "D:Test" -s -m *.* /D -1 /C "cmd /c Del #path"
Check if file is directory and do remove dir rd if is older than 100 days.
forfiles /S /D -100 /C "cmd /c IF #isdir == TRUE rd /S /Q #path"
ForFiles /P "D:Test" /D -1 /C "CMD /C if #ISDIR==TRUE echo RD /Q #FILE &RD /Q /S #FILE"
The above script worked fine.

Delete all files in directory except in one subdirectory

I have this script:
forfiles /p "C:\Hello" /m *.* /s /d -5 /c "cmd /c if /i not #path==C:\Hello\Dontdelete del #file"
I'm trying to delete every file older than 5 days in C:\Hello except for files that are in C:\Hello\Dontdelete or any directory within this path.
Currently everything is getting deleted when I use the above script.
Thanks!
The fundamental problem is that #path is the full pathname - including the filename+extension, and quoted and #file is the filename+extension, also quoted. In consequence, the if statement as constructed can't be manipulated to work as anticipated.
Here's a way to get it working (I've made a few changes to suit my system which should be easy to manipulate to suit yours)
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
forfiles /p "%sourcedir%" /m *.* /s /d -6 /c "cmd /c call %cd%\selectdel #PATH"
GOTO :EOF
I set up selectdel as a batch in my current directory. No doubt it would be fine on the path, or simply forced as I have with the %cd%\.
selectdel.bat
#echo off
setlocal
if /i not "%~dp1"=="%sourcedir%\Dontdelete\" echo DEL %1
GOTO :eof
Obviously, I've simply echoed the del command - in case of accidents...
I do not know much about it, but I think you should make a lopping for every 5 days, the script did a check to see if there are files in the folder C :/ Hello, if yes, the script clears, if not, it does nothing. Search about it.

Resources