China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-07 12:11
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Seek to delete files based on time: delete files with a creation date greater than 1 day View 1,630 Replies 13
Original Poster Posted 2008-02-10 03:11 ·  中国 云南 昆明 电信
初级用户
★★
Credits 130
Posts 59
Joined 2007-12-25 20:32
18-year member
UID 106775
Gender Male
Status Offline
As the title says: I want to delete files in a certain disk whose creation date is more than one day. ... As long as the files are more than one day, delete them regardless of the format...

Can someone make a batch file for me to use??

───────────────── Moderation Record ─────────────────
Performed by: HAT
Operation: Added search keywords to the post title
Description: The original title "Ask for a batch file to delete files according to time" is not conducive to forum search
───────────────── Moderation Record ─────────────────


[ Last edited by HAT on 2008-9-27 at 10:20 ]
Floor 2 Posted 2008-02-10 03:33 ·  中国 江苏 常州 溧阳市 电信
银牌会员
★★★
Credits 2,404
Posts 946
Joined 2005-09-08 13:44
20-year member
UID 42345
Status Offline
A few days ago I posted on the forum saying the test was not successful, you take a look

@echo off
SET/p TT=Enter the number of days:
SET/p PH=Enter the path:
set p1=%date:~0,4%
set p2=%date:~5,2%
set p3=%date:~8,2%
setlocal enabledelayedexpansion
for /l %%a in (1,1,3) do (
for /f "delims=0 tokens=*" %%i in ("!p%%a!") do set p%%a=%%i
)
if %p3% gtr %TT% goto ppp
:nxt
set/a p2-=1
if %p2% equ 0 set/a p1-=1&&set p2=12
SET/a PN1=%p1%%%4
set PN2=28
if %PN1% EQU 0 set pN2=29
for /f "tokens=%p2%" %%i in ("31 %pN2% 31 30 31 30 31 31 30 31 30 31") do set/a p3=%p3%+%%i
if %p3% leq %tt% goto nxt
:ppp
set/a p3=%p3%-%tt%
set p2=0%p2%
set p2=%p2:~-2,2%
set p3=0%p3%
set p3=%p3:~-2,2%
for %%i in ("%PH%\*") do set t=%%~ti &&call :loop "%%~fi"
pause
goto :eof
:loop
if not "%~1" == "%~f0" (
IF %p1%%p2%%p3% gtr %t:~0,4%%t:~5,2%%t:~8,2% echo del "%~1"
)
简单!简单!再简单!
Floor 3 Posted 2008-02-11 00:11 ·  中国 云南 昆明 电信
初级用户
★★
Credits 130
Posts 59
Joined 2007-12-25 20:32
18-year member
UID 106775
Gender Male
Status Offline
No, it can be displayed to delete, but it can't be deleted
Floor 4 Posted 2008-02-11 01:51 ·  中国 江苏 常州 溧阳市 电信
银牌会员
★★★
Credits 2,404
Posts 946
Joined 2005-09-08 13:44
20-year member
UID 42345
Status Offline
Originally posted by yhshiro at 2008-2-11 00:11:
It doesn't work. It can display deletion, but can't delete.

Change echo del "%~1" to del "%~1"
简单!简单!再简单!
Floor 5 Posted 2008-02-11 20:27 ·  中国 云南 昆明 电信
初级用户
★★
Credits 130
Posts 59
Joined 2007-12-25 20:32
18-year member
UID 106775
Gender Male
Status Offline
Still not working....Now it doesn't even display, it's completely done./.
Floor 6 Posted 2008-02-11 20:40 ·  中国 江苏 常州 电信
银牌会员
★★★
Credits 2,404
Posts 946
Joined 2005-09-08 13:44
20-year member
UID 42345
Status Offline
What content was displayed when it could be shown before? Can you post it?
简单!简单!再简单!
Floor 7 Posted 2008-02-11 21:02 ·  中国 云南 昆明 电信
初级用户
★★
Credits 130
Posts 59
Joined 2007-12-25 20:32
18-year member
UID 106775
Gender Male
Status Offline
It shows that the file name to be deleted

del "f:\XXXXXXX.XX"
That's the kind of content
Floor 8 Posted 2008-02-11 21:05 ·  中国 云南 昆明 电信
初级用户
★★
Credits 130
Posts 59
Joined 2007-12-25 20:32
18-year member
UID 106775
Gender Male
Status Offline
Now it's okay, you can use... But it can only delete files under the specified drive, not files inside folders
Floor 9 Posted 2008-02-11 21:17 ·  中国 江苏 常州 电信
银牌会员
★★★
Credits 2,404
Posts 946
Joined 2005-09-08 13:44
20-year member
UID 42345
Status Offline
How about this?

Originally posted by yhshiro at 2008-2-11 21:05:
Now it's okay, can be used...
But it can only delete files in the specified disk, cannot delete files in the folder

@echo off
:st
cls
SET/p TT=Enter the number of days:
SET/p PH=Enter the path:
if "%tt%" =="" echo The number of days cannot be empty&pause&goto st
if "%ph%" =="" echo The path cannot be empty &pause&goto st
echo %tt%|findstr "^*$">nul&&goto ys||echo The number of days can only be 1-9&pause&goto st
:ys
set p1=%date:~0,4%
set p2=%date:~5,2%
set p3=%date:~8,2%
setlocal enabledelayedexpansion
for /l %%a in (1,1,3) do (
for /f "delims=0 tokens=*" %%i in ("!p%%a!") do set p%%a=%%i
)
if %p3% gtr %TT% goto ppp
:nxt
set/a p2-=1
if %p2% equ 0 set/a p1-=1&&set p2=12
SET/a PN1=%p1%%%4
set PN2=28
if %PN1% EQU 0 set pN2=29
for /f "tokens=%p2%" %%i in ("31 %pN2% 31 30 31 30 31 31 30 31 30 31") do set/a p3=%p3%+%%i
if %p3% leq %tt% goto nxt
:ppp
set/a p3=%p3%-%tt%
set p2=0%p2%
set p2=%p2:~-2,2%
set p3=0%p3%
set p3=%p3:~-2,2%
for /r "%ph%" %%i in (*.*) do call :loop "%%~fi"
pause
goto :eof
:loop
if not "%~1" == "%~f0" (
IF %p1%%p2%%p3% gtr %t:~0,4%%t:~5,2%%t:~8,2% del %1
)

[ Last edited by terse on 2008-2-11 at 09:28 PM ]
简单!简单!再简单!
Floor 10 Posted 2008-02-11 21:26 ·  中国 云南 昆明 电信
初级用户
★★
Credits 130
Posts 59
Joined 2007-12-25 20:32
18-year member
UID 106775
Gender Male
Status Offline
Please provide the batch script code for deleting all files in the f:\ directory that are older than 1 day, including those in subfolders. The following is a simple batch script to achieve this function:

```batch
@echo off
forfiles /p "f:\" /s /m *.* /d -1 /c "cmd /c del /q @path"
```

Explanation:
- `forfiles`: A command used to find files that meet specific time conditions.
- `/p "f:\"`: Specify the target directory as the f drive root directory.
- `/s`: Recursively search subdirectories.
- `/m *.*`: Match all files.
- `/d -1`: Find files older than 1 day.
- `/c "cmd /c del /q @path"`: For each found file, execute the `del /q` command to delete the file quietly.
Floor 11 Posted 2008-02-11 21:44 ·  中国 云南 昆明 电信
初级用户
★★
Credits 130
Posts 59
Joined 2007-12-25 20:32
18-year member
UID 106775
Gender Male
Status Offline
The filename, directory name, or volume label syntax is incorrect.

意思是:文件名、目录名或卷标语法不正确。
Floor 12 Posted 2008-02-11 21:45 ·  中国 江苏 常州 电信
银牌会员
★★★
Credits 2,404
Posts 946
Joined 2005-09-08 13:44
20-year member
UID 42345
Status Offline
@echo off
SET TT=1
set p1=%date:~0,4%
set p2=%date:~5,2%
set p3=%date:~8,2%
setlocal enabledelayedexpansion
for /l %%a in (1,1,3) do (
for /f "delims=0 tokens=*" %%i in ("!p%%a!") do set p%%a=%%i
)
if %p3% gtr %TT% goto ppp
:nxt
set/a p2-=1
if %p2% equ 0 set/a p1-=1&&set p2=12
SET/a PN1=%p1%%%4
set PN2=28
if %PN1% EQU 0 set pN2=29
for /f "tokens=%p2%" %%i in ("31 %pN2% 31 30 31 30 31 31 30 31 30 31") do set/a p3=%p3%+%%i
if %p3% leq %tt% goto nxt
:ppp
set/a p3=%p3%-%tt%
set p2=0%p2%
set p2=%p2:~-2,2%
set p3=0%p3%
set p3=%p3:~-2,2%
for /r "f:\" %%i in (*.*) do set t=%%~ti &&call :loop "%%~fi"
pause
goto :eof
:loop
if not "%~1" == "%~f0" (
IF %p1%%p2%%p3% gtr %t:~0,4%%t:~5,2%%t:~8,2% del %1
)
简单!简单!再简单!
Floor 13 Posted 2008-02-11 21:48 ·  中国 云南 昆明 电信
初级用户
★★
Credits 130
Posts 59
Joined 2007-12-25 20:32
18-year member
UID 106775
Gender Male
Status Offline
Thanks, it's already working~~~
Thanks a lot
Floor 14 Posted 2008-02-12 18:18 ·  中国 陕西 汉中 电信
初级用户
Credits 114
Posts 36
Joined 2007-12-22 17:42
18-year member
UID 106486
Gender Male
Status Offline
Use for to get the date and file name of dir, judge, delete the ones that should be deleted with del file name, and pass the ones that should not be deleted.
Forum Jump: