Tuesday, March 24, 2015

robocopy - Batch file to move/compress/delete files

I am trying to write a batch script that will run automatically daily to do the following:




  1. Move files older than 2 days to from the main directory (Jason) to an archive directory.

  2. Zip files in the archive directory that are older than 1 week and delete files from this directory that are older than 6 months.

  3. I want to run this script from a different directory (not the directory that has the files).



I wrote the following script but it’s not working correctly.





REM move files older than 2 days to an archive directory
robocopy D:\Agentrics\integration\incoming\Jason D:\Agentrics\integration\incoming\Jason\archive /MOV /MINAGE:2


Questions:




  1. How can I change the command below to zip files older than 1 week?

  2. Is it possible that the zipped files can have the same creation date and time as the original files?





REM zip all files in the backup directory
FOR %%A IN (*.TXT*, *.cpi*) DO "C:\Program Files\WinRAR\WinRAR.exe" a -r "%%~nA.zip" "%%A"
FOR %%A IN (*.TXT, *.cpi) DO DEL "D:\Agentrics\integration\incoming\Jason\archive\.cpi*" "%%A"

REM Delete all files in the backup directory that are older than 6 months
forfiles /p D:\Agentrics\integration\incoming\Jason\archive /s /m *.* /d -500 /c "cmd /c del /q @path"

No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...