# Thread: Batch files that search three folders

1. The batch works great, all is good it does what i needed it to do.
I may need it to be tweaked a little bit but i will confirm it later on.
It wasn't homework it was for personal use, i couldn't find any software that would do the same job so i decide that i would do it myself, with some help of course
Thanks you.

2. Had you considered using the Windows REPLACE command line utility? Doing something like replace /u "F:\Dan 09\SourceA\*.*" "F:\Dan 09\Target\*.*" will only copy matching files from the source to the target if the source files are newer than the corresponding ones in the target. You could run this multiple times, once for each source folder. I use it often to update my USB drive with updated files after making changes and it keeps me from copying over stuff that's not already on the stick. Only files in both places are considered for updating.

Joe

3. Fine - if that's what the OP wanted to do. But he wants the (single) newest file in three folders to be copied to the destination folder. We aren't comparing the timestamps for two identically-named files!

You could also use XCOPY /D (see the /? help information) to give a similar result to REPLACE, but I say again, what you are suggesting is the answer to a different problem!

4. BATcher is right, i was looking for it to find the newest file out of the 3 folders.
But thank you anyway

5. I see. I didn't quite understand the problem statement. Never mind!

6. Sorry, but when I saw this post I just had to write the script in PwoerShell. Quick-and-dirty (replace the fake directory names with the names you want):

Code:
Copy-Item ((dir "c:\test1") + (dir "c:\test2") + (dir "c:\test3") | sort LastWriteTime -desc)[0] "c:\to dir"
Here is a script file that will ask for the three directory names and assumes the current directory as the destination.

Code:
param (
[string]$toDir = ".", [string]$fromDir1 = $(Read-Host -prompt "First directory"), [string]$fromDir2 = $(Read-Host -prompt "Second directory"), [string]$fromDir3 = $(Read-Host -prompt "Third directory") )$file = ((dir $fromDir1) + (dir$fromDir2) + (dir $fromDir3) | sort LastWriteTime -desc)[0] Copy-Item$file.PSPath $toDir "Copied " +$file.FullName + " to " + $toDir You can replace the Read-Host parts in the param section with hard-coded values if you like, example: Code:  [string]$fromDir1 = "c:\test1"),
So what does the script do? It gathers the contents of each directory as a collection and combines the collections, which it then sorts in descending order of last modified time. It then grabs the first file in the result (which is the newest file) and copies it.

7. ## Thanks

I managed to get it working anyway without the code.
But thanks anyway for all your help means a lot that there is a community out there helping people out.
Dan

8. I hate to hijack a thread, its been awhile and I would like to see if someone can help me with the code that is already here by BATcher. I am looking to do what the op wanted with a little more "involvement".

3 servers each with D:\folder, and sub folders inside of that (D:\folder\Dist0000, D:\folder\Dist0001 and so on). My hope is to loop through each sub folder and sync up the files within the sub folders not the entire sub folder with the newest version across all 3 servers.

I hope that makes sense.

Thanks,

9. Hey there, I will try and help as much as I can.
Since asking the question I have managed to sort out my problem

What I have done is copy to a central place at start up (newest files only), then when the other pc shutdown they will copy the files over.

Code:
xcopy "D:\folder\Dist0000\files.txt" "C:\Users\test" /D /Y >> "Y:\Log\Log.txt"
Then do the opposite on shutdown

you may need to tweak it a bit to go through sub folders but that's what I have done
Hope this helped

10. Robocopy is best for updating to the latest IMO.
Something like this: robocopy \\server1\share\folder \\server2\share\folder /s /np /ndl /nfl /log+:C:\temp\updates.log

cheers, Paul

Page 2 of 2 First 12

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•