copying the latest of some folders to a new destination

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Sebastian42
Posts: 34
Joined: 17 Feb 2017 02:28

copying the latest of some folders to a new destination

#1 Post by Sebastian42 » 30 Oct 2021 05:58

Some forum whose name I do not remember, kindly gave me the code to select the latest from a number of files, and copy it to a new destination. I do not understand the code at all, so cannot do the next step myself, but believe that it could be used as a template for copying the latest FOLDER to a new destination. I would like help from someone who understands this.

set source="C:\folderpath\filename"
set destination="C:\destinationpath\filename"

FOR /F "delims=" %%I IN ('DIR %source%\*.* /A:-D /O:-D /B') DO COPY %source%\"%%I" %destination% & echo %%I & GOTO :END
:END

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: copying the latest of some folders to a new destination

#2 Post by aGerman » 30 Oct 2021 07:22

/A:-D explicitly excludes directories from being processed by DIR. In contrast, /A:D would only process directories.

Steffen

Sebastian42
Posts: 34
Joined: 17 Feb 2017 02:28

Re: copying the latest of some folders to a new destination

#3 Post by Sebastian42 » 30 Oct 2021 18:36

I made three changes; since no files are involved, but only folders, I removed 'filename' from the first two lines and removed the '-' in front of D. But the resulting code did not seem to do anything.

set source="C:\folderpath"
set destination="C:\destinationpath"

FOR /F "delims=" %%I IN ('DIR %source%\*.* /A:D /O:-D /B') DO COPY %source%\"%%I" %destination% & echo %%I & GOTO :END
:END

AR Coding
Posts: 53
Joined: 02 May 2021 21:16

Re: copying the latest of some folders to a new destination

#4 Post by AR Coding » 30 Oct 2021 22:31

I dont know if this helps but i fixed some quoteS

Code: Select all

set "source=C:\folderpath"
set "destination=C:\destinationpath"

FOR /F "delims=" %%I IN ('DIR "%source%\*.*" /A:D /O:-D /B') DO COPY "%source%\%%I" "%destination%" && echo %%I

Sebastian42
Posts: 34
Joined: 17 Feb 2017 02:28

Re: copying the latest of some folders to a new destination

#5 Post by Sebastian42 » 31 Oct 2021 04:36

The change in quotes seemed sensible but did not achieve functionality.

Intentionally or other wise, you made changes after "%destination%".
With or without those other changes, it still does not work.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: copying the latest of some folders to a new destination

#6 Post by aGerman » 31 Oct 2021 08:47

Code: Select all

FOR /F "delims=" %%I IN ('DIR "%source%\*.*" /A:D /O:-D /B') DO robocopy "%source%\%%I" "%destination%\%%I" /s /e /r:1 /w:1 &echo "%%I"&goto END
:END
Steffen

Sebastian42
Posts: 34
Joined: 17 Feb 2017 02:28

Re: copying the latest of some folders to a new destination

#7 Post by Sebastian42 » 31 Oct 2021 14:53

That worked ! Thank you.

Sebastian42
Posts: 34
Joined: 17 Feb 2017 02:28

Re: copying the latest of some folders to a new destination

#8 Post by Sebastian42 » 25 Sep 2023 00:42

I do not know when the success ended nor why, but that code no longer copies (any) file into the destination.

Perhaps because I have shifted the goal posts.
I do not remember why I wanted a FOLDER copied. I may have used the wrong term, actually meaning FILE - that is certainly what I want NOW in 2023.

My own efforts can achieve copying of all the files in the source, but I want to copy only the latest.

Batcher
Posts: 74
Joined: 16 Apr 2009 10:36

Re: copying the latest of some folders to a new destination

#9 Post by Batcher » 25 Sep 2023 02:18

CopyLatestOneFile.bat

Code: Select all

@echo off
set "source=C:\Your\Folder\From"
set "destination=C:\Your\Folder\To"
for /f "delims=" %%i in ('dir /b /a-d /o-d "%source%"') do (
    echo %%i
    copy "%source%\%%i" "%destination%"
    goto :end
)

:end
pause

Batcher
Posts: 74
Joined: 16 Apr 2009 10:36

Re: copying the latest of some folders to a new destination

#10 Post by Batcher » 25 Sep 2023 02:22

CopyNewerFiles.bat

Code: Select all

@echo off
set "source=C:\Your\Folder\From"
set "destination=C:\Your\Folder\To"
xcopy /d "%source%" "%destination%"
pause

Sebastian42
Posts: 34
Joined: 17 Feb 2017 02:28

Re: copying the latest of some folders to a new destination

#11 Post by Sebastian42 » 25 Sep 2023 03:14

I tried your second one - it looked the simplest. It did no better than my own primitive efforts.

Could you please tell me what the 'pause' achieves ?

Your first one achieves what I want,
so I repeat what I said in 2021 "That worked ! Thank you."

For what it is worth - it is just alphabet soup to me.
Gratefully yours.

Post Reply