• 0

batch file to move-compress-delet3 files


Question

Hello,

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 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 its not correctly working. Can you please help.

REM move files older than 2 days to a archive directory

robocopy D:\Agentrics\integration\incoming\Jason D:\Agentrics\integration\incoming\Jason\archive /MOV

/MINAGE:2

Question: How can i change the command below to zip files older than 1 week. Also i want to put this

command in a batch file that will run from a different location? 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"

Kind Regards,

Jason

9 answers to this question

Recommended Posts

  • 0

How can i change this script so that it only compresses the 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"

FOR %%A IN (D:\Agentrics\integration\incoming\Mansoor\backup\*.TXT,D:\Agentrics\integration\incoming\Mansoor\backup\*.cpi) DO DEL "D:\Agentrics\integration\incoming\Mansoor\backup\.cpi*" "%%A"

Regards,

Jason

  • 0

Although its not exactly what you're looking for, this article describes the creation a DIY backup system. Since it discusses many of the issues and their possible solutions in-depth, it might help. Good luck.

  • 0

You could let RAR handle that using -to7d (meaning exactly what you want - time older than 7 days) switch. If no file(s) being processed qualify, no archive is created (and exit code 10 is returned, should you want to debug with %errorlevel%).

As for the whole backup system - surely there must be a better way.

  • 0

Thank you guys for your help. Howerver I have a question for Phouchg. You said that i can use the -to7d option in my command. I tried this option but getting errors. Can you please take a look at the command below, i added -to7d in this command.

FOR %%A IN (D:\Agentrics\integration\incoming\Mansoor\backup\*.txt*, D:\Agentrics\integration\incoming\Mansoor\backup\*.cpi*) DO -to7d "C:\Program Files\WinRAR\WinRAR.exe" a -r D:\Agentrics\integration\incoming\Mansoor\backup\"%%~nA.zip" "%%A"

kind Regards,

Jason

  • 0

looks like to me, you should include the a and -r functions in quotes too...so it would be "C:\Program Files\WinRAR\Winrar.exe a -r"....but I could be wrong....I'm a little rusty.

  • 0

It's a switch for WinRAR, not for CMD ;)

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 -to7d D:\Agentrics\integration\incoming\Mansoor\backup\"%%~nA.zip" "%%A"

See WinRAR.chm help file in your C:\Program Files\WinRAR\ folder. Command line syntax is explained in detail. There's overwhelming amount of stuff, mostly useful.

Though I don't think either CMD or RAR can change creation time to an arbitrary value (-tl switch preserves modification time only). Powershell might.

  • 0

Thanks phouch for sharing your knowledge. I looked at WinRAR.chm help and it really helped. Also I added -to7d and worked just fine.I have one more question. Is it possible to create the zip files with the same file creation date as the origininal files (non-zipped files)? I want to delete the zipped files that are older than 6 months from the archieve directory but after those files are zipped they show the current date and time. For example if i zip a file that was created 6 months ago i want the zipped file creation date date also be six months? Is it possible to do that?

Regards,

Jason

  • 0
  On 04/03/2013 at 06:12, jmander said:

Thanks phouch for sharing your knowledge. I looked at WinRAR.chm help and it really helped. Also I added -to7d and worked just fine.I have one more question. Is it possible to create the zip files with the same file creation date as the origininal files (non-zipped files)? I want to delete the zipped files that are older than 6 months from the archieve directory but after those files are zipped they show the current date and time. For example if i zip a file that was created 6 months ago i want the zipped file creation date date also be six months? Is it possible to do that?

Regards,

Jason

1. Surely you should be deleting the files before compressing them - why go to the effort of compressing them just to delete them?

2. You can alter file creation dates - it's a bit messy and not-advised, especially on compressed files (zip,rar,etc) as it may cause corruption.

3. You could always create the compressed file with the original date in the filename (myfile01.txt -> 20120328_myfile01.rar).

  • 0

Here is my script. Can someone answer my two questions please?

I have a batch script that does the following @ECHO OFF

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

REM Zip files in the Archieve directory that are older than one week FOR %%A IN (D:\Agentrics\integration\download\archive*.txt*, D:\Agentrics\integration\download\archive*.cpi*) DO "C:\Program Files\WinRAR\WinRAR.exe" a -r -to7d D:\Agentrics\integration\download\archive\"%%~nA.zip" "%%A"

REM Delete Original files after they are zipped forfiles /p D:\Agentrics\integration\download\archive /s /m .txt /d -7 /c "cmd /c del /q @path" forfiles /p D:\Agentrics\integration\download\archive /s /m .cpi /d -7 /c "cmd /c del /q @path"

REM Delete files that are older than 6 months from the archive directory forfiles /p D:\Agentrics\integration\download\archive /s /m .zip /d -180 /c "cmd /c del /q @path" pause

Question 1: When i run the script i get WinRAR diagnostic messages for some files. For example if there are files in the incoming directory that are not older than two days i get this message."WinRAR Diagnostic messages: No File To Add". Because of this message the scripts stops until i click on the close button of the dialogue box. I am using the free version of WinRAR and its not expired

Question 2: I have two seprate command in the script above. One is for zipping the files older than a week and the other one is deleting the original files after they are zipped. How can i link those two commands so that if some reason the files did not get zipped they should also not get deleted. Or is is there a command to break the script if the files did not get zipped? I just want to zipp the files first and then delete the original ones.

Thanks,

Jason

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Microsoft isn't happy you're using unsupported Exchange versions, announces final deadline by Usama Jawad Earlier this month, Microsoft announced Exchange Server Subscription Edition (SE), which is the official transition of the product to the Modern Lifecycle Policy, where software is continuously serviced without an end-of-life date, as long as you keep it updated. It also revealed surprising, but brief, Extended Security Updates (ESUs) for Exchange 2016 and 2019. As it winds down support for these products, the company has expressed some displeasure that some customers are using even older and, obviously, unsupported versions of Exchange. In a blog post, the company has noted that it currently offers migration tools that enable the migration of public folders from on-premise Exchange 2013 or older versions to Exchange Online. This is by design, but Microsoft is now changing its tune on the topic. Starting from October 1, 2025, customers leveraging Exchange 2010 or older versions of the software will not be allowed to use Microsoft's tools to migrate their public folders to Exchange Online. Microsoft believes that this deprecation will reduce reliance on legacy systems and enhance "long-term service reliability". Any migrations that are attempted after the aforementioned date will fail, so Microsoft has urged customers to complete their migrations as soon as possible. If customers want to move their data to Exchange Online after October 1, they will first have to upgrade to a newer Exchange version, which is Exchange 2013, but it is important to keep in mind that supported versions are 2016 and 2019. Microsoft has emphasized in a rather stern tone that it does not encourage using unsupported versions of Exchange Server at all, and it has just put out this advisory because it is aware that public folder migrations from legacy systems are currently active, even though they shouldn't be. Needless to say, customers should upgrade to Exchange Server 2016 or 2019 as quickly as possible, but ideally, they should consider moving to Exchange Server SE at this point, considering that the other two versions are running out of support soon, too.
    • A little bit, yeah, if you ask me. Granted, he has the right to be upset with this jerk user that attacked him, but why drop the entire project just because of ONE person? Seems a little exaggerated.
    • Xbox July Update brings PC app cloud upgrades and Rewards support by Pulasthi Ariyasinghe The Xbox team at Microsoft has another major series of updates hitting its platforms. The Xbox July Update is primarily bringing new features to the PC application, while cloud gaming services are also being upgraded. A lot of these additions were a part of Insider testing sessions previously, but now they are ready for prime time. First off, Game Pass Ultimate members can now stream supported games over the cloud, as long as they own a copy on the Xbox store. The Stream Your Own Game feature can be accessed via the Cloud Gaming section on the Xbox PC app. The feature now boasts over 250 supported games too, with recent additions including classic Assassin's Creed titles, LEGO games, and the Saints Row series. Upcoming games to the lineup include RoboCop: Rogue City – Unfinished Business, Tetris Effect Connected, Wo Long Fallen Dynasty, and more. Check here to get a full list of games. Don't forget that cross-device play histories on the app also landed for Xbox Insiders earlier this month, letting players see what games they have been playing regardless of console, PC, or cloud Xbox platform being used. Another new feature announced today as landing on the PC app this month is Rewards with Xbox. Only available in select markets and only for those above 18, Rewards can now be found in the Home section with easy access to checking out how to get more points, track progress, and more. The Xbox and Antstream Arcade joint venture, the Retro Classics app, is gaining seven more games too. These are Caesar, Conquests of Camelot: The Search for the Grail, Gabriel Knight: Sins of the Fathers, Hard Head, Okie Dokie, Skate Boardin’, and Skeleton+. Lastly, mouse and keyboard as well as touch controls continue to roll out for more games, with Police Simulator: Patrol Officers getting support for the former while South of Midnight has gained the latter.
  • Recent Achievements

    • Week One Done
      NeoWeen earned a badge
      Week One Done
    • One Month Later
      BA the Curmudgeon earned a badge
      One Month Later
    • First Post
      Doreen768 earned a badge
      First Post
    • One Month Later
      James_kobe earned a badge
      One Month Later
    • Week One Done
      James_kobe earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      660
    2. 2
      ATLien_0
      251
    3. 3
      Xenon
      164
    4. 4
      neufuse
      144
    5. 5
      +FloatingFatMan
      117
  • Tell a friend

    Love Neowin? Tell a friend!