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

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?

Related

what is windows equivalent of Touch command to modify timestamp of files in some directory, how to achieve by windows batch built-in commands [duplicate]

This question already has answers here:
Touch file to update the date/time of file? (Modified Date) (WindowsXP BatchScript)
(3 answers)
Closed 2 years ago.
I want to develop batch having step that it will update timestamps of all files within some directory.
Below command gives list of all files in directories.
Can we redirect output of this command to some other windows equivalent touch command, so as to update the timestamps for all files?
cmd>dir /B
Log_adminUser_1.log
Log_adminUser_2.log
Log_adminUser_3.log
Log_adminUser_4.log
Log_adminUser_5.log
Log_adminUser_6.log
As a trick for touching all files in a directory, you could use this:
#ECHO OFF
FOR /F "delims=" %%G IN ('DIR /B') DO (
COPY /B "%%G"+,, "%%G" > NUL
)
The COPY /B construct is documented in TOUCH on SS64, which also explains some caveats with it.

CMD Line - Help adding time to my filename [duplicate]

This question already has answers here:
How do I get current date/time on the Windows command line in a suitable format for usage in a file/folder name?
(30 answers)
Format date and time in a Windows batch script
(37 answers)
Time is set incorrectly after midnight
(4 answers)
Closed 2 years ago.
Can someone please help me figure out the syntax to add the time to an output file's name? I was able to get the date, but the time isn't working for me. I keep googling it but so far I have come up empty. I have figured out how to add it to the file itself, but I want to be able to see it in the name.
The code below, is just a super simple robocopy command that I have saved as test.cmd.
Everything works, everything except adding the time.
Feel free to steal it if you want. Just make sure you Google the switches so that you know what you're doing. That part is easy to find.
My Code
#ECHO OFF
if not exist "C:\Example" mkdir "C:\Example"
ROBOCOPY.EXE C:\Example Source "\\192.168.0.100\Shared Storage" /E /Z /J /NOOFFLOAD /R:2 /W:1 /REG /UNILOG+:"C:\Example Source\%date:~-10,2%-%date:~-7,2%-%date:~-4,4% Time.txt" /MT:4
exit
I have figured it out!
Here it is if someone else needs it.
#ECHO OFF
if not exist "C:\Example" mkdir "C:\Example"
ROBOCOPY.EXE C:\Example Source "\192.168.0.100\Shared Storage" /E /Z /J /NOOFFLOAD /R:2 /W:1 /REG /UNILOG+:"C:\Example Source\date %date:~-10,2%-%date:~-7,2%-%date:~-4,4% time %time:~0,2%.%time:~3,2%.txt" /MT:4
exit

ForFiles Newer Than Batch

I'm working with the forfiles command
I am attempting to get a list of files newer than X days ago.
When I use the /D +X command I get a not files found error:
forfiles /P %filename% /D +3
However if I use a date it works fine:
forfiles /P %filename% /D +06/29/2016
Am I missing something on the +X formatting? It seems like it is adding 3 days to the current date and then looking for files newer than that.
Or is there a simpler way to increment the date than I'm aware of. Only work with Batch once in a while
I don't want to use vbs, and I would rather not have a set of code longer than the rest of my script just to get the date, there should be a simpler way.

copy files by date to another folder

I want to be able to create a batch files that copies files by date only up to 3 or 4 days but use the %edate% %user%\ syntax instead of specifying a date. Here is what I have so far:
XCOPY %user%\folder_1 %edate% %user%\folder_2.
I am unable to download any 3 party software. I can only use the .bat format.
The comment suggestion to use forfiles is a good one, but I note the question has remained open. Perhaps more details are required.
If the questioner has a system more recent than XP (and I assume so, as XP and previous are now deprecated) the ROBOCOPY also has appropriate options. I'll give examples of both for files greater than 4 days old.
Using forfiles:
FORFILES -P %user%\folder_1 /D - 4 /C "CMD /C XCOPY #file %user\folder_2"
Using ROBOCOPY:
ROBOCOPY %user%\folder_1 %user%\folder_2 /MINAGE:4
I hope that is sufficient detail....

Writing a Batch File to delete all files [duplicate]

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

Resources