Writing a Batch File to delete all files [duplicate] - batch-file

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".

Related

unable to delete the directory while executing a batch via cmd

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.

Batch Script to delete files past 5 days old

I have a batch script that I have run through Task Scheduler every night at midnight. Here is the script:
forfiles /M *.bak /p "Z:\Logs" /S /D -5 /C "cmd /c del #file : date >= 5 days >NUL"
But when the task runs at midnight, it does not delete the files older than 5 days. If I double click on the batch file and run it manually, it does delete the files older than 5 days. What is wrong or do I need to do something different to make this work?
EDIT:
Here is my full batch file and more information about the task schedule:
sqlcmd -S server\SQLEXPRESS -U user -P password -i "D:\BackupPrograms\translogsbackup.sql"
forfiles /M *.trn /p "Z:\Logs" /S /D -5 /C "cmd /c del #file >NUL"
I am using an administrator account for the task schedule to run every night. I am trying to get it to delete the older backups that the sqlcmd is creating, that way I make sure I am not wasting a bunch of space on Full SQL backups that are not needed. I hope this helps more. I am just confused why the batch file would act differently running through the Task Scheduler and when I double click on it to run.
It doesn't work from the command prompt any more than it does from the scheduler, and here's why.
/C "cmd /c del #file : date >= 5 days>NUL"
The : is illegal at that position in a command line, and it's ignored.
The >= is interpreted as the output redirection symbol, and therefore all of the output is redirected to a file named 5 in the current directory.
You can test this at a command prompt yourself:
Create a new, empty folder on your system, such as C:\Test, from a command prompt, and make it the active directory.
C:\>md Test
C:\>cd Test
Create a couple of dummy files in the folder:
C:\Test>echo file1 > file1.txt
C:\Test>
C:\Test>echo file2 > file2.txt
Do a directory to see what's there:
C:\Test>dir /b
file1.txt
file2.txt
C:\Test>
Try this forfiles command to see the output:
C:\Test>forfiles /M *.txt /C "cmd /c echo #file"
"file1.txt"
"file2.txt"
C:\Test>
Change the forfiles to add the : date >= 5 days and run again:
C:\Test>forfiles /M *.txt /C "cmd /c echo #file : date >= 5 days"
C:\Test>
Do a directory to see what's there:
C:\Test>dir /b
5
file1.txt
file2.txt
C:\Test>
Note the new file with the name 5.
So the solution: Delete the : date >= 5 days. You can leave the NUL portion, as that legitimately redirects any output to NUL (nothing) so that it's not displayed. So your command would look like this:
forfiles /M *.bak /p "Z:\Logs" /S /D -5 /C "cmd /c del #file >NUL"

If Batch File Modified Date is older than 90 days, delete files

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"

Command to delete a file which is two days older [duplicate]

This question already has answers here:
Batch file to delete files older than N days
(25 answers)
Closed 9 years ago.
what is the CMD command to delete the files which is two days older from a particular folder when we run a batch file?
You could use FORFILES command together with DEL: FORFILES has an option to filter files older than x days and than execute another command on filtered files.
forfiles /P "path you want to search files" /S /D -2 /C "cmd /c del #file"
FORFILES is a native command under Vista/Windows7/2008, you can get it from the Resource Kit for Windows XP. Note that the syntax may change a little depending on version of FORFILES you're using.
You could use the fileDate variable, and compare it to the current date, then if it is 2 days or older, you run the eraser over it?

Delete files with batch job

I want to delete .txt extention files in my computer every 30 days using batch script...
files name are like:
filename_20082013.txt
filename_21082013.txt
filename_22082013.txt
filename_23082013.txt
For Windows:
forfiles -p "C:\what\ever" -s -m *.* -d <number of days> -c "cmd /c del #path"
So:
forfiles -p "C:\your\path" -s -m *.txt -d -30 -c "cmd /c del #path"
Source: Batch file to delete files older than N days
This will delete files that are at least 30 days old (you can change it to any number)...now you need to schedule this task to run every 30 days:
You can use Windows Task Scheduler:
http://windows.microsoft.com/en-us/windows7/schedule-a-task
Or you can use StreamServe Task Scheduler:
http://streamshare.streamserve.com/Articles/Article/?articleId=424
suggestion for approx. 30 days, run the command on the first day of the month:
del *072013.txt
for this month and
del *082013.txt
for the next month.
The command also works in a batch script.
You are looking for the "Task Scheduler" on Windows, or a cron job on Unix.

Resources