为了切换到位于不同的驱动器上的目录,你需要使用cd /d
,而不只是cd
。如果你不这样做,目录不会改变。
当您通过双击脚本运行脚本时,批处理将当前目录视为脚本当前所在的目录。由于您没有使用/d
选项和cd
,因此您正在脚本所在的目录上运行del /Q *.*
。
为了解决这个问题,你有两个选择:
cd /d "D:\ACT\ACTBKUP"
del /Q *.*
或
del /Q "D:\ACT\ACTBKUP"
有一个在
forfiles
没有选项得到的只是最近的文件;
/D +1
将返回所有文件的上次修改日期为今天或以后。为了得到最新的文件,并没有别的,你将需要一个
for /f
循环:
rem /b returns just the file name and /o:d sorts the files by date
rem Since newest_file gets set for each .zip file in the directory,
rem the last file set will be the newest
for /f "delims=" %%A in ('dir /b /o:d E:\ACT_Backups\*.zip') do set newest_file=%%A
copy %newest_file% D:\ACT\ACTBKUP
哪里是脚本位于何处? – SomethingDark
该脚本位于桌面上 –
而Windows安装在C:\驱动器上,对吗? – SomethingDark