Batch file to move/compress/delete files












2















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:AgentricsintegrationincomingJason D:AgentricsintegrationincomingJasonarchive /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 FilesWinRARWinRAR.exe" a -r "%%~nA.zip" "%%A"
FOR %%A IN (*.TXT, *.cpi) DO DEL "D:AgentricsintegrationincomingJasonarchive.cpi*" "%%A"

REM Delete all files in the backup directory that are older than 6 months
forfiles /p D:AgentricsintegrationincomingJasonarchive /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:AgentricsintegrationincomingMansoorbackup*.txt*, D:AgentricsintegrationincomingMansoorbackup*.cpi*) DO "C:Program FilesWinRARWinRAR.exe" a -r D:AgentricsintegrationincomingMansoorbackup"%%~nA.zip" "%%A" Regards, Mansoor

    – Mansoor Ahmad
    Feb 28 '13 at 16:23


















2















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:AgentricsintegrationincomingJason D:AgentricsintegrationincomingJasonarchive /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 FilesWinRARWinRAR.exe" a -r "%%~nA.zip" "%%A"
FOR %%A IN (*.TXT, *.cpi) DO DEL "D:AgentricsintegrationincomingJasonarchive.cpi*" "%%A"

REM Delete all files in the backup directory that are older than 6 months
forfiles /p D:AgentricsintegrationincomingJasonarchive /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:AgentricsintegrationincomingMansoorbackup*.txt*, D:AgentricsintegrationincomingMansoorbackup*.cpi*) DO "C:Program FilesWinRARWinRAR.exe" a -r D:AgentricsintegrationincomingMansoorbackup"%%~nA.zip" "%%A" Regards, Mansoor

    – Mansoor Ahmad
    Feb 28 '13 at 16:23
















2












2








2








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:AgentricsintegrationincomingJason D:AgentricsintegrationincomingJasonarchive /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 FilesWinRARWinRAR.exe" a -r "%%~nA.zip" "%%A"
FOR %%A IN (*.TXT, *.cpi) DO DEL "D:AgentricsintegrationincomingJasonarchive.cpi*" "%%A"

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









share|improve this question
















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:AgentricsintegrationincomingJason D:AgentricsintegrationincomingJasonarchive /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 FilesWinRARWinRAR.exe" a -r "%%~nA.zip" "%%A"
FOR %%A IN (*.TXT, *.cpi) DO DEL "D:AgentricsintegrationincomingJasonarchive.cpi*" "%%A"

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






batch-file robocopy winrar






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 24 '17 at 17:59









djsmiley2k

5,12612336




5,12612336










asked Feb 27 '13 at 18:49









Mansoor AhmadMansoor Ahmad

1112




1112








  • 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:AgentricsintegrationincomingMansoorbackup*.txt*, D:AgentricsintegrationincomingMansoorbackup*.cpi*) DO "C:Program FilesWinRARWinRAR.exe" a -r D:AgentricsintegrationincomingMansoorbackup"%%~nA.zip" "%%A" Regards, Mansoor

    – Mansoor Ahmad
    Feb 28 '13 at 16:23
















  • 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:AgentricsintegrationincomingMansoorbackup*.txt*, D:AgentricsintegrationincomingMansoorbackup*.cpi*) DO "C:Program FilesWinRARWinRAR.exe" a -r D:AgentricsintegrationincomingMansoorbackup"%%~nA.zip" "%%A" Regards, Mansoor

    – Mansoor Ahmad
    Feb 28 '13 at 16:23










1




1





doesn't this work: forefiles /M *.txt /D -7 /C WinRAR a -r aa.zip @file ?

– harper
Feb 27 '13 at 19:27





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:AgentricsintegrationincomingMansoorbackup*.txt*, D:AgentricsintegrationincomingMansoorbackup*.cpi*) DO "C:Program FilesWinRARWinRAR.exe" a -r D:AgentricsintegrationincomingMansoorbackup"%%~nA.zip" "%%A" Regards, Mansoor

– Mansoor Ahmad
Feb 28 '13 at 16:23







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:AgentricsintegrationincomingMansoorbackup*.txt*, D:AgentricsintegrationincomingMansoorbackup*.cpi*) DO "C:Program FilesWinRARWinRAR.exe" a -r D:AgentricsintegrationincomingMansoorbackup"%%~nA.zip" "%%A" Regards, Mansoor

– Mansoor Ahmad
Feb 28 '13 at 16:23












1 Answer
1






active

oldest

votes


















0














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:AgentricsintegrationincomingJason /M *.txt /S /D -2 /C "cmd /c move @file D:AgentricsintegrationincomingJasonarchive"
forfiles /P D:AgentricsintegrationincomingJason /M *.cpi /S /D -2 /C "cmd /c move @file D:AgentricsintegrationincomingJasonarchive"
REM zip all files in the backup directory
for /f "tokens=*" %%a in ('forfiles /p D:AgentricsintegrationincomingJasonarchive /s /d -7') do "C:Program FilesWinRARWinRAR.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:AgentricsintegrationincomingJasonarchive /s /d -183') do del D:AgentricsintegrationincomingJasonarchive%%a /y





share|improve this answer























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "3"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f558352%2fbatch-file-to-move-compress-delete-files%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    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:AgentricsintegrationincomingJason /M *.txt /S /D -2 /C "cmd /c move @file D:AgentricsintegrationincomingJasonarchive"
    forfiles /P D:AgentricsintegrationincomingJason /M *.cpi /S /D -2 /C "cmd /c move @file D:AgentricsintegrationincomingJasonarchive"
    REM zip all files in the backup directory
    for /f "tokens=*" %%a in ('forfiles /p D:AgentricsintegrationincomingJasonarchive /s /d -7') do "C:Program FilesWinRARWinRAR.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:AgentricsintegrationincomingJasonarchive /s /d -183') do del D:AgentricsintegrationincomingJasonarchive%%a /y





    share|improve this answer




























      0














      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:AgentricsintegrationincomingJason /M *.txt /S /D -2 /C "cmd /c move @file D:AgentricsintegrationincomingJasonarchive"
      forfiles /P D:AgentricsintegrationincomingJason /M *.cpi /S /D -2 /C "cmd /c move @file D:AgentricsintegrationincomingJasonarchive"
      REM zip all files in the backup directory
      for /f "tokens=*" %%a in ('forfiles /p D:AgentricsintegrationincomingJasonarchive /s /d -7') do "C:Program FilesWinRARWinRAR.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:AgentricsintegrationincomingJasonarchive /s /d -183') do del D:AgentricsintegrationincomingJasonarchive%%a /y





      share|improve this answer


























        0












        0








        0







        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:AgentricsintegrationincomingJason /M *.txt /S /D -2 /C "cmd /c move @file D:AgentricsintegrationincomingJasonarchive"
        forfiles /P D:AgentricsintegrationincomingJason /M *.cpi /S /D -2 /C "cmd /c move @file D:AgentricsintegrationincomingJasonarchive"
        REM zip all files in the backup directory
        for /f "tokens=*" %%a in ('forfiles /p D:AgentricsintegrationincomingJasonarchive /s /d -7') do "C:Program FilesWinRARWinRAR.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:AgentricsintegrationincomingJasonarchive /s /d -183') do del D:AgentricsintegrationincomingJasonarchive%%a /y





        share|improve this answer













        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:AgentricsintegrationincomingJason /M *.txt /S /D -2 /C "cmd /c move @file D:AgentricsintegrationincomingJasonarchive"
        forfiles /P D:AgentricsintegrationincomingJason /M *.cpi /S /D -2 /C "cmd /c move @file D:AgentricsintegrationincomingJasonarchive"
        REM zip all files in the backup directory
        for /f "tokens=*" %%a in ('forfiles /p D:AgentricsintegrationincomingJasonarchive /s /d -7') do "C:Program FilesWinRARWinRAR.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:AgentricsintegrationincomingJasonarchive /s /d -183') do del D:AgentricsintegrationincomingJasonarchive%%a /y






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 10 '15 at 12:11









        HardomanHardoman

        76339




        76339






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Super User!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f558352%2fbatch-file-to-move-compress-delete-files%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to reconfigure Docker Trusted Registry 2.x.x to use CEPH FS mount instead of NFS and other traditional...

            is 'sed' thread safe

            How to make a Squid Proxy server?