用于删除目录中旧的和不同的文件夹的批处理文件

发布于 2024-11-08 23:17:19 字数 394 浏览 0 评论 0原文

我想编写一个 DOS 批处理文件来删除目录中的旧文件夹,但保留最新的文件夹。以下是 D:\myfolders\ 下的文件夹示例:

Jack_20110507  (previous day time-stamped folder)
Jack_20110508  (current day time-stamped folder)
James_20110507
James_20110508 
Kenny_20110507
Kenny_20110508
...

我想删除所有前一天带时间戳的文件夹 *_20110507,但保留所有当天时间 -带标记的文件夹*_20110508每天都会创建新的带时间戳的文件夹

I want to write a DOS batch file to delete old folders in a directory but keep the newest folder. Here is a sample of my folders under D:\myfolders\:

Jack_20110507  (previous day time-stamped folder)
Jack_20110508  (current day time-stamped folder)
James_20110507
James_20110508 
Kenny_20110507
Kenny_20110508
...

I would like to delete all previous day time-stamped folders *_20110507, but keep all current day time-stamped folders *_20110508. New timestamped folders are created daily.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

大海や 2024-11-15 23:17:19

下面的脚本将获取 YYYYMMDD 中的当前本地日期(由此答案提供)。

然后,它循环遍历给定目录中的所有文件夹(在我的例子中,这是包含脚本的文件夹下的测试子文件夹)并检查文件夹名称的最后 8 个字符。如果它们与今天的日期不匹配,则会删除该文件夹。

/Q 禁用文件夹删除确认。如果您希望在删除文件夹之前收到确认提示,请将其删除。

@ECHO off & setlocal EnableDelayedExpansion

REM Get the local datetime
FOR /F "usebackq tokens=1,2 delims==" %%i IN (`wmic os get LocalDateTime /VALUE 2^>NUL`) DO IF '.%%i.'=='.LocalDateTime.' SET ldt=%%j

REM Set the datetime to YYYYMMDD format.
SET today=%ldt:~0,4%%ldt:~4,2%%ldt:~6,2%

REM For every file in the folder test:
FOR /D %%f IN (.\test\*) DO (
    REM Store the folder name temproarily
    SET folder=%%f
    REM Get just the ending date part of the folder name
    SET folderpart=!folder:~-8!
    REM If it doesn't match todays date, delete the folder.
    IF NOT !folderpart!==!today! RD /Q %%f
)
@ECHO ON

The script below will get the current local date in YYYYMMDD (courtesy of this answer).

It then loops through all the folders in a given directory (In my case this was a test sub folder beneath the folder containing the script) and checks the last 8 characters of the folder name. If they do not match today's date, it deletes the folder.

The /Q disables the confirmation for folder deletion. If you want to be prompted for confirmation before a folder is deleted, remove this.

@ECHO off & setlocal EnableDelayedExpansion

REM Get the local datetime
FOR /F "usebackq tokens=1,2 delims==" %%i IN (`wmic os get LocalDateTime /VALUE 2^>NUL`) DO IF '.%%i.'=='.LocalDateTime.' SET ldt=%%j

REM Set the datetime to YYYYMMDD format.
SET today=%ldt:~0,4%%ldt:~4,2%%ldt:~6,2%

REM For every file in the folder test:
FOR /D %%f IN (.\test\*) DO (
    REM Store the folder name temproarily
    SET folder=%%f
    REM Get just the ending date part of the folder name
    SET folderpart=!folder:~-8!
    REM If it doesn't match todays date, delete the folder.
    IF NOT !folderpart!==!today! RD /Q %%f
)
@ECHO ON
墨落成白 2024-11-15 23:17:19

除了最简单的事情之外,我不会使用批处理脚本。

我建议您查看脚本语言,例如 Python。

I wouldn't use a batch script for anything other than the very simplest stuff.

I recommend you look at a scripting language such as Python.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文