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-22 12:00
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How do I write this backup and upgrade batch file? View 3,281 Replies 14
Original Poster Posted 2004-08-25 00:00 ·  中国 广东 东莞 电信
初级用户
Credits 172
Posts 14
Joined 2004-08-25 00:00
21-year member
UID 30723
Gender Male
Status Offline
First, thanks to Climbing for introducing me to such a good place. I didn't even know there was a China DOS Union before, so I hope the veterans here won't mind. Knowledge about DOS is getting less and less now, and this place really lets us review the old and learn the new.
Enough small talk, I want to ask a question, one I've never been able to solve.
> --------------------------------------
> 1. Create a directory on drive C called TBLBAK; if it already exists, don't create it. Then create a directory inside TBLBAK named after the date
(or it can be named some other way too)
> 2. Copy c:\faspic\tbl*.* to C:\tblbak\ previously created directory\.
> 3. Overwrite-copy a:\tbl*.* to c:\faspic\.
> 4. Check whether the directories created by this batch file under c:\tblbak exceed 10. If they do, delete the earliest
created one, i.e. keep at most the latest 10 directories.
> 5. Can one batch file carry out all the above operations? Of course, temporarily created batch files can be ignored.
>
> ---------------------------------------------


There are several difficult points:
1. How do I create a directory name based on the date that DOS can recognize? That is, the year can only take the last few digits; if it goes beyond that, DOS won't recognize it?
2. How do I detect whether those directories it created have already exceeded 10? Or some user-defined number?
3. How do I intelligently delete the earliest-created one while always keeping 10?
4. Aside from temporarily created batch files, how feasible is it to have one batch file do all this?
5. I'm not very capable, so I hope all you experts can lend me a hand and help me out.
Floor 2 Posted 2004-08-25 00:00 ·  中国 河北 石家庄 联通
铂金会员
★★★★
网络独行侠
Credits 6,962
Posts 2,753
Joined 2003-04-16 00:00
23-year member
UID 1565
Gender Male
From 河北保定
Status Offline
First tell us what operating system this batch file is running under. Is it a pure DOS environment, or the DOS prompt under 9x/nt? Before asking a question, write your environment clearly first.
偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
Floor 3 Posted 2004-08-25 00:00 ·  中国 广东 东莞 电信
初级用户
Credits 172
Posts 14
Joined 2004-08-25 00:00
21-year member
UID 30723
Gender Male
Status Offline
Oh, thanks, it's a pure DOS environment. DOS 6.22.
Floor 4 Posted 2004-08-25 00:00 ·  中国 河北 石家庄 联通
铂金会员
★★★★
网络独行侠
Credits 6,962
Posts 2,753
Joined 2003-04-16 00:00
23-year member
UID 1565
Gender Male
From 河北保定
Status Offline
I no longer have a DOS 6.22 environment, so it's not easy for me to test. I suggest you first write out what you can, then we'll help you solve the parts you can't solve. Basically all of it can be done with a batch file, of course using third-party tools, especially Horst's PBAT batch utility tools.
偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
Floor 5 Posted 2004-08-25 00:00 ·  中国 广东 东莞 电信
初级用户
Credits 172
Posts 14
Joined 2004-08-25 00:00
21-year member
UID 30723
Gender Male
Status Offline
But I simply can't write the parts for creating the corresponding directory according to the date, checking how many directories are under a directory, and deleting the old directories.
Floor 6 Posted 2004-08-25 00:00 ·  中国 广东 东莞 电信
初级用户
Credits 172
Posts 14
Joined 2004-08-25 00:00
21-year member
UID 30723
Gender Male
Status Offline
Does any expert know? Please help write it, especially the difficult points I mentioned. I just can't figure them out. If I understood those difficult points, my problem would have been solved long ago.
Floor 7 Posted 2004-08-26 00:00 ·  中国 河北 石家庄 联通
铂金会员
★★★★
网络独行侠
Credits 6,962
Posts 2,753
Joined 2003-04-16 00:00
23-year member
UID 1565
Gender Male
From 河北保定
Status Offline
You yourself aren't making any effort at all, which makes me feel very disappointed。The batch file Climbing wrote for StoneLei (including all the tool programs used, 14KB, click to download)Below is the contents of the batch file:

@echo off
:: StoneLei.BAT is a batch file created for forum user StoneLei for the following purposes
:: 1. Create a directory on drive C called TBLBAK; if it already exists, do not create it. Then create a directory inside TBLBAK named with the date (or it can be named some other way too)
:: 2. Copy c:\faspic\tbl*.* to the directory previously created under C:\tblbak\
:: 3. Overwrite-copy a:\tbl*.* to c:\faspic
:: 4. Check whether the directories created by this batch file under c:\tblbak exceed 10; if they do, delete the earliest ones created, that is, keep at most the latest 10 directories
::
:: Author: Climbing(xclimbing@msn.com)
:: Version: 1.0
:: Creation date: 2004.08.26 12:15
:: Last modified date: 2004.08.26 12:54
:: This batch file is designed for the MS-DOS 6.22 environment, and will use Horst's nset, dready, and lmod utility programs while running
:: This batch file is not compatible with the Tianhui Chinese character support system; it is recommended that you use the UCDOS Chinese character support system。cls:: Check whether the C:\tblbak directory exists; if it does not, create it
if not exist c:\tblbak\nul md c:\tblbak:: Use Horst's nset command to obtain an environment variable curdir named after the current date
set curdir=
echo. | date | nset /S- curdir=$5$6$7:: Check whether the directory to be created with the current date as its name exists; if it does, error
if exist c:\tblbak\%curdir%\nul goto _error1:: If it does not exist, create it
md c:\tblbak\%curdir%:: Carry out the first-step file backup operation
if not exist c:\faspic\nul goto _error2
xcopy /e /y c:\faspic\tbl*.* c:\tblbak\%curdir% > nul:: Check whether the floppy drive is ready; if it is, carry out the file copy operation
:_chkflp
cls
echo Please put the floppy disk into the floppy drive, and press any key to continue after it is ready...
pause > nul
dready a:
if errorlevel 2 goto _noflp
goto _cpflp
:_noflp
echo Error: the floppy drive is not ready!
echo Please check whether the floppy disk is correct, then press any key to retry...
pause > nul
goto _chkflp:_cpflp
xcopy /e /y a:\tbl*.* c:\faspic > nul:: Start checking whether directories named with dates exceed 10; if they do, delete the extras
:: Note: for the convenience of checking directories named with dates, all directories beginning with 20 are treated by default as date-named directories
dir /ad/b/o-d c:\tblbak\20*.* | lmod /L11* @deltree /y c:\tblbak\ > temp.bat
call temp.bat
del temp.bat
goto _runok:_error1
echo Error: directory c:\tblbak\%curdir% already exists!
goto _runerr:_error2
echo Error: directory c:\faspic does not exist!
goto _runerr:_runerr
echo.
echo Sorry, an error occurred while the program was running.
echo The program will exit.
goto _quit:_runok
echo Congratulations: the program completed successfully!
goto _quit:_quit
set curdir=
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
heroyb +1 2006-12-06 10:24
偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
Floor 8 Posted 2004-08-26 00:00 ·  中国 广东 东莞 电信
初级用户
Credits 172
Posts 14
Joined 2004-08-25 00:00
21-year member
UID 30723
Gender Male
Status Offline
Big brother Climbing, your warm-hearted help really moved me to tears. Here I want to say "thank you" to you, and I will follow your advice and work and study harder in the future. Thank you.
Floor 9 Posted 2004-08-27 00:00 ·  中国 福建 厦门 电信
系统支持
★★★
Credits 904
Posts 339
Joined 2002-10-10 00:00
23-year member
UID 1904
From 厦门
Status Offline
Brother Stone really is admirable in the way he treats people!!! Salute to Climbing!
Floor 10 Posted 2004-09-20 00:00 ·  中国 山西 运城 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re Climbing:
Brother Climbing's program is already quite complete, except for two details:
1. When checking whether a directory exists, using if exist c:\directory\nul cannot detect whether a directory with hidden or system attributes exists, which will cause errors later in the directory creation process. The solution is either dir c:\directory /a | find "c:\directory" /i, or attrib c:\directory | find "direcory" /i, and then use if errorlevel to test the errorlevel returned by find.
2. I am not quite sure how nset parses date strings separated by "-", but since it uses the output of the date command, it should take into account that under DOS6 the date format is not necessarily the Eastern year-month-day style, but the American month-day-year style, unless the country setting is configured in config.sys. Therefore, there should be a problem in the module that checks the number of directories; please adjust it as appropriate.

Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
heroyb +1 2006-12-06 10:23
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 11 Posted 2004-09-20 00:00 ·  中国 河北 保定 联通
铂金会员
★★★★
网络独行侠
Credits 6,962
Posts 2,753
Joined 2003-04-16 00:00
23-year member
UID 1565
Gender Male
From 河北保定
Status Offline
Re Willsort:
Brother Willsort really does strive for perfection. To tell the truth, I really didn't think about things that carefully.
1. If there really are hidden or system directories, I suggest the user first manually remove those attributes (actually, not removing them does not affect the program's operation either). Under normal circumstances this problem probably won't come up. But thank you for telling me this method can't be used for directories with hidden or system attributes (I really wasn't very clear about that). Also, under DOS 6.22 there probably is no find command, which is another troublesome matter.
2. The batch file I wrote above really was tested by finding a DOS 6.22 system, then installing and debugging it in a virtual machine. In that DOS 6.22 system, the Date command really did display in yyyy-mm-dd format (without setting any country option). But even if it displays as mm-dd-yyyy, it doesn't matter; just change it to nset /S- curdir=$7$5$6.
Thanks again to brother Willsort for not being stingy with your advice. Actually, writing this program was just to get the job done. I've always been rather careless, so as long as I can muddle through, that's fine, hehe.
偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
Floor 12 Posted 2004-09-20 00:00 ·  中国 山西 运城 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re stonelei:

1. As for obtaining the date, I already explained it in another post, but considering that DOS6.22 DATE uses a four-digit year value, and your requirement that you don't want to lose the last two digits, I'll add another method: you can capture the creation and modification date of a file under DOS. The rough code is as follows:

echo if not == goto end>getdate.bat
echo set date=%%3>>getdate.bat
dir getdate.bat | find "getdate bat" /i >>getdate.bat
echo :end>>getdate.bat
for %%c in (call del) do %%c getdate.bat


2. As for determining the number of directories, you can operate by transforming the file list, mainly using dir and find. The rough code is as follows:

dir c:\\directory /ad /od /b | find "-0" > datedir.lst
fc datedir.lst nul /n | find "11: ">delcall.bat
echo deltree %%1 /y >11.bat
for %%c in (call del) do %%c delcall.bat
for %%f in (11.bat datedir.lst) do del %%f


3. As for floppy disk testing, you can actually use a simple debug script to call DOS int21 functionality to implement it, which avoids bringing in third-party programs to solve the problem and is suitable for an environment that has only a standard DOS configuration. But if your conditions allow it and you do not mind third-party programs, then you can ignore this detail.

4. From the above description, you should be able to understand that this batch file can absolutely be implemented with a single file. In fact, any problem that can be implemented by multiple batch files can be implemented by a single batch file, but sometimes, for design-principle considerations, it is necessary to split it into several modules.

Also, the code portions above are only an offline design based on experience, and have not actually been tested in a DOS6 environment. You can adjust them at any time according to your own needs and any problems that arise, and they are not subject to any form of copyright.

Re Climbing:

I just saw your reply, so I'm replying along with editing this post.

find.exe has been packaged and installed together with DOS since DOS5 as an external command specifically for pipes. Similar commands include fc.exe and more.exe. Perhaps your DOS6.22 is not very complete. Also, edlin and debug are relatively commonly used pipe commands. Unfortunately, edlin was removed after DOS6, which greatly weakened batch files' ability to control text strings, but it has always existed in the nt series.

The date format issue used to be a very troublesome problem for me when using DOS6, because with different country settings, the date formats vary greatly, creating a major challenge for the universality of some of my batch programs. The behavior on your machine may be because the virtual machine's virtualization was incomplete, or because dos6 gets different results for the date from different BIOS versions, or because your dos6.22 was not using the original configuration.

[ Last edited by willsort on 2005-10-13 at 10:29 ]
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 13 Posted 2004-09-20 00:00 ·  中国 河北 保定 联通
铂金会员
★★★★
网络独行侠
Credits 6,962
Posts 2,753
Joined 2003-04-16 00:00
23-year member
UID 1565
Gender Male
From 河北保定
Status Offline
Once again I sigh over how endless the sea of learning is.
偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
Floor 14 Posted 2004-09-23 00:00 ·  中国 吉林 长春 联通
初级用户
Credits 120
Posts 6
Joined 2004-09-14 00:00
21-year member
UID 31413
Gender Male
Status Offline
I don't understand batch files at all. Do those Chinese characters need to be written into it too?
Floor 15 Posted 2010-03-16 23:38 ·  中国 北京 鹏博士BGP
新手上路
Credits 25
Posts 11
Joined 2010-03-15 17:17
16-year member
UID 162247
Gender Male
Status Offline
Very good, I learned something, but why is it incompatible with the Tianhui Chinese character support system?
Forum Jump: