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.
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% "
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
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 already have a script (see below) to recursively delete files with a specific extensions and older than x number of days.
I'm looking for the command to recursively delete empty folders where the name begins with { and ends with } and is older than x number of days. Any ideas?
set deletepath="C:\test"
set days=10
for %G in (.log, .dat, .sts, .mdn, .req, .err, .edi, .xml.filename, .xml) do FORFILES /P %deletepath% /S /M *%G /D -%days% /C "cmd /c del #path"
This should work. Test it on sample folders first.
FORFILES /P %deletepath% /S /M "{*}" /D -%days% /C "cmd /c if #isdir==TRUE rd #path 2>nul"
It will only remove the folder if it is empty - the 2>nul removes harmless error messages when there are files inside the folder.
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.