• 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

    • I don't think there is any problem with the two-digit number. Even if someone didn't understand the meaning, 26 > 18, so they will still understand at a glance that it is a newer version. The only downside I see is someone assuming 18 is a MUCH older than 26, but IMO, that confusion really isn't a big issue. The truth is that the majority of software companies use the 2-digit number; Microsoft is the outlier, and even they used 2-digit numbers in the past.
    • So, now it can give inaccurate answers and peddle lies in an even more human sounding voice? :)
    • Aiarty Image Enhancer for PC & Mac (worth $85) free offer ends today June 8 by Steven Parker Claim your complimentary eBook worth $85 for free, before the offer ends today June 8. Get Aiarty Image Enhancer to restore and upscale images to 32K with more details. Make your images and photos larger, sharper and clearer with AI. Tired of the tiny, blurry or low-resolution images downloaded from web or grainy pictures taken by old smartphones? Aiarty Image Enhancer comes to help you out. Leveraging tailored AI models and deep learning algorithms, it can transform any images, photos and AI arts into high-quality visuals with exceptional clarity and realistic details. No matter you’re enhancing web graphics, restoring cherished family memories, or preparing visuals for creative projects, Aiarty can produce studio-quality results in just a few clicks. Get it now to transform your images to stunning masterpieces at zero cost! Key Features: Upscale: enlarge images and photos to 4K/8K/32K for printing, archiving, sharing, displaying, or more. More details: generate more realistic details for hair, fur, feature, skin textures and so on, ensuring crystal-clear pixels. Restore face with faithful details, perfect for portraits, selfies, old photos, group shot. 4-in-1 enhancement: deblur, denoise, dejpeg and upscale image in one click. Batch process. Enhance 3000+ images at once, streamlining image editing workflow. Fast & Simple. Upscale 100 images in 2 minutes with just one click. No skills needed. Trained on 6.78 million images. Optimized for NVIDIA, AMD, and Intel GPUs & CPUs. The exclusive offer ends on June 8th. Don't miss out – grab your free licensed copy today. How to get it Please ensure you read the terms and conditions to claim this offer. Complete and verifiable information is required in order to receive this free offer. If you have previously made use of these free offers, you will not need to re-register. While supplies last! Download Aiarty Image Enhancer for PC/Mac (worth $85) for free Offered by Digiarty WinXDVD, view other free resources The below offers are also available for free in exchange for your (work) email: Winxvideo AI V3.0 Lifetime License for PC ($69.95 Value) FREE – Expires 6/8 Aiarty Image Enhancer for PC/Mac ($85 Value) FREE – Expires 6/8 Solutions Architect's Handbook, Third Edition ($42.99 Value) FREE – Expires 6/10 AI and Innovation ($21 Value) FREE – Expires 6/11 Unruly: Fighting Back when Politics, AI, and Law Upend [...] ($18 Value) FREE - Expires 6/17 SQL Essentials For Dummies ($10 Value) FREE – Expires 6/17 Continuous Testing, Quality, Security, and Feedback ($27.99 Value) FREE – Expires 6/18 VideoProc Converter AI v7.5 for FREE (worth $78.90) – Expires 6/18 Macxvideo AI ($39.95 Value) Free for a Limited Time – Expires 6/22 The Ultimate Linux Newbie Guide – Featured Free content Python Notes for Professionals – Featured Free content Learn Linux in 5 Days – Featured Free content Quick Reference Guide for Cybersecurity – Featured Free content We post these because we earn commission on each lead so as not to rely solely on advertising, which many of our readers block. It all helps toward paying staff reporters, servers and hosting costs. Other ways to support Neowin The above deal not doing it for you, but still want to help? Check out the links below. Check out our partner software in the Neowin Store Buy a T-shirt at Neowin's Threadsquad Subscribe to Neowin - for $14 a year, or $28 a year for an ad-free experience Disclosure: An account at Neowin Deals is required to participate in any deals powered by our affiliate, StackCommerce. For a full description of StackCommerce's privacy guidelines, go here. Neowin benefits from shared revenue of each sale made through the branded deals site.
    • You will find sketch and clone apps on both the Play Store and the App Store contrary to what Google and Apple will tell you. This fact negates their argument for why there shouldn't be other app stores and so on, it's BS at the end.
    • I ran the powershell script and it did not "recreate the inetpub directory if it has been deleted" I rebooted several times and also tried re running the script. I had to click y three times when I ran it initially as well, now running it just returns to a new poweshell line with no errors but still no directory recreated. meh
  • Recent Achievements

    • First Post
      viraltui earned a badge
      First Post
    • Reacting Well
      viraltui earned a badge
      Reacting Well
    • Week One Done
      LunaFerret earned a badge
      Week One Done
    • Week One Done
      Ricky Chan earned a badge
      Week One Done
    • Week One Done
      maimutza earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      482
    2. 2
      +FloatingFatMan
      264
    3. 3
      snowy owl
      238
    4. 4
      ATLien_0
      231
    5. 5
      Edouard
      177
  • Tell a friend

    Love Neowin? Tell a friend!