Reputation: 397
I found a script which copies the newest file from a directory into another directory and renames it to a static name, this works will.
I'm looking to amend it to cater for files that sit within folders that that are created daily.
So the folder structure is:
C:\Logs\Check\20170806
C:\Logs\Check\20170807 etc
The script reads:
@echo off
set source="C:\test case"
set target="C:\Users\sam.green\Desktop\Random Folder\output.txt"
FOR /F "delims=" %%I IN ('DIR %source%\*.* /A:-D /O:-D /B') DO COPY %source%\"%%I" %target% & echo %%I & GOTO :END
:END
TIMEOUT 4
Is there a way to get the script to look in today's folder and take the newest file and copy it to the static location and filename?
I think adding this (but without the time) may help but unsure how to add it into the script:
set d=%t:~10,4%%t:~7,2%%t:~4,2%_%t:~15,2%%t:~18,2%%t:~21,2%
I was thinking something like this would work:
@echo off
set source="C:\test case\%todaysdate%\
set target="C:\Users\sam.green\Desktop\Random Folder\output.txt"
set todaysdate="%yyyy%%mm%%dd%"
FOR /F "delims=" %%I IN ('DIR %source%%todaysdates%\*.* /A:-D /O:-D /B') DO COPY %source%\"%%I" %target% & echo %%I & GOTO :END
:END
TIMEOUT 4
Update, so I've tried this but it's still copying the newest file that sits in "test case" folder:
@echo off
set todaysdate="%yyyy%%mm%%dd%"
set source="C:\test case\%todaysdate%"
set target="C:\Users\sam.green\Desktop\Random Folder\output.txt"
FOR /F "delims=" %%I IN ('DIR %source%\%todaysdate%*.* /A:-D /O:-D /B') DO COPY %source%\%todaysdate%"%%I" %target% & echo %%I & GOTO :END
:END
TIMEOUT 4
I've also tried this after realizing %todaysdate% was specified in both the variable and body of the code:
@echo off
set todaysdate="%yyyy%%mm%%dd%"
set source="C:\test case\"
set target="C:\Users\sam.green\Desktop\Random Folder\output.txt"
FOR /F "delims=" %%I IN ('DIR %source%\%todaysdate%*.* /A:-D /O:-D /B') DO COPY %source%\%todaysdate%"%%I" %target% & echo %%I & GOTO :END
:END
TIMEOUT 4
Latest version of the code:
@echo off
for /f %%a in ('powershell -NoP -C "get-date -f "yyyyMMdd"') Do Set today=%%A
set "source=C:\test case\%today%"
set "target=C:\Users\sam.green\Desktop\Random Folder\output.txt"
Pushd "%source%"
FOR /F "delims=" %%I IN ('DIR "*.*" /A:-D /O:-D /B') DO COPY "%%I" "%target%" & echo %%I & GOTO :EOF
Popd
TIMEOUT 10
Upvotes: 0
Views: 383
Reputation:
@echo off
for /f %%A in ('powershell -NoP -C "get-date -f \"yyyyMMdd\""') Do Set today=%%A
set "source=C:\test case\%today%"
set "target=C:\Users\sam.green\Desktop\Random Folder\output.txt"
Pushd "%source%" || (Echo Dir %source% not found&goto :END)
FOR /F "delims=" %%I IN ('DIR /B/A-D/O-D * 2^>Nul') DO (
COPY /Y "%%I" "%target%" >Nul && echo copied %%~fI to %target%
GOTO :END
)
:END
Popd
TIMEOUT 10
Edit did a test on my local ramdisk - should work now.
Upvotes: 1