如何编写批处理文件以按时删除文件夹和文件?

时间:2021-06-07 02:23:14

Exact Duplicates:

I want write a batch file to delete folders, subfolders and temporary files on time basis.

我想写一个批处理文件来按时删除文件夹,子文件夹和临时文件。

The conditions are like this:

条件是这样的:

  • I need to delete the file from folder one hour before to current running time.
  • 我需要在文件夹前一小时将文件删除到当前运行时间。

  • I want to get the current time from system and delete the one hour before files
  • 我想从系统获取当前时间并删除文件前一小时

  • The file format is like this: <file name> <date>
  • 文件格式如下: <文件名> <日期>

  • The time format is dd-mm-yyyy-hh-mm or dd-mm-yy-hh-mm
  • 时间格式为dd-mm-yyyy-hh-mm或dd-mm-yy-hh-mm

2 个解决方案

#1


Batch can be surprisingly powerful but I don't think the kind of date manipulation you want will be practical.

批量可以令人惊讶地强大,但我不认为你想要的那种日期操作是实用的。

You can use FOR to split the date into elements and then use SET /A to do the subtraction, but then you're going to need a huge number of IF and GOTO statements to handle cases like subtracting an hour from half past midnight on the 1st of January.

您可以使用FOR将日期拆分为元素,然后使用SET / A进行减法,但是您需要大量的IF和GOTO语句来处理从午夜半夜减去一小时的情况。 1月1日。

I think you'd be better off investigating VBS or Powershell.

我认为你最好调查VBS或Powershell。

#2


You may want to try this script to delete files from a folder that are older than 1 hour.

您可能希望尝试此脚本从文件夹中删除超过1小时的文件。

# Script Delete1Hour.txt
var str folder, list, file
cd $folder
lf -n "*" $folder ($ftype=="f") AND ( $fmtime < addtime(diff("-10000")) ) > $list
while ($list <> "")
do
    lex "1" $list > $file
    system del ("\""+$file+"\"")
done

This is biterscripting. The important command is

这是biterscripting。重要的命令是

lf -n "*" $folder ($ftype=="f") AND ( $fmtime < addtime(diff("-10000")) )

Which tells biterscripting - give me all entries in folder $folder with names that match the patten "*" whose file type is "f" (flat file, not directory), and whose file modification time ($fmtime) is earlier than 1 hour. See the documentation for the lf command at http://www.biterscripting.com/helppages/lf.html .

这告诉biterscripting - 给我文件夹$ folder中的所有条目,其名称与文件类型为“f”(平面文件,而不是目录)的模式“*”匹配,并且其文件修改时间($ fmtime)早于1小时。请参阅http://www.biterscripting.com/helppages/lf.html上的lf命令的文档。

Save the script in file C:/Scripts/Delete1Hour.txt, run it as

将脚本保存在文件C:/Scripts/Delete1Hour.txt中,运行它

script  "C:/Scripts/Delete1Hour.txt" folder("C:/testfolder")

It will delete all files from C:/testfolder that are older than 1 hour.

它将删除C:/ testfolder中超过1小时的所有文件。

#1


Batch can be surprisingly powerful but I don't think the kind of date manipulation you want will be practical.

批量可以令人惊讶地强大,但我不认为你想要的那种日期操作是实用的。

You can use FOR to split the date into elements and then use SET /A to do the subtraction, but then you're going to need a huge number of IF and GOTO statements to handle cases like subtracting an hour from half past midnight on the 1st of January.

您可以使用FOR将日期拆分为元素,然后使用SET / A进行减法,但是您需要大量的IF和GOTO语句来处理从午夜半夜减去一小时的情况。 1月1日。

I think you'd be better off investigating VBS or Powershell.

我认为你最好调查VBS或Powershell。

#2


You may want to try this script to delete files from a folder that are older than 1 hour.

您可能希望尝试此脚本从文件夹中删除超过1小时的文件。

# Script Delete1Hour.txt
var str folder, list, file
cd $folder
lf -n "*" $folder ($ftype=="f") AND ( $fmtime < addtime(diff("-10000")) ) > $list
while ($list <> "")
do
    lex "1" $list > $file
    system del ("\""+$file+"\"")
done

This is biterscripting. The important command is

这是biterscripting。重要的命令是

lf -n "*" $folder ($ftype=="f") AND ( $fmtime < addtime(diff("-10000")) )

Which tells biterscripting - give me all entries in folder $folder with names that match the patten "*" whose file type is "f" (flat file, not directory), and whose file modification time ($fmtime) is earlier than 1 hour. See the documentation for the lf command at http://www.biterscripting.com/helppages/lf.html .

这告诉biterscripting - 给我文件夹$ folder中的所有条目,其名称与文件类型为“f”(平面文件,而不是目录)的模式“*”匹配,并且其文件修改时间($ fmtime)早于1小时。请参阅http://www.biterscripting.com/helppages/lf.html上的lf命令的文档。

Save the script in file C:/Scripts/Delete1Hour.txt, run it as

将脚本保存在文件C:/Scripts/Delete1Hour.txt中,运行它

script  "C:/Scripts/Delete1Hour.txt" folder("C:/testfolder")

It will delete all files from C:/testfolder that are older than 1 hour.

它将删除C:/ testfolder中超过1小时的所有文件。