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

Author Topic: copy folder with latest time stamp using batch file  (Read 21779 times)

0 Members and 1 Guest are viewing this topic.

user123

    Topic Starter


    Greenhorn

    • Experience: Beginner
    • OS: Windows 10
    copy folder with latest time stamp using batch file
    « on: February 13, 2020, 01:56:00 PM »
    Hello,

    I am a new user trying to use batch files for the first time. I am trying to create a batch file that will detect the folder with the latest time stamp, copy the entire folder and paste into another folder. The batch file will run everyday as new folders are added on a daily basis.

    The folders are located on a network drive and I have been having trouble with the path as well.

    I have been using the code below but it hasn't been working in my favor. Any help would be appreciated. Thanks!

    @echo off

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

    copy "%newest% "%dest%"
    popd
    cmd /k

    Sidewinder



      Guru

      Thanked: 139
    • Experience: Familiar
    • OS: Windows 10
    Re: copy folder with latest time stamp using batch file
    « Reply #1 on: February 15, 2020, 07:34:00 AM »
    When variables are set inside a do loop, delayed expansion is needed to access them outside the loop:

    Code: [Select]
    @echo off
    setlocal enabledelayedexpansion

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

    copy "!newest!" "%dest%"
    popd
    cmd /k

    Happy Coding  8)
    The true sign of intelligence is not knowledge but imagination.

    -- Albert Einstein

    user123

      Topic Starter


      Greenhorn

      • Experience: Beginner
      • OS: Windows 10
      Re: copy folder with latest time stamp using batch file
      « Reply #2 on: February 20, 2020, 08:08:28 AM »
      Thanks for your reply Sidewinder. Would you be able to help me with another question I had about my code? I need to copy the folder with the latest timestamp on it including all the sub folders in that folder. I think I need to make changes to this part of the code ('dir *.* /b /a-d /od').

      Would you know how to alter it to include copying over folders and the subfolders?

      Thanks!

      Sidewinder



        Guru

        Thanked: 139
      • Experience: Familiar
      • OS: Windows 10
      Re: copy folder with latest time stamp using batch file
      « Reply #3 on: February 20, 2020, 10:12:29 AM »
      The dir command would need to select directories and not files [change the /a switch]. Also xcopy would be used instead of copy. Something like this might be helpful:

      Code: [Select]
      @echo off
      setlocal enabledelayedexpansion

      set source=\\network_path\test
      set dest= \\network_path\test2
      pushd "%source%"
      for /f "tokens=*" %%G in ('dir *.* /b /ad /od') do SET newest=%%G

      xcopy /s /e /i "!newest!" "%dest%"
      popd
      cmd /k

      Hope This Helps  8)
      The true sign of intelligence is not knowledge but imagination.

      -- Albert Einstein

      user123

        Topic Starter


        Greenhorn

        • Experience: Beginner
        • OS: Windows 10
        Re: copy folder with latest time stamp using batch file
        « Reply #4 on: February 20, 2020, 01:29:19 PM »
        Thanks for your quick reply Sidewinder. This worked perfectly but the only problem I am facing is that the code is copying all the folders within the "test" folder and not the "test" folder itself.

        Is there a way we can copy the "test" folder and the sub folders within it?

        Thank you for your continued help!

        Sidewinder



          Guru

          Thanked: 139
        • Experience: Familiar
        • OS: Windows 10
        Re: copy folder with latest time stamp using batch file
        « Reply #5 on: February 20, 2020, 02:51:56 PM »
        I'm a bit confused, but onward and upward. The test in the source variable is not a folder but a share name. You can direct the data on the dest variable

        Something like this:
        Code: [Select]
        @echo off
        setlocal enabledelayedexpansion

        set source=\\network_path\test
        set dest=\\network_path\test2
        pushd "%source%"

        for /f "tokens=*" %%G in ('dir *.* /b /ad /od') do SET newest=%%G

        xcopy /s /e /i "!newest!" "%dest%\!newest!"
        popd
        cmd /k

        Note: As written above on the xcopy command, the top level folder will be the name of the folder you just copied. If you want the top level folder to be something specific, you can hardcode the value in place of !newest!, like "%dest%\test"

        Keep Us Posted  8)
        The true sign of intelligence is not knowledge but imagination.

        -- Albert Einstein

        user123

          Topic Starter


          Greenhorn

          • Experience: Beginner
          • OS: Windows 10
          Re: copy folder with latest time stamp using batch file
          « Reply #6 on: February 20, 2020, 03:19:00 PM »
          Thanks for your reply. Let me try explaining exactly what I need my code to do. The "test" folder has various folders in it which correspond with today's date. A new folder is added everyday with the current date as its filename. Here is an example of what the inside of the "test" folder looks like:

          19Feb2020
          18Feb2020
          17Feb2020
          and so on...

          The code I am trying to create needs to pick up the folder with the latest timestamp (i.e. 19Feb2020) and paste it into folder "test2".

          So far, the code is copying all the contents inside "19Feb2020" and pasting it into "test2". I need the code to copy the folder (19Feb2020), including the sub folders, and paste it into "test2".

          Since the filename changes everyday, I am using this strategy to detect the latest file and paste it into the required path. Please let me know if you can think of another approach to this problem or any alterations to the pre-existing code.

          Thank you very much!


          Sidewinder



            Guru

            Thanked: 139
          • Experience: Familiar
          • OS: Windows 10
          Re: copy folder with latest time stamp using batch file
          « Reply #7 on: February 21, 2020, 06:22:34 AM »
          You are using the Universal Naming Convention (UNC) for both the source and dest variables. Not a problem but they are different than local folder paths. The \\network_path in both variables is not a folder name but the machine name of the network device. test and test2 in each of the variables is also not a folder name but the sharename used to access the device or when mapping the remote drive to a local drive letter. If you want everything to be copied to the test2 folder, you will have to create it yourself. XCOPY will do this for you if you add test2 to the dest variable on the xcopy instruction.

          Code: [Select]
          @echo off
          setlocal enabledelayedexpansion

          set source=\\network_path\test
          set dest=\\network_path\test2
          pushd "%source%"

          for /f "tokens=*" %%G in ('dir *.* /b /ad /od') do SET newest=%%G

          xcopy /s /e /i "!newest!" "%dest%\Test2\!newest!"
          popd
          cmd /k

          You may have to tweak the xcopy parameters, but this should work if my interpretation of your request is correct.

           8)
          The true sign of intelligence is not knowledge but imagination.

          -- Albert Einstein

          user123

            Topic Starter


            Greenhorn

            • Experience: Beginner
            • OS: Windows 10
            Re: copy folder with latest time stamp using batch file
            « Reply #8 on: February 21, 2020, 07:55:15 AM »
            You are a genius Sidewinder. This works perfectly! I have been trying to figure this out for the past week. Thank you for your help!  :D

            patio

            • Moderator


            • Genius
            • Maud' Dib
            • Thanked: 1769
              • Yes
            • Experience: Beginner
            • OS: Windows 7
            Re: copy folder with latest time stamp using batch file
            « Reply #9 on: February 21, 2020, 09:03:25 AM »
            + 1 Sidewinder ! !
            " Anyone who goes to a psychiatrist should have his head examined. "

            Squashman



              Specialist
            • Thanked: 134
            • Experience: Experienced
            • OS: Other
            Re: copy folder with latest time stamp using batch file
            « Reply #10 on: February 23, 2020, 01:44:48 AM »
            There is no need for using delayed expansion as the variable is used after the FOR command has completed.