0

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
greenage
  • 399
  • 3
  • 13
  • I was hoping this would work but it doesn't appear to be taking the date variable: – greenage Aug 07 '17 at 15:38
  • `@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%\*.* /A:-D /O:-D /B') DO COPY %source%\"%%I" %target% & echo %%I & GOTO :END :END TIMEOUT 4` – greenage Aug 07 '17 at 15:39
  • 1
    Don't put code in comments - edit your question. –  Aug 07 '17 at 16:34
  • Apologies, question edited. – greenage Aug 08 '17 at 10:01
  • That could work if you put the commands in the right order (set todaysdate first). –  Aug 08 '17 at 10:19
  • Thanks, I've reworked it but it's still ignoring the folder named "todaysdate" and copying the newest file within the "test case" folder. – greenage Aug 08 '17 at 11:30
  • are the vars `yyyy`,`mm` and `dd` set prior running the batch? –  Aug 08 '17 at 11:32
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/151379/discussion-between-greenage-and-lotpings). – greenage Aug 08 '17 at 11:38

1 Answers1

1
@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.

  • I've ran this from an open cmd prompt and now I can read the following error: No batch label specified to GOTO command. – greenage Aug 08 '17 at 13:13
  • Well that is an error I took over from your code and didn't discover, answer changed. AT one point the :end has swapped to the next line wrongly. I hope the debugging rules make sense now for you ;-) –  Aug 08 '17 at 13:17
  • It does thank you! Moving END to the end of the previous line still had the same output, changing it to EOF has eliminated the error, but "The system cannot find the the file specified. %I" issue still remains. I'll update the question with the most recent version. – greenage Aug 08 '17 at 13:25
  • My apollogies, my powershell command had missing/unescaped quotes. –  Aug 08 '17 at 13:36
  • No need to apologize :) That has changed things, I now get this: The system cannot find the path specified. NTUSER.DAT, is it now looking in another folder elsewhere somehow? – greenage Aug 08 '17 at 13:38
  • Fantastic! Works perfectly! Thank you so so much! – greenage Aug 08 '17 at 14:25