日期公式用于更新批处理文件中的文件路径

日期公式用于更新批处理文件中的文件路径

我正在尝试编写一个批处理文件,将一组 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

我希望当我下个月运行这个程序时,它会自动将“04 April”放到 03 March 的位置。如果我也可以在文件路径中填充 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:\File Path\2018\Support\04 April\Other\04 Export.xls” ^
“H:\File Path\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”

相关内容