Basically what I'm trying to do is create a batch file and place it in my startup that will use the modified date of the same batch file and see if it is greater or less than 90 days old. If it isn't, nothing happens and life goes on, but if it is, I want it to delete the contents of my downloads folder. I have a little bit of coding, but I've been testing it, but for some reason it isn't wanting to work for me. Any help would be greatly appreciated.
forfiles -p "C:\LOCATION OF .BAT\" -s -m rmdownload.bat /D -90 /C "cmd /c del C:\user folder\Downloads"
I'm just starting out using batch commands and would really love the help
This works for you if you are using Win2003 or WinXP and have forfiles.exe installed on your machine...
forfiles -p "C:\path_of_your_bat" -s -m rmdownload.bat -d -90 -c "cmd /c del C:\user folder\Downloads"
Later versions of Windows and Windows Server have it installed by default.
For Win7 or higher: Syntax has changed a little therefore the updated command is:
forfiles -p "C:\path_of_your_bat" -s -m rmdownload.bat /D -90 /C "cmd /c del C:\user folder\Downloads"
Assuming your target path contains spaces etc, then use this in your forfiles command. The 0x22 represents a double quote character.
If it echos the command then try it without the echo to actually perform the deletion
"cmd /c echo del 0x22C:\user folder\Downloads\*.*?0x22"
Related
I'm having trouble deleting the sub-directory, while I'm executing the following batch via cmd. The mentioned dir is getting deleted, and I'm getting the relevant log file of the deleted files, but the sub-dir isn't getting deleted. Since it ain't getting deleted, I don't have a proper log file.
Here is the code that I'm executing,
#echo off
SETLOCAL
:: set folder path
set dump_path=%1
:: set min age of files and folders to delete
set max_days=%2
shift
shift
:: remove files from %dump_path%
forfiles -p %dump_path% -m *.* -d -%max_days% -c "cmd /c del /q #path && echo #path>>logfile.log"
:: remove subdirectories from %dump_path%
forfiles -p %dump_path% -m *.* -d -%max_days% -c "cmd /c IF #isdir == TRUE rd /S /Q #path && echo #path>>logfile.log"
Any help is appreciated.
Below is the pic, in that further, I added a folder with some contents to check, but it ain't working.
More info, as it still ain't working, any way to change it into a PowerShell execution?
Below pic, before executing the cmd,
Post execution of the cmd,
Still, I'm unable to understand why it isn't removing the sub-dir files...This time, I removed the sub-dir cmd from my batch and added a '/S' to my first cmd.
There are two problems with your batchfile:
You mention you want to remove the files with are older than 15 days, but in your batchfile you remove the files which are more recent than 15 days (there's a minus in front of the %max_days%, not a plus, you need to change that).
You mention launching the batchfile, using 15>nul as a second parameter, which is wrong. It should simply be 15.
we have an active directory with about 80 users.
what i want to do now is place in each userfolder a script that the user can run for themselves and it makes a log file in that same folder with files older than 5 days.
The homefolder for each user is mapped as drive in windows to the letter h
right now i have this:
forfiles -p "%cd%" -s -m *.* /D -5 /C "cmd /c echo #path >> %cd%\log.txt"
but it throws me this error.
H:\>forfiles -p "H:\" -s -m *.* /D -5 /C "cmd /c echo #path >> H:\\log.txt"
ERROR: Invalid argument/option - '#path'.
is there any way to solve this?
if i run in on my local pc with a test folder it works fine.
When it comes to the closing doublequote, if you move it to before your redirection it certainly would make more sense, as would changing it to > instead of >>.
The fix to your issue, is when you use the /P option, you cannot doublequote it, and also include a trailing backslash, (it's one or the other).
My reading is that the backslash is escaping the doublequote.
Because drives tend to require a trailing backslash, all you have to do is remove the doublequotes around it, (they're not needed anyhow because drives don't include spaces).
Also please remember that, as already inferred in my comment, ForFiles already uses the current directory by default so removing it entirely is easier.
I cant get FORFILES to work for me. I run a .bat that backs up a few databases I have each day using this one example below.
COPY "X:\W Attendenance\C Crew Attendance\Attendance Control DB v4.xx.accdb" "X:\Supervisors Log\Backups\Attendance Bak\bak_%currdate%_Attendance Control DB v4.xx.accdb"
Then after running my backups I wanted to delete the old files that were older than five days by using the below example.
FORFILES /p "X:\Supervisors Log\Backups\Attendance Bak\" /d -5 /c "cmd /c del #path "
But what happens is it creates the backups but never deletes the old ones. What am I doing wrong?
Ken it still seems to not work. I changed the #file to #path and ran and I still have my backups. I also had another question, can I limit the delete to just files starting in bak_ ? and can I delete the file by its name because every backup starts with bak_ then the date mm-dd-yyyy_ then name of the file...
This fixed it... Thanks for your help and links!
forfiles -p "X:\Supervisors Log\Backups\Attendance Bak" -s -m *.* -d -1 -c "cmd /c del #path"
On a side note that only is able to delete the File but Sometimes I needed to delete a folder and everything in it so I use:
FORFILES /P "H:\BACKUPS\bk_Operator_Rate_Program" /d -10 /c "cmd /c IF #isdir == TRUE rd /S /Q #path"
FORFILES -pc:\tempfolder -s -d-6 -m* -c "CMD /C if #ISDIR==TRUE RD /S /Q #FILE"
is not working in batch file but it is working in command line
I am using older version of forfiles for windows xp. As mentioned above forfiles is working in command prompt, but when i copy the same command to batch file it's giving can't execute (error 2).
http://web.archive.org/web/20150527024532/http://www.sharedcache.com/cms/tips_and_tricks.aspx
Click Download
Drop Forfiles.exe C:\
C:\forfiles.exe -p "C:\Documents and Settings\test\My Documents\Downloads\Test file" -s -m . /C "cmd /c del #path" /d -14
Copy a old computer .exe file in the above location (don't drag it or you lose it forever.)
Change the "-14" to any days with a newer file in the same location, (make sure you copy it) run the batch file.
Now change "-14" to "-1", both files should be gone if you did it correctly.
Now set up a schedule to run it every whatever days for your heart contempts.
wrote this here bc I keep running in here from google and so many sites doesn't say you needed fortfiles.exe program itself for the cmd prompt from seeing any errors.
You can test the file your self and list the files wherever forfile.exe is located in. It is safe to run bc it doesn't have a cmd batch file to delete.
Enjoy!
ps. you can Drop the "forfiles" here "C:\WINDOWS\system32\"
without having to add "C:\forfiles.exe" in the cmd prompt and run it.
This question already has answers here:
Batch file to delete files older than N days
(25 answers)
Closed 4 years ago.
Can somebody tell me how I write a batch file that I can attach to a scheduled task that will delete IIS logs that are older than 2 weeks.
Use forFiles in a batch file:
forfiles -p C:\WINDOWS\system32\LogFiles\W3SVC1 -s -m *.log -d -14 -c "Cmd /C DEL #File"
Commandline params explained:
-d -14 // Specifies num days (14 for 2 weeks)
-p C:\WINDOWS\system32\LogFiles\W3SVC1 // path
-m *.log // file mask
You can find all in the technet link in my post.
You will need to install one the Win server resource kits such as Windows Server 2003 Resource Kit Tools
forfiles -p "C:\what\ever" -s -m *.* -d <number of days> -c "cmd /c del #path"
Syntax
FORFILES [/p Path] [/m Mask] [/s] [/c Command] [/d [+ | -] {dd/MM/yyyy | dd}]
Key
/p Path The Path to search (default=current folder)
/s Recurse into sub-folders
/C command The command to execute for each file.
Wrap the command string in double quotes.
Default = "cmd /c echo #file"
The Command variables listed below can also be used in the
command string.
/D date Select files with a last modified date greater than or
You could use the script deleteoldfiles.bat as an example.
(From "How to Delete Old IIS Log Files" blog post)
The following example deletes all files named “ex*.log” that were last modified over 1 year (365 days) ago. The starting directory is C:\Windows\System32\LogFiles:
deleteoldfiles.bat C:\Windows\System32\LogFiles ex*.log 365
The following example searches all subdirectories in C:\Windows and deletes all files named “dump.txt” that were last modified over 60 days ago:
deleteoldfiles.bat C:\Windows dump.txt 60
From there, you can schedule that batch, making sure of the arguments and their enclosing quotes aren't an issue.
See for instance "How to use the Windows Task Scheduler".