📄 删除指定天数之前的文件.txt
字号:
今天有个网友说他公司里的监控录像软件自带的删除功能没用了,但是它要定时删除那些n天前的文件,因为不及时删除的话,硬盘空间会不够,所以我就用批处理结合vbs脚本帮他写了一个。
下面的代码复制下来之后,保存成.bat文件,也就是批处理文件,然后把这个批处理保存到要删除文件的那个目录下,然后双击运行就可以删除指定类型的文件了。
懂得命令的人可以根据我这个批处理,把它修改成自己需要的代码。
@echo off
title 忆林子--删除指定天数之前的文件
color 0a
set listFile=忆林子
set vbsFile=忆林子.vbs
CALL :delTempFile
echo ===================================
echo.
echo 例如:如果要dat为后缀的文件的话
echo 请输入dat
echo.
echo ===================================
set /p inputFileType=[请输入后缀]
dir *.%inputFileType% /a/o-n/b>>%listFile% 2>nul
cls
echo ===================================
echo.
echo 例如:如果要删除3天之前的%inputFileType%
echo 文件的话,请输入3
echo.
echo 注意:删除到现在为止的所有文件的话
echo 请输入0
echo.
echo ===================================
set /p inputDays=[请输入天数]
CALL :createVBSFile >> %vbsFile%
%vbsFile% %inputDays% "%~dp0" "%listFile%"
CALL :delTempFile
echo 操作结束,请按任意键退出该批处理...
pause>nul
:delTempFile
del %listFile% /q 1>nul 2>nul
del %vbsFile% /q 1>nul 2>nul
goto :eof
:createVBSFile
echo Dim MyArray,strResult,filePath,days,listFile
echo days = WScript.Arguments.Unnamed.Item^(0^)
echo filePath = WScript.Arguments.Unnamed.Item^(1^)
echo listFile = WScript.Arguments.Unnamed.Item^(2^)
echo strResult = ""
echo MyArray = Split^(readFile^(listFile^), vbCrlf, -1^)
echo for i = 0 to UBOUND^(MyArray^)-1
echo strResult = getGreateTime^(filePath ^& MyArray^(i^)^)
echo IF DateDiff^("d", Now, strResult^) ^<= Int^(-days^) Then
echo del^(filePath ^& MyArray^(i^)^)
echo end if
echo NEXT
echo '---------------------------------------
echo '功能:读取文本里的所有文本
echo '用法:readFile^("c:\test.txt"^)
echo '---------------------------------------
echo Function readFile^(filename^)
echo Dim fso, ts, s
echo Const ForReading = 1
echo Set fso = CreateObject^("Scripting.FileSystemObject"^)
echo ' 读取文件内容。
echo Set ts = fso.OpenTextFile^(filename, ForReading^)
echo s = ts.ReadAll
echo ts.Close
echo readFile = s
echo End Function
echo '---------------------------------------
echo '功能:删除一个文件
echo '用法:del^("c:\windows\system32\cmd.exe"^)
echo '---------------------------------------
echo Sub del^(filespec^)
echo Dim fso
echo Set fso = CreateObject^("Scripting.FileSystemObject"^)
echo fso.DeleteFile^(filespec^)
echo End Sub
echo '---------------------------------------
echo '功能:取得一个文件的建立时间
echo '用法:getGreateTime^("c:\windows\system32\cmd.exe"^)
echo '---------------------------------------
echo Function getGreateTime^(filespec^)
echo Dim fso, f
echo Set fso = CreateObject^("Scripting.FileSystemObject"^)
echo Set f = fso.GetFile^(filespec^)
echo getGreateTime = f.DateCreated
echo End Function
goto :eof
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -