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
Related
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% "
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.
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.
I've found several posts that are close to my question, but wanted to run it by you guys just to be 100% clear.
I have a folder: B:/Backups it has two subfolders B:/Backups/Cust1 and B:/Backups/Cust2. B:/Backups also contains many .bak files.
I'd like to delete the .bak files older than X`` days from B:/Backups while completely ignoring those subfolders AND their contents.
I prefer a batch file/robocopy if possible.
Can anyone help me get started? Here is what I have tried:
forfiles /p b:/Backups\ /s /m *.bak /d -7 /c "cmd /c DEL #file"
forfiles -p "b:\Backups\" -s -m *.bak /D 7 /C "cmd /c del #file"
This will delete .bak files older than 7 days in the specified folder, while completely ignoring subfolders.
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.