copying the latest of some folders to a new destination
Moderator: DosItHelp
-
- Posts: 34
- Joined: 17 Feb 2017 02:28
copying the latest of some folders to a new destination
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
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
/A:-D explicitly excludes directories from being processed by DIR. In contrast, /A:D would only process directories.
Steffen
Steffen
-
- Posts: 34
- Joined: 17 Feb 2017 02:28
Re: copying the latest of some folders to a new destination
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
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
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
-
- Posts: 34
- Joined: 17 Feb 2017 02:28
Re: copying the latest of some folders to a new destination
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.
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
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
-
- Posts: 34
- Joined: 17 Feb 2017 02:28
Re: copying the latest of some folders to a new destination
That worked ! Thank you.
-
- Posts: 34
- Joined: 17 Feb 2017 02:28
Re: copying the latest of some folders to a new destination
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.
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
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
CopyNewerFiles.bat
Code: Select all
@echo off
set "source=C:\Your\Folder\From"
set "destination=C:\Your\Folder\To"
xcopy /d "%source%" "%destination%"
pause
-
- Posts: 34
- Joined: 17 Feb 2017 02:28
Re: copying the latest of some folders to a new destination
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.
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.