Delete files older than x while ignoring subfolders - windows

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.

Related

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.

Need a windows batch script that will zip files by name AND date

I am in the process of editing a little script utility on Windows 2012 that will recursively go through a directory, look at each file and zip it up based in it's age (over 30 days) but ignore any files with the .zip extension.
I can get the script to do either zip files by age or ignore the .zip extension but not both.
Here is the script I am working with
forfiles /P G:\temp\admin\archives /s /m *.* /D -365 /C "cmd /c for /r %f in (*.*) do if not %~xf==.zip C:\progra~1\7-Zip\7z a -sdel %f.zip %f"
Feedback appreciated.
Here is a possible solution:
forfiles /P G:\temp\admin\archives /s /m *.* /D -30 /C "cmd /c if not #ext=="zip" C:\progra~1\7-Zip\7z a -sdel #fname.zip #path"

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.

forfiles error while deleting folders

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.

Resources