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-06-28 11:53
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Help] Automatic Backup and Deletion of Files View 9,976 Replies 25
Original Poster Posted 2006-10-19 05:55 ·  中国 辽宁 大连 联通
新手上路
Credits 10
Posts 4
Joined 2006-10-19 04:01
19-year member
UID 66708
Gender Male
Status Offline
In work, I often encounter customers asking to automatically back up files. Usually, I use software to complete file backups. General databases can usually perform backups by themselves or be done by DBAs, so there are relatively few batch processing issues.

But now there is a project where all files (except databases) need to be backed up regularly, and according to time, only a specified number (such as 5 copies) is retained. Such backups are easy to handle, but deletion is difficult. I sincerely ask experts to give a complete solution. I am very grateful. Maybe others will use it in the future, which is beneficial to both others and myself.

Thanks
Floor 2 Posted 2006-10-19 05:56 ·  中国 辽宁 大连 联通
新手上路
Credits 10
Posts 4
Joined 2006-10-19 04:01
19-year member
UID 66708
Gender Male
Status Offline
This is very similar to the automatic backup of the Windows registry
Floor 3 Posted 2006-10-19 06:10 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
"Also, it deletes according to time, keeping only the agreed number". Based on what time? The file creation time? The file's last modification time? Or the time a few days ago from the current time?
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 4 Posted 2006-10-19 06:12 ·  中国 辽宁 大连 联通
新手上路
Credits 10
Posts 4
Joined 2006-10-19 04:01
19-year member
UID 66708
Gender Male
Status Offline
The time when the file was created. The backup file is not modified, just to ensure that it can be restored and used later.
Floor 5 Posted 2006-10-19 07:32 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
1. dir

@echo off
set strTime=2006-10-17
set "command=dir /a-d /od /t:c ^| findstr /irc:"^%%strTime%%""
for /f "tokens=3,*" %%i in ('%command%') do echo %%j
pause

To delete, just change echo to del. If dealing with multiple directories, you can consider using the form call :label parameter, which makes the code easier to organize and modify.
2. wmic
wmic datafile where "InstallDate>='20061018000000.000000+480' and path='\\documents and settings\\xxx\桌面\\'" get caption

Note: If you want to delete directly without displaying, you can change the subsequent get caption to call delete /NOINTERACTIVE. Without /NOINTERACTIVE, it is in interactive mode and requires confirmation.
Floor 6 Posted 2006-10-19 10:28 ·  IANA 局域网IP(Private-Use)
新手上路
Credits 11
Posts 5
Joined 2006-10-19 09:15
19-year member
UID 66825
Gender Male
Status Offline
Learning, there are really many experts ^0^
Floor 7 Posted 2006-10-19 21:56 ·  中国 辽宁 大连 联通
新手上路
Credits 10
Posts 4
Joined 2006-10-19 04:01
19-year member
UID 66708
Gender Male
Status Offline
Maybe it's my expression that's problematic. Here, the time can't be hard-coded. I only dynamically ensure that the specified number of files in the backup folder are the most recent backups. For example, if there are 5 backups, when a new one is added recently, the earliest one among these 5 is automatically deleted. It's a process of constantly updating.

And it's similar to the following post

lilankui
Newcomer

Score 6
Posts 1
Registered 2006-9-5
From Jiangxi
Status Offline 『L楼 owner』: How to use batch processing to delete files older than 7 days

@echo
Rem Get the date 7 days ago, and get it into variable nowdate
echo wscript.echo dateadd("d",-6,date) >%tmp%\tmp.vbs
for /f "tokens=1,2,3* delims=-" %%i in ('cscript /nologo %tmp%\tmp.vbs') do set y=%%i
for /f "tokens=1,2,3* delims=-" %%i in ('cscript /nologo %tmp%\tmp.vbs') do set m=%%j
for /f "tokens=1,2,3* delims=-" %%i in ('cscript /nologo %tmp%\tmp.vbs') do set d=%%k
if %m% LSS 9 set m=0%m%
if %d% LSS 9 set d=0%d%
set nowdate=%y%-%m%-%d%
I want to know how to interpret these lines "for /f "tokens=1,2,3* delims=-" %%i in ('cscript /nologo %tmp%\tmp.vbs') do set d=%%k", which expert can tell me

『2nd floor』: There are too many things to consider for date operations, at least with batch processing. Back to LZ, he combines VBS script and batch processing to complete it. The code hasn't been tested. I'm going to write a batch processing about this. There are really too many things to consider, hehe................

[ Last edited by fangcheng79 on 2006-10-19 at 22:03 ]
Floor 8 Posted 2006-10-20 07:19 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
A feasible method has come out. Do we really have to post the complete code?

dir /a-d /od /t:c /b can list the files in the current directory from the most recent to the oldest by time. Put it inside for, use for /f "delims=" %%i in ('%command%') do set "file=%%i"
Then just del %file% is done.
p.s: dir also supports dir abcd*.xxx
Floor 9 Posted 2006-11-25 13:24 ·  中国 福建 泉州 石狮市 电信
初级用户
Credits 32
Posts 14
Joined 2006-11-22 12:05
19-year member
UID 71377
Gender Male
Status Offline
I'm a newbie. Please, moderator, spend a little time posting the complete program. Thanks
Floor 10 Posted 2006-11-25 23:18 ·  中国 福建 泉州 石狮市 电信
初级用户
Credits 32
Posts 14
Joined 2006-11-22 12:05
19-year member
UID 71377
Gender Male
Status Offline
Bump
Floor 11 Posted 2006-11-26 03:35 ·  中国 四川 南充 电信
超级版主
★★★★
我爱DOS
Credits 5,310
Posts 2,044
Joined 2005-09-26 12:00
20-year member
UID 42843
Gender Male
From 四川南充
Status Offline
```@echo off
echo wscript.echo dateadd("d",-6,date)>tmp.vbs
for /f "tokens=1,2,3* delims=-" %%i in ('cscript /nologo tmp.vbs') do set/a a=%%i*365+%%j*12+%%k+1300
for /f "tokens=1,2,3,5* delims=- " %%a in ('dir/a-d^|findstr "199 200"') do (set/a b=%%a*365+1%%b*12+1%%c
if !b! lss %a% del "%%e")
```
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
redtek +5 2006-11-26 07:00
Floor 12 Posted 2006-11-26 06:59 ·  中国 北京 东城区 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
The moderator is super powerful~ Appreciate~ :)))

Originally, set /a a=2006*365+11*12+19+1300... All convert years/months/days into pure numbers,
and then compare with pure numbers~ :)))
    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
Floor 13 Posted 2006-11-26 08:54 ·  中国 福建 泉州 电信
初级用户
Credits 32
Posts 14
Joined 2006-11-22 12:05
19-year member
UID 71377
Gender Male
Status Offline
Learning... Another question, where should this processing file be placed, inside the backup file or outside? If placed inside, will it be deleted?

@echo off
echo wscript.echo dateadd("d",-6,date)>tmp.vbs
for /f "tokens=1,2,3* delims=-" %%i in ('cscript /nologo tmp.vbs') do set/a a=%%i*365+%%j*12+%%k+1300
for /f "tokens=1,2,3,5* delims=- " %%a in ('dir/a-d^|findstr "199 200"') do (set/a b=%%a*365+1%%b*12+1%%c
if !b! lss %a% del "%%e")

Who can explain this code?

[ Last edited by linqlou on 2006-11-25 at 08:09 PM ]
Floor 14 Posted 2006-11-26 09:33 ·  中国 四川 南充 电信
超级版主
★★★★
我爱DOS
Credits 5,310
Posts 2,044
Joined 2005-09-26 12:00
20-year member
UID 42843
Gender Male
From 四川南充
Status Offline
The following will not delete itself:
@echo off
echo wscript.echo dateadd("d",-6,date)>tmp.vbs
for /f "tokens=1,2,3* delims=-" %%i in ('cscript /nologo tmp.vbs') do set/a a=%%i*366+%%j*31+%%k+3200
for /f "tokens=1,2,3,5* delims=- " %%a in ('dir/a-d^|findstr "199 200"') do (set/a b=%%a*366+1%%b*31+1%%c
if !b! lss %a% if not "%~nx0"=="%%e" del "%%e")
Recent Ratings for This Post ( 4 in total) Click for details
RaterScoreTime
redtek +9 2006-11-26 22:15
ccwan +3 2006-11-27 02:44
chainliq -1 2006-12-01 17:40
Dana +2 2008-01-03 17:14
Floor 15 Posted 2006-11-26 20:59 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
The moderator is awesome! When I am unrestricted, I will definitely give you extra points!
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Forum Jump: