0

My goal is to copy the latest file (based on modified date) in Windows to a different folder, and then rename the file to something different. My last line that is commented out was just a test to see that the copy command was working. Feel free to use whatever is installed by default in Windows.

Here's what I have so far:

cd C:\Users\Mac\Desktop\old
FOR /f %%i IN ('dir /o:-d /b') DO (set LAST=%%i goto stop)
:stop
copy /Y %LAST% C:\Users\Mac\Desktop\new\current.csv
# copy C:\Users\Mac\Desktop\old\a1.txt C:\Users\Mac\Desktop\new\current.csv
MacGyver
  • 1,864
  • 7
  • 37
  • 50
  • Why not use Powershell? Something like `"gci path | sort LastWriteTime | select -last 1 | select-object name"` to start with, then put that in a variable and continue on with your script to copy it out somewhere and then rename it. – TheCleaner Nov 18 '15 at 15:28
  • You can use whatever language is installed in Windows by default. – MacGyver Nov 18 '15 at 15:33
  • But are you asking a question or just telling people how to do this with DOS commands? I haven't run your script to test...so I'm not sure if you are asking a question and if so what it is. – TheCleaner Nov 18 '15 at 15:34
  • I have not figured it out with cmd nor with Powershell. So that is why I'm asking the question. It really doesn't matter which language I do it in. I'm just trying to build a little bit of automation into an ETL process. One of our vendors isn't consistent with how they name the file, but we get a file once a month. So this will just check for the latest *csv file. – MacGyver Nov 18 '15 at 15:37
  • Gotcha. Please edit your question then to make it more than a "gimme the codez" question. Include errors, what is line/char is failing, etc. But it may be better over on SO. Not sure. – TheCleaner Nov 18 '15 at 15:40
  • There aren't any errors that I can see. In cmd, it usually works or it doesn't. – MacGyver Nov 18 '15 at 15:40

1 Answers1

0

This is the closest I got. Good enough for now. If anyone knows how to search for JUST *.csv files in the loop, let me know and I'll make your answer the correct answer. This seems to work though.

@echo off

setlocal

set "src=C:\Users\Mac\Desktop\old"
set "dst=C:\Users\Mac\Desktop\new"

pushd "%src%"
for /f "delims=" %%f in ('dir /b /a:-d /o:-d') do (
  del "%dst%\current.csv"
  copy "%%~f" "%dst%\current.csv"
  goto next
)

:next
popd
MacGyver
  • 1,864
  • 7
  • 37
  • 50