windows 批处理脚本定时删除指定文件夹、文件

1.删除文件夹:rd /S /Q (rd /? 可以查看命令参数)

@echo off
rem 删除文件夹
set logPath=D:\tool\DelHisKLineData\log
set dateSuffix=%DATE:~0,4%%DATE:~5,2%%DATE:~8,2%

rem FCT_INDEX_2
rem sh
set klinepath=D:\Server\FCT_INDEX_2\hqservice\data\sh
rd /S /Q %klinepath%\min1kline >> %logPath%\DelHisKLine_%dateSuffix%.txt
rd /S /Q %klinepath%\min5kline >> %logPath%\DelHisKLine_%dateSuffix%.txt
rd /S /Q %klinepath%\min15kline >> %logPath%\DelHisKLine_%dateSuffix%.txt
rd /S /Q %klinepath%\min30kline >> %logPath%\DelHisKLine_%dateSuffix%.txt
rd /S /Q %klinepath%\min60kline >> %logPath%\DelHisKLine_%dateSuffix%.txt

rem sz
set klinepath=D:\Server\FCT_INDEX_2\hqservice\data\sz
rd /S /Q %klinepath%\min1kline >> %logPath%\DelHisKLine_%dateSuffix%.txt
rd /S /Q %klinepath%\min5kline >> %logPath%\DelHisKLine_%dateSuffix%.txt
rd /S /Q %klinepath%\min15kline >> %logPath%\DelHisKLine_%dateSuffix%.txt
rd /S /Q %klinepath%\min30kline >> %logPath%\DelHisKLine_%dateSuffix%.txt
rd /S /Q %klinepath%\min60kline >> %logPath%\DelHisKLine_%dateSuffix%.txt


rem 删除文件夹
rem FCT_INDEX
rem sh
set klinepath=D:\Server\FCT_INDEX\hqservice\data\sh
rd /S /Q %klinepath%\min1kline >> %logPath%\DelHisKLine_%dateSuffix%.txt
rd /S /Q %klinepath%\min5kline >> %logPath%\DelHisKLine_%dateSuffix%.txt
rd /S /Q %klinepath%\min15kline >> %logPath%\DelHisKLine_%dateSuffix%.txt
rd /S /Q %klinepath%\min30kline >> %logPath%\DelHisKLine_%dateSuffix%.txt
rd /S /Q %klinepath%\min60kline >> %logPath%\DelHisKLine_%dateSuffix%.txt

rem sz
set klinepath=D:\Server\FCT_INDEX\hqservice\data\sz
rd /S /Q %klinepath%\min1kline >> %logPath%\DelHisKLine_%dateSuffix%.txt
rd /S /Q %klinepath%\min5kline >> %logPath%\DelHisKLine_%dateSuffix%.txt
rd /S /Q %klinepath%\min15kline >> %logPath%\DelHisKLine_%dateSuffix%.txt
rd /S /Q %klinepath%\min30kline >> %logPath%\DelHisKLine_%dateSuffix%.txt
rd /S /Q %klinepath%\min60kline >> %logPath%\DelHisKLine_%dateSuffix%.txt

2.删除指定文件:del /F /S /Q

rem 删除文件
rem 查看del命令的参数:del /?
@echo off
setlocal enabledelayedexpansion
rem 变量延迟

set logPath=D:\tool\DelLog\log
set path=D:\app\taf\app_log\IndicatorSys\FormulaCalcServer
set dateSuffix=%DATE:~0,4%%DATE:~5,2%%DATE:~8,2%

for /r %path% %%i in (*.*) do (
	rem echo 完整路径: %%i
	rem echo 无后缀路径: %%~dpni
	rem echo 所在目录: %%~dpi
	rem echo 完整文件名: %%~nxi
	rem echo 不带后缀文件名: %%~ni
	rem echo 文件后缀: %%~xi
	set filename=%%~nxi
	set filetmp=%%~ni
	echo 文件日期后缀: !filetmp:~-8,8!
	set datetmp=!filetmp:~-8,8!
	if /I !datetmp! LSS %dateSuffix% (
		echo !datetmp!
		echo:
		del /F /S /Q %path%\!filename! >> %logPath%\delLog_%dateSuffix%.txt
	) else (
		echo GEQ
		echo:
	)
)

rem 右括号和左括号要和else在同行,else两边需要有空格

 setlocal enabledelayedexpansion的作用参见:https://www.cnblogs.com/ydhliphonedev/archive/2012/09/25/2702092.html

参见:https://www.yiibai.com/batch_script/batch_script_operators.html

 

posted @ 2019-11-28 14:14  那一剑的風情  阅读(9443)  评论(0编辑  收藏  举报