Computer Hope

Microsoft => Microsoft DOS => Topic started by: user123 on February 13, 2020, 01:56:00 PM

Title: copy folder with latest time stamp using batch file
Post by: user123 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
Title: Re: copy folder with latest time stamp using batch file
Post by: Sidewinder 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)
Title: Re: copy folder with latest time stamp using batch file
Post by: user123 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!
Title: Re: copy folder with latest time stamp using batch file
Post by: Sidewinder 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)
Title: Re: copy folder with latest time stamp using batch file
Post by: user123 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!
Title: Re: copy folder with latest time stamp using batch file
Post by: Sidewinder 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)
Title: Re: copy folder with latest time stamp using batch file
Post by: user123 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!

Title: Re: copy folder with latest time stamp using batch file
Post by: Sidewinder 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)
Title: Re: copy folder with latest time stamp using batch file
Post by: user123 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
Title: Re: copy folder with latest time stamp using batch file
Post by: patio on February 21, 2020, 09:03:25 AM
+ 1 Sidewinder ! !
Title: Re: copy folder with latest time stamp using batch file
Post by: Squashman 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.