![]() |
China DOS Union-- Unite DOS · Advance DOS · Grow DOS --Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum |
| Guest | Log in | Register | Members | Search | China DOS Union |
|
中国DOS联盟论坛 The time now is 2026-09-15 12:59 |
47,812 topics / 349,917 posts / today 0 new / 48,268 members |
| DOS批处理 & 脚本技术(批处理室) » [Help] Automatic Backup and Deletion of Files |
| Printable Version 10,701 / 25 |
| Floor1 fangcheng79 | Posted 2006-10-19 05:55 |
| 新手上路 Posts 4 Credits 10 | |
|
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 |
|
| Floor2 fangcheng79 | Posted 2006-10-19 05:56 |
| 新手上路 Posts 4 Credits 10 | |
|
This is very similar to the automatic backup of the Windows registry
|
|
| Floor3 namejm | Posted 2006-10-19 06:10 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
|
"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?
|
|
| Floor4 fangcheng79 | Posted 2006-10-19 06:12 |
| 新手上路 Posts 4 Credits 10 | |
|
The time when the file was created. The backup file is not modified, just to ensure that it can be restored and used later.
|
|
| Floor5 3742668 | Posted 2006-10-19 07:32 |
| 荣誉版主 Posts 718 Credits 2,013 | |
|
1. dir
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 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. |
|
| Floor6 xyxFlysky | Posted 2006-10-19 10:28 |
| 新手上路 Posts 5 Credits 11 | |
|
Learning, there are really many experts ^0^
|
|
| Floor7 fangcheng79 | Posted 2006-10-19 21:56 |
| 新手上路 Posts 4 Credits 10 | |
|
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 ] |
|
| Floor8 3742668 | Posted 2006-10-20 07:19 |
| 荣誉版主 Posts 718 Credits 2,013 | |
|
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 |
|
| Floor9 linqlou | Posted 2006-11-25 13:24 |
| 初级用户 Posts 14 Credits 32 | |
|
I'm a newbie. Please, moderator, spend a little time posting the complete program. Thanks
|
|
| Floor10 linqlou | Posted 2006-11-25 23:18 |
| 初级用户 Posts 14 Credits 32 | |
|
Bump
|
|
| Floor11 不得不爱 | Posted 2006-11-26 03:35 |
| 超级版主 Posts 2,044 Credits 5,310 From 四川南充 | |
|
```@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") ``` |
|
| Floor12 redtek | Posted 2006-11-26 06:59 |
| 金牌会员 Posts 1,147 Credits 2,902 | |
|
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~ :))) |
|
| Floor13 linqlou | Posted 2006-11-26 08:54 |
| 初级用户 Posts 14 Credits 32 | |
|
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 ] |
|
| Floor14 不得不爱 | Posted 2006-11-26 09:33 |
| 超级版主 Posts 2,044 Credits 5,310 From 四川南充 | |
|
The following will not delete itself:
|
|
| Floor15 ccwan | Posted 2006-11-26 20:59 |
| 金牌会员 Posts 1,160 Credits 2,725 From 河北廊坊 | |
|
The moderator is awesome! When I am unrestricted, I will definitely give you extra points!
|
|
| 1 2 Next |
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |