我正在嘗試編寫一個批次文件,將一組 excel 文件從 .xls 轉換為 .xlsx,以獲取我的月度報告。
這就是我目前所擁有的:
"C:\Program Files (x86)\Microsoft Office\Office14\excelcnv.exe" -oice "H:\File Path\2018\Support\03 March\Other\03 Export.xls" "H:\File Path\2018\Support\03 March\Other\03 Export.xlsx"
我希望能夠運行它而不必更改檔案路徑中的月份,它看起來像這樣:
H:\File Path\2018\Support\03 March\Other
我想這樣做,以便當我下個月運行這個時,它會自動將“4 月 4 日”放在 3 月 3 日的位置。如果我也可以在檔案路徑中填充 2018 年,那就太好了。這對於批次檔來說可能嗎?
謝謝!
答案1
:: Q:\Test\2018\04\12\SU1313056.cmd
@Echo off&SetLocal EnableExtensions EnableDelayedExpansion
:: Get date in a locale/user-settings independent format
for /f "tokens=1-3 delims=.+-" %%A in (
'wmic os get LocalDateTime^|findstr ^^[0-9]'
) do Set _DT=%%A
Set "yy=%_DT:~0,4%"&Set "MM=%_DT:~4,2%"&Set "dd=%_DT:~6,2%"
:: Build MonthName[01..12] array
Set i=100&Set "MonthName= January February March April May June July August September October November December"
Set "MonthName=%MonthName: ="&Set /a i+=1&Set "MonthName[!i:~-2!]=%"
:: Set MonthName
Set "Excel=C:\Program Files (x86)\Microsoft Office\Office14\excelcnv.exe"
Set "Src=H:\File Path\%yy%\Support\%MM% !MonthName[%MM%]!\Other\%MM% Export.xls"
Set "Dst=%Src%x"
:: Show command
Echo "%Excel%" -oice^^
Echo "%Src%" ^^
Echo "%Dst%"
If Not exist "%Excel%" (Echo Can't find "%Excel%" & Pause & Exit /B 1)
If Not exist "%Src%" (Echo Can't find "%Src%" & Pause & Exit /B 1)
::DoIt
"%Excel%" -oice "%Src%" "%Dst%"
範例輸出:
"C:\Program Files (x86)\Microsoft Office\Office14\excelcnv.exe" -oice^
"H:\檔案路徑\2018\Support\04 April\Other\04 Export.xls" ^
"H:\檔案路徑\2018\Support\04 April\Other\04 Export.xlsx」
找不到「C:\Program Files (x86)\Microsoft Office\Office14\excelcnv.exe」
找不到「H:\File Path\2018」 \ Support\04 April\Other\04 Export.xls"