Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: batch to copy last modified file and paste to a new folder  (Read 33386 times)

0 Members and 1 Guest are viewing this topic.

daillest319

    Topic Starter


    Beginner

    • Experience: Beginner
    • OS: Unknown
    I'm having trouble with a batch script to copy last modified file and paste to a new folder. IThe Script i have just copys my bat script plaease help. if anyone can help that would be great thank!!!


    Code: [Select]

    @echo off


    set source=C:\test\*.xls
    set dest=C:\test2
    pushd "%source%"
    set t=%date:~4%
    for /f %%a in ('dir /b /a-d /o-d') do call :PROCESS "%%a"
    goto :eof
    popd

    :PROCESS
    for /f %%j in ('echo %~t1') do set d=%%j
    if "%d%"=="%t%" copy %1 "%dest%"
    goto :eof




    pause


    Squashman



      Specialist
    • Thanked: 134
    • Experience: Experienced
    • OS: Other
    Re: batch to copy last modified file and paste to a new folder
    « Reply #1 on: May 18, 2012, 08:17:32 AM »
    Change your source variable to an actual directory name.  The PUSHD statement is failing on it.  You would see that if you acutally DEBUGGED your code by keeping the ECHO ON.

    If you just want to look for XLS documents then put that in your DIR command.

    Not really understanding why you are calling another LABEL and doing all the voodoo in there.

    daillest319

      Topic Starter


      Beginner

      • Experience: Beginner
      • OS: Unknown
      Re: batch to copy last modified file and paste to a new folder
      « Reply #2 on: May 18, 2012, 08:23:10 AM »
      is there a better way to do this? i honeslty been trying different script on the web. so i'm not even sure what all of this code does

      Squashman



        Specialist
      • Thanked: 134
      • Experience: Experienced
      • OS: Other
      Re: batch to copy last modified file and paste to a new folder
      « Reply #3 on: May 18, 2012, 08:40:39 AM »
      Assuming that when you mean LATEST modified file you really mean newest modified file in your source directory.

      Code: [Select]
      @echo off

      set source=C:\test
      set dest=C:\test2
      pushd "%source%"
      for /f "tokens=*" %%G in ('dir *.xls /b /a-d /od') do SET newest=%%G

      copy "%newest% "%dest%"
      popd

      dscam



        Newbie

        • Experience: Experienced
        • OS: Windows XP
        Re: batch to copy last modified file and paste to a new folder
        « Reply #4 on: December 19, 2015, 09:29:48 AM »
        Just wanted send a thanks to Squashman for posting this.
        I was looking for a way to make a copy of a backup file to 2 different
        network storage drives. I have done some sed scripting for unix enviorment
        so have some, I mean a (Little) :) knowledge of the process
        Maybe you can recommend some good ref books for DOS batch, learning for beginners. 

        Here is what I got to work for me (At least for my small test backup)
        Now when Genie Backup finishes, It executes this .bat file while I am sleeping.  :)
        Can never have to many Backups  ;D
        In case you see any potential problems.
        Changes
        1) 2 Destinations
        2) Put newest=%%G inside "" (Read that's for file names with spaces) (Not Sure But Works)
        3) Added 2nd " after "%newest%

        @echo on

        set source=W:\Test
        set dest1=X:\Test
        set dest2=V:\Test
        pushd "%source%"
        for /f "tokens=*" %%G in ('dir *.gbp /b /a-d /od') do SET "newest=%%G"

        copy "%newest%" "%dest1%"
        copy "%newest%" "%dest2%"
        popd

        Thanks Again for the info

        foxidrive



          Specialist
        • Thanked: 268
        • Experience: Experienced
        • OS: Windows 8
        Re: batch to copy last modified file and paste to a new folder
        « Reply #5 on: December 20, 2015, 12:28:11 AM »
        This is another way you could do it.

        Code: [Select]
        @echo on
        set "source=W:\Test"
        set "dest1=X:\Test"
        set "dest2=V:\Test"
        for /f "tokens=*" %%G in ('dir "%source%\*.gbp" /b /a-d /o-d') do (
           echo copying "%%a"
           copy "%source%\%%a" "%dest1%" >nul
           copy "%source%\%%a" "%dest2%" >nul
           goto :done
        )
        :done

        dscam



          Newbie

          • Experience: Experienced
          • OS: Windows XP
          Re: batch to copy last modified file and paste to a new folder
          « Reply #6 on: December 21, 2015, 03:36:42 PM »
          My Updated .bat to include a email notification upon copy completion
          Foxidrive I did see your email-bat.cmd at http://www.computerhope.com/forum/index.php?topic=132965.0
          But I think that's a little beyond me right now :)  It's not all contained in 1 file, but seems to work great.

          :: Test Batch File to Copy Genie Backup file
          :: to 2 Different Network Storage Devices.
          :: If the Copy is Successful, Then I get a Successful Email.
          :: If the Copy fails, Then I get a Failure Email.
          :: Emails sent using Switmail (Setup Individually for each Backup) 
          :: Created 12-21-15, Aint Technology Wonderful ;D

          @echo off

          set source=W:\Test
          set dest1=X:\Test
          set dest2=V:\Test
          pushd "%source%"
          for /f "tokens=*" %%G in ('dir *.gbp /b /a-d /od') do SET "newest=%%G"
             copy "%newest%" "%dest1%"
                IF %ERRORLEVEL%==0 (
                SwithMail.exe /s /x "C:\home\GBM_Scripts\SwMail\1TB_Copy_Test_Success.xml"
                ) ELSE (
                SwithMail.exe /s /x "C:\home\GBM_Scripts\SwMail\1TB_Copy_Test_Failure.xml")

             copy "%newest%" "%dest2%"
                IF %ERRORLEVEL%==0 (
                SwithMail.exe /s /x "C:\home\GBM_Scripts\SwMail\3TB_Copy_Test_Success.xml"
                ) ELSE (
                SwithMail.exe /s /x "C:\home\GBM_Scripts\SwMail\3TB_Copy_Test_Failure.xml")

          :done

          Maybe when I have more time & knowledge I try your email-bat.cmd

          Have a Good Holiday