Error 1004
Error 1004

Reputation: 8220

Open a file with Batch Script

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!

Upvotes: 0

Views: 308

Answers (1)

user6811411
user6811411

Reputation:

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"

Upvotes: 2

Related Questions