我正在尝试创建一个简单的批处理脚本,以便在任何 Windows PC 上备份我的驱动程序。如果我在 cmd 中逐行输入脚本,它可以正常工作,但是当我将其作为批处理脚本运行时,它会给出错误“目标文件夹或文件 ......不存在”。但事实并非如此。这是我的脚本:
@echo off
SETLOCAL EnableExtensions EnableDelayedExpansion
set timestamp=%date%-%time:~0,2%-%time:~3,2%-%time:~6,2%
set folder="C:\Users\%Username%\Desktop\backups\drivers_%timestamp%"
mkdir %folder%
DISM /online /export-driver /destination:%folder%
答案1
@echo off && setlocal
for /f ^eol^=^.^ ^usebackq^ ^tokens^=^4^ ^delims^=^<^>^. %%i in =;(`
wmic.exe os get LocalDateTime /format:xml ^| find.exe "VALUE"
`);= do set "_dt=%%~i" && set "_time=%time:~6,2%"
set "_timestamp=%_dt:~0,4%-%_dt:~4,2%-%_dt:~6,2% %_dt:~8,2%-%_dt:~10,2%-%_time%"
set "_folder="C:\Users\%UserName%\Desktop\backups\drivers_%_timestamp%""
mkdir %_folder%
DISM /online /export-driver /destination:%_folder%
endlocal
- 结果:
mkdir "C:\Users\ecker\Desktop\backups\drivers_2023-09-04 06-54-12"
DISM /online /export-driver /destination:"C:\Users\ecker\Desktop\backups\drivers_2023-09-04 06-54-12"
您的错误是指文件夹名称布局中存在的字符
,确切地说是/"/"
中的字符。%timestamp%
%folder%
如何以合适的格式在 Windows 命令行上获取当前日期时间?
Windows 和 Linux 目录名称中禁止使用哪些字符
观察:如果名称中使用数字顺序,则可以获得文件夹中项目的最佳列表,并按升序或降序排序:
set "_folder="C:\Users\%UserName%\Desktop\backups\drivers_%_timestamp%""
...\drivers_2023-09-04 06-54-12
...\drivers_2023-08-31 21-01-08
...\drivers_2023-07-03 01-12-11
...
:: ... or
set "_folder="C:\Users\%UserName%\Desktop\backups\%_timestamp%_drivers""
...\2023-09-04 06-54-12_drivers
...\2023-08-31 21-01-08_drivers
...\2023-07-03 01-12-11_drivers