Page 1 of 2 12 LastLast
Results 1 to 15 of 25
  1. #1
    New Lounger
    Join Date
    Apr 2012
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Batch files that search three folders

    I would like a batch code to search threw 3 folders and to copy the newest file out of the 3 folders and to copy it to another location.
    I have a working batch that looks for the newest file but only searches in one folder

    Here it is:
    @echo off
    setlocal
    set srcDir=D:\test1
    set destdir=D:\test2
    set lastmod=
    pushd %srcDir%
    for /f "tokens=*" %%a in ('dir /b /od 2^>NUL') do set lastmod=%%a
    if "%lastmod%"=="" echo Could not locate files.&goto :eof
    copy "%lastmod%" "%destDir%"
    pause

    But i would like it to search in 3 folders and copy the newest file.
    Any help will be greatly appreciated.
    Thanks

  2. #2
    Super Moderator BATcher's Avatar
    Join Date
    Feb 2008
    Location
    A cultural area in SW England
    Posts
    2,854
    Thanks
    19
    Thanked 110 Times in 104 Posts
    Clearly you will need to search the three folders, and keep on saving the folder name and the file name of the "newest" file by comparing the 'modified' date and time of each file, sorted into yyyymmddhhmm order. Once you'd finished all three folders, you're left with the saved folder and 'newest' filename, and you can copy it to the destination folder.

    Is that good enough for you? or would you like me to have a go at rewriting it for you? (not until tomorrow!)
    If the latter, you're going to have to say whether there are any blanks or 'poison characters' like % & ^ etc in either the folder names or any of the filenames, which always make things difficult.

    By the way, you do a PUSHD without the compensating POPD (won't matter for one folder)
    and you can do
    IF NOT DEFINED lastmod (correct, no % signs)
    instead of testing whether %lastmod% is null.

    Alternatively you can find the newest file in each folder much as your code shows (repeated three times), then compare the 'newest' files from each of the three folders, to find which of the three is the 'newest', and then copy that file to the target folder. On further thought that would be easier. But you'd still have to find the date/time-stamp of each of the three files, knowing what format DIR gives for the date in your locale!
    Last edited by BATcher; 2012-04-25 at 15:47.
    BATcher

    Dear Diary, today the Hundred Years War started ...

  3. #3
    New Lounger
    Join Date
    Apr 2012
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Yeah i know that the 3 folders will be modified at different intervals and therefore when the batch is run it will scan the folders and copy the latest one will be copied to the destination folder.
    If you wouldn't mind could you have a go rewriting the code please.
    I am still new to batch coding but willing to give it a go and learn how it works.
    Thanks in advanced

  4. #4
    Super Moderator BATcher's Avatar
    Join Date
    Feb 2008
    Location
    A cultural area in SW England
    Posts
    2,854
    Thanks
    19
    Thanked 110 Times in 104 Posts
    Here you are! Instead of messing around with date and time formats I've chosen to set up an intermediate folder to hold the newest file from each of the three original folders, then found the newest of the three files from the intermediate folder to copy to the destination folder. I've assumed that there will always be at least one file in each of the three original folders, so no need to ensure that there really is a "newest" file each time. Also assumed is that there are no spaces in any of the original foldernames. Anything of a debugging nature is put in CAPITALS. The . in .off simply makes all the output visible, remove if you wish invisibility.

    Code:
    @echo .off
    CLS
    :: copy the newest file in each of three source folders 
    ::   into an intermediate folder
    :: then from the intermediate folder, copy the newest  
    ::    of the three copied files into a target folder
    
    setlocal
    
    :: folder names - replace as appropriate
    set sourcea=F:\Dan09\SourceA
    set sourceb=F:\Dan09\SourceB
    set sourcec=F:\Dan09\SourceC
    set intermed=F:\Dan09\$$temp$$
    set target=F:\Dan09\Target
    
    :: set up the intermediate folder
    rd %intermed% /q /s 2>nul
    md %intermed%
    
    :: find the newest file in each of the source folders 
    ::   and copy each to the destination folder
    set dest=%intermed%
    for %%a in (%sourcea% %sourceb% %sourcec%) do call :copynewest %%a
    
    :: find the newest file in the intermediate folder 
    ::   and copy to the target folder
    set dest=%target%
    call :copynewest %intermed%
    
    :: tidy up by deleting the intermediate folder
    rd %intermed% /q /s
    
    endlocal
    goto :eof
    ::--------------------------------------------------------------------
    
    :copynewest  from passed foldername to dest foldername
    set passed=%1
    for /f "tokens=*" %%a in ('dir /b /od "%passed%" 2^>nul') do set newest=%%a
    copy "%passed%\%newest%" "%dest%"
    DIR %dest%
    PAUSE
    goto :eof
    BATcher

    Dear Diary, today the Hundred Years War started ...

  5. The Following 2 Users Say Thank You to BATcher For This Useful Post:

    dan09 (2012-04-26),SF99 (2012-04-26)

  6. #5
    New Lounger
    Join Date
    Apr 2012
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks for that I will try out the batch code later when I get home.
    Thanks you again and I will let you know how I got on

  7. #6
    New Lounger
    Join Date
    Apr 2012
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks you, the batch code works brilliantly and works just how i would like it to be.
    Thanks again

  8. #7
    Super Moderator BATcher's Avatar
    Join Date
    Feb 2008
    Location
    A cultural area in SW England
    Posts
    2,854
    Thanks
    19
    Thanked 110 Times in 104 Posts
    I'm very pleased it does what you wanted!
    BATcher

    Dear Diary, today the Hundred Years War started ...

  9. #8
    New Lounger
    Join Date
    Apr 2012
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I was just wondering if possible could you add a feature that will only copy/find a specific file type
    E.g .doc or .zip
    It would be helpful if you could
    Thanks

  10. #9
    Super Moderator BATcher's Avatar
    Join Date
    Feb 2008
    Location
    A cultural area in SW England
    Posts
    2,854
    Thanks
    19
    Thanked 110 Times in 104 Posts
    To find the newest ZIP file in the three folders, change the FOR /F line to be
    for /f "tokens=*" %%a in ('dir /b /od "%passed%\*.zip" 2^>nul') do set newest=%%a

    From your question I'd be slightly suspicious that you might not have written the original BATch file fragment...
    BATcher

    Dear Diary, today the Hundred Years War started ...

  11. #10
    New Lounger
    Join Date
    Apr 2012
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Ha yeah your right , how did you guess that lol
    Thanks for the quick response.
    Your really good at batch scripts

  12. #11
    New Lounger
    Join Date
    Apr 2012
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts
    The batch works fine when it only has a folder with no spaces.
    but when there is a space it it fails to work.
    Is there any workaround to this besides from renaming the folders

  13. #12
    Super Moderator BATcher's Avatar
    Join Date
    Feb 2008
    Location
    A cultural area in SW England
    Posts
    2,854
    Thanks
    19
    Thanked 110 Times in 104 Posts
    Quote Originally Posted by dan09 View Post
    The batch works fine when it only has a folder with no spaces.
    That was one of the conditions I stated in my post #4, based on your example folders!

    I will try to rework it later today...
    BATcher

    Dear Diary, today the Hundred Years War started ...

  14. #13
    Super Moderator BATcher's Avatar
    Join Date
    Feb 2008
    Location
    A cultural area in SW England
    Posts
    2,854
    Thanks
    19
    Thanked 110 Times in 104 Posts
    Here's the revised version, with spaces in the folder names...
    Code:
    @echo .off
    CLS
    :: copy the newest file in each of three source folders 
    ::   into an intermediate folder
    :: then from the intermediate folder, copy the newest  
    ::    of the three copied files into a target folder
    
    setlocal
    
    :: folder names - replace as appropriate
    set sourcea=F:\Dan 09\Source A
    set sourceb=F:\Dan 09\Source B
    set sourcec=F:\Dan 09\Source C
    set intermed=F:\Dan 09\$$ temp $$
    set target=F:\Dan 09\Tar get
    
    :: set up the intermediate folder
    rd "%intermed%" /q /s 2>nul
    md "%intermed%"
    
    :: find the newest file in each of the source folders 
    ::   and copy each to the destination folder
    set dest=%intermed%
    for %%a in ("%sourcea%" "%sourceb%" "%sourcec%") do call :copynewest %%a
    
    :: find the newest file in the intermediate folder 
    ::   and copy to the target folder
    set dest=%target%
    call :copynewest "%intermed%"
    
    :: tidy up by deleting the intermediate folder
    rd "%intermed%" /q /s
    
    endlocal
    goto :eof
    ::--------------------------------------------------------------------
    
    :copynewest  from passed foldername to dest foldername
    
    set passed=%~1
    for /f "tokens=*" %%a in ('dir /b /od "%passed%\*.zip" 2^>nul') do set newest=%%a
    copy "%passed%\%newest%" "%dest%"
    DIR "%dest%"
    PAUSE
    goto :eof
    BATcher

    Dear Diary, today the Hundred Years War started ...

  15. #14
    Platinum Lounger
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    3,837
    Thanks
    7
    Thanked 253 Times in 238 Posts
    What happens if you have more than one file that has not been copied to the destination? Does it matter?

    cheers, Paul

  16. #15
    Super Moderator BATcher's Avatar
    Join Date
    Feb 2008
    Location
    A cultural area in SW England
    Posts
    2,854
    Thanks
    19
    Thanked 110 Times in 104 Posts
    The specification was simply to find the newest file in three folders, and copy this single file to the destination folder.
    No, I don't know what purpose this served, but i suspect it was someone's BATch file homework!
    BATcher

    Dear Diary, today the Hundred Years War started ...

Page 1 of 2 12 LastLast

Posting Permissions

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