Page 1 of 1

copying the latest of some folders to a new destination

Posted: 30 Oct 2021 05:58
by Sebastian42
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

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

Posted: 30 Oct 2021 07:22
by aGerman
/A:-D explicitly excludes directories from being processed by DIR. In contrast, /A:D would only process directories.

Steffen

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

Posted: 30 Oct 2021 18:36
by Sebastian42
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

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

Posted: 30 Oct 2021 22:31
by AR Coding
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

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

Posted: 31 Oct 2021 04:36
by Sebastian42
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.

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

Posted: 31 Oct 2021 08:47
by aGerman

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

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

Posted: 31 Oct 2021 14:53
by Sebastian42
That worked ! Thank you.

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

Posted: 25 Sep 2023 00:42
by Sebastian42
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.

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

Posted: 25 Sep 2023 02:18
by Batcher
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

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

Posted: 25 Sep 2023 02:22
by Batcher
CopyNewerFiles.bat

Code: Select all

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

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

Posted: 25 Sep 2023 03:14
by Sebastian42
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.