Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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"
share|improve this question
1  
doesn't this work: forefiles /M *.txt /D -7 /C WinRAR a -r aa.zip @file ? – harper Feb 27 '13 at 19:27
    
Yes that works and thank you. Can you tell me how can i change the following script so that it only compresses files that are older than 7 days. FOR %%A IN (D:\Agentrics\integration\incoming\Mansoor\backup*.txt*, D:\Agentrics\integration\incoming\Mansoor\backup*.cpi*) DO "C:\Program Files\WinRAR\WinRAR.exe" a -r D:\Agentrics\integration\incoming\Mansoor\backup\"%%~nA.zip" "%%A" Regards, Mansoor – Mansoor Ahmad Feb 28 '13 at 16:23

If I were you, I would not use for cycle for this. I would prefer the way proposed by @harper But still, here is the way you wanted it:

REM move files older than 2 days to an archive directory
forfiles /P D:\Agentrics\integration\incoming\Jason /M *.txt /S /D -2 /C "cmd /c move @file D:\Agentrics\integration\incoming\Jason\archive"
forfiles /P D:\Agentrics\integration\incoming\Jason /M *.cpi /S /D -2 /C "cmd /c move @file D:\Agentrics\integration\incoming\Jason\archive"
REM zip all files in the backup directory
for /f "tokens=*" %%a in ('forfiles /p D:\Agentrics\integration\incoming\Jason\archive /s /d -7') do "C:\Program Files\WinRAR\WinRAR.exe" a -r "%%~nA.zip" "%%a"
REM Delete all files in the backup directory that are older than 6 months
for /f "tokens=*" %%a in ('forfiles /p D:\Agentrics\integration\incoming\Jason\archive /s /d -183') do del D:\Agentrics\integration\incoming\Jason\archive\%%a /y
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.