0

Dears,

I 'm facing some difficulties trying to create a batch script to open a specific excel file with variable name.

@ECHO OFF
for /f %%x in ('wmic path win32_localtime get /format:list ^| findstr "="') 
do set %%x
set today=%Year%%Month%%Day%
"C:\xxx\xxx\Desktop\FileName" + today-1 + ".xlsx"

The path includes two pieces: Fix piece :"C:\xxx\xxx\Desktop\FileName" Variable piece: yesterday's date in format "YYYYMMDD" without any separation between year,month or day.

thanks for your help!

1
  • 1
    You cannot do date math like that in batch files. Batch file know nothing of any other variable type. Everything is a string. You don't use + symbols either to concatenate things together. If you want the date minus one then call out to Powershell inside your FOR command instead of WMIC. Commented Oct 11, 2018 at 12:40

1 Answer 1

2

I'd use powershell as a tool to do the date calculation:

:: Q:\Test\2018\10\11\SO_52760062.cmd
@ECHO OFF
for /f "usebackq" %%A in (`
  powershell -NoP -C "(Get-Date).AddDays(-1).ToString('yyyyMMdd')"
`) Do Set Yesterday=%%A
echo "C:\xxx\xxx\Desktop\FileName%Yesterday%.xlsx"

Sample output:

> Q:\Test\2018\10\11\SO_52760062.cmd
"C:\xxx\xxx\Desktop\FileName20181010.xlsx"
Sign up to request clarification or add additional context in comments.

Comments

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.