1

I am trying to create a batch file to find the newest file in a directory. I am using the following commands in the batch:

call cd\
call d:
call cd "D:\OMS\Outbound\"
call for /f "tokens=* delims= " %%G in ('dir /b /od') do set newest=%%G
call pause

I am getting the error G was unexpected at this time.

Also I would like to ftp the newest file and will have to append "local:D:\OMS\Outbound\" before %%G in the following manner newest=local:D:\OMS\Outbound\%G. The code works just fine when run from command prompt but the batch file is not working.

1
  • first, remove all heading calls in every line. Commented Jun 19, 2013 at 9:47

1 Answer 1

3
cd /d "D:\OMS\Outbound"
for /f "delims=" %%G in ('dir /b /od') do set "newest=local:D:\OMS\Outbound\%%~G"
echo %newest%
pause

and remove other lines.

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks @Endoro. It worked. Could you please tell me why my commands did not give the required results?
there is no need for call, see also call /? on the command line.
add /a-d to the DIR switches to stop any directory names being included.
That's ok. tokens= is most valuable when splitting up a string into sections. "delims=" takes the entire line. The ~ in %%~G is not needed here but it doesn't cause any issue. It's used to remove any surrounding double quotes on the variable.
You had conflicting commands with tokens=* and delims= ". While it may work, and only for filenames with no leading spaces, it was confusing syntax. Tokens=* strips leading whitespace.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.