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-24 13:15
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Original] [XP] File Backup Utility V2.4 (Final) [Updated on 07-4-8] DigestI View 34,114 Replies 112
Original Poster Posted 2006-05-22 23:35 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Recent Ratings for This Post ( 2 in total) Click for details
RaterScoreTime
feiying001 +1 2007-02-21 13:47
huzixuan +4 2007-04-07 23:38
Attachments
文件备份器(Final).rar (117.32 KiB, Downloads: 328)
Floor 2 Posted 2006-05-23 01:47 ·  中国 山西 运城 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re: namejm:

It's a very good batch script, and it can be seen that you have put a lot of effort into it. Therefore, I have a suggestion: transform this thread into an original thread about "Automated File Backup Batch Scripts". The first post should repost the latest source code, and subsequent replies can discuss several details and issues involved. This requires transferring and merging some of your past thread posts, so I need to obtain your consent.

Regarding the execution problem of the at3 command, since I haven't finished reading the code yet, I can't give any opinions. I only speculate that it might be related to the corresponding backup script, so the code of this batch processing needs to be posted.

In addition, just a brief discussion on the following section quoted from the source code:


set /p source= Please enter the path of the file to be backed up:
:: Here, you can prompt to directly drag the backup target into the running window to obtain the source path

for /f "delims=:\" %%i in ("%source%") do set subarea1=%%i
:: It seems to be to obtain the drive letter of the source. Why not use set subarea1=%source:~0,1%

if not "%source%"=="" set source=%source%
:: The meaning of this sentence is unclear, please enlighten me
:: If you want to format the path, you need to use for %%p in ("%source%") do set source=%%~fp

if "%source%"=="" goto input_source
:: It is suggested to assign the selection of directly pressing Enter (that is, %source% is empty) to the operation with high frequency
if "%source%"=="0" exit
if "%source%"=="1" goto backup_task

echo "%source%"|find "%subarea1%:\" > nul
if "%ERRORLEVEL%"=="1" goto input_source
:: Judge whether there is a drive letter in the path? Can it be done with if "%source:~1,2%"==":\"

if "%source%"=="%subarea1%:\" goto forbid
:: Judge whether the path is the root path? When source is C:\\ or C:\., it will not work

if not exist "%source%" goto source_noexist
goto input_object
:: The meaning of object is object, which is ambiguous with the backup target. It is better to change it to target or destination


[ Last edited by willsort on 2006-5-23 at 01:48 ]
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 3 Posted 2006-05-23 12:30 ·  中国 北京 联通
银牌会员
★★★
DOS联盟捡破烂的
Credits 1,144
Posts 425
Joined 2005-10-20 00:00
20-year member
UID 43784
From 北京
Status Offline
LZ really has patience. It's so long, longer than my graduation thesis. Admiration, admiration.......... Just as WILLSOFT said, I haven't finished reading the code yet and need to digest it slowly.
Floor 4 Posted 2006-05-23 13:05 ·  中国 浙江 杭州 电信
铂金会员
★★★★
网络独行侠
Credits 6,962
Posts 2,753
Joined 2003-04-16 00:00
23-year member
UID 1565
Gender Male
From 河北保定
Status Offline
I think the error with at has little to do with the batch script. It might be due to other aspects.

From the owner's batch script, as long as one at plan runs normally, the others should also be fine.

I respect the owner's patience. But if you just want to implement the file backup function by writing this batch script, it's really simpler to introduce some third - party tools, such as SecondCopy or the synchronization software CmdSync, etc. Of course, personal environments are different, so methods vary. Mine is just a suggestion.
偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
Floor 5 Posted 2006-05-23 14:11 ·  中国 上海 华为云
中级用户
★★
Credits 256
Posts 93
Joined 2006-03-26 22:12
20-year member
UID 52853
Gender Male
From 广东
Status Offline
Agree with the suggestion of building 4!

Show respect for the building owner's patience

Although scripts can help us complete some repetitive tasks!
But sometimes it's not bad to use some third-party software.
Floor 6 Posted 2006-05-23 14:18 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
to willsort Moderator:
I don't know what conditions are required when you perform the operation of "transferring and merging some of your past theme posts". I'm very happy that this script can be further improved by everyone. Please edit it according to your ideas. If there's anything that needs me to do, I will cooperate as much as possible. Thank you, willsort.
Report the problem:
Just now, I suddenly recalled that the scheduled task batch file generated by manual execution shows "…… Whether to rewrite? (YES or NO or All)" (something like that). Now, due to the rush of time, I didn't have time to look closely. It seems that because at runs in the background, it won't make any prompts for commands that need manual confirmation, so it causes the cmd.exe and xcopy.exe processes to always exist, and the backup operation doesn't execute.
Now, adding the /y parameter after the xcopy sentence solves the problem.
I have read everyone's replies. It seems that there are still many details that need to be improved in this script. Because I have to go to work soon, I don't have time to reply one by one. I can only reply after getting off work. Please forgive me. At the same time, please continue to find faults in it, especially see if there are some places that don't have better solutions. Thank you all here.
Floor 7 Posted 2006-05-23 18:40 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Thanks to the review of moderator willsort, here are some of my thoughts:

"set /p source= Please enter the path of the file to be backed up:
:: Here you can prompt to directly drag the backup target into the running window to obtain the source path"
——It turns out that you can directly drag it over to display the path. Hehe, unexpected gain. Thanks to willsort here. If the path has spaces, when dragging it over directly, it will automatically add quotes at the beginning and end of the path. And originally, in order to take care of the path with spaces entered manually, the following variable references all added quotes. If the prompt is to use the drag-and-drop method, it will cause the program to error. If manual input is not allowed and only drag-and-drop is allowed, then removing the quotes in the variable references should solve this contradiction. It's hard to choose for the time being.

"for /f "delims=:\" %%i in ("%source%") do set subarea1=%%i
:: It seems to get the drive letter of the source. Why not use set subarea1=%source:~0,1%"
——If the path entered by the drag-and-drop method has spaces, it can only get the first quote. If the solution of the previous step is adopted and only drag-and-drop is allowed, it will cause an error. It seems that the drag-and-drop method is quite error-prone.

"if not "%source%"=="" set source=%source%
:: The meaning of this sentence is unclear. Please give advice
:: If you need to format the path, you need to use for %%p in ("%source%") do set source=%%~fp"
——To be honest, I don't quite understand this. I copied it from the batch program of the Shenshan Hongye CD. At that time, I thought it was a format for getting keyboard input and didn't think too much about it.

"if "%source%"=="" goto input_source
:: It is suggested to assign the choice of directly pressing Enter (that is, %source% is empty) to the operation with high frequency of use
if "%source%"=="0" exit
if "%source%"=="1" goto backup_task"
——My original intention was to detect whether there is input content. If there is none, then force input and not allow skipping. I feel that this processing is better, and I don't plan to change this code for the time being.

"echo "%source%"|find "%subarea1%:\" > nul
if "%ERRORLEVEL%"=="1" goto input_source
:: Judge whether the path contains a drive letter? Can it be used if "%source:~1,2%"==":\" "
——For the same reason as above, when dragging and inputting, only the first quote can be obtained.


"if "%source%"=="%subarea1%:\" goto forbid
:: Judge whether the path is the root path? When source is C:\\ or C:\. it will not work"
——I didn't consider this situation at that time. Thanks to the moderator for the reminder. Now add two more lines of code at the back, which should avoid this error:
if "%source%"=="%subarea1%:\\" goto forbid
if "%source%"=="%subarea1%:\." goto forbid

"if not exist "%source%" goto source_noexist
goto input_object
:: The meaning of object is object, which is ambiguous with the backup target. It is better to change it to target or destination"
——It's embarrassing to say. I have always been worried about English, so I often make jokes. I have changed it in the new code. Thanks for the correction.

The following is the modified code:

[ Last edited by willsort on 2006-5-24 at 01:42 ]
Floor 8 Posted 2006-05-23 18:54 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Originally posted by Climbing at 2006-5-23 13:05:
I respect your patience, but if you're only writing this batch script for file backup, it's actually simpler to use some third-party tools. For example: SecondCopy, or the synchronization software CmdSync, etc. Of course, personal environments are different, so methods vary. Mine is just a suggestion.

Thanks to the boss for providing the clues about these two programs. I searched online, and their functions are very appealing.
But there are two drawbacks: 1. As I said in the update history of this script program, "There are many restrictions on backup software on the Internet." SecondCopy has a 30-day free trial restriction. For rookies like me who don't know how to crack, after using it for 30 days, I may have to adjust the system time forward by N days (not necessarily effective), or back up manually again. And the registration codes on the Internet are all expired, and there seem to be no cracks; 2. Both are in English. This has always been a headache for me. Although I know that not knowing much English is not easy to get along on the Internet and not easy in real life, due to the limitation of my major, I haven't spent much time on English since graduating from college. So the software I use is either developed by Chinese people or has been localized. Pitiful :(
For the above reasons, I decided to make a script program by myself. On one hand, I can practice skills, and on the other hand, I can use it for my friends who are more rookie than me. Bitter smileing……
Floor 9 Posted 2006-05-24 02:47 ·  中国 山西 太原 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re namejm:

I have made some changes to the first post of the topic, including modifying the title and adjusting the order of the main content.

Originally, I tried to merge all discussion topics related to this program that brothers posted into here, but I found that brother has put a lot of effort into this script and actually found more than a dozen related topics. In this case, not only is the merging workload heavy, but it will also cause many discussion topics with relatively little relevance to be mixed in the same topic, which反而 reduces the role of the topic. Therefore, I finally decided to only provide links to all related discussion topics in the first post for reference and indexing.

Secondly, the code in brother's 7th floor is moved to the first post. This is due to the following reasons: First, because the code volume is too large, if there are multiple versions with little difference, it will reduce the page opening speed, increase the user's reading time, and consume more forum space and time resources; second, because our principle of publishing the program is to let users obtain the latest version in the most convenient way as much as possible. If the new version is updated in the reply post, it may require scrolling or even turning pages to get it, which raises the threshold for promotion. Therefore, it is suggested that when brother releases the new version of the program, he can also edit the code into the first post.

Again, regarding that small section I quoted, your dilemma can be solved. Users can drag and back up the source, or directly enter the source path (absolute path or relative path). No matter whether it contains quotes or non-standard path reference methods (C:\\, C:\.\), we can solve these incompatible problems through pre-formatting and standardizing the path string. The following statement can remove the quotes in the path, convert the relative path to the absolute path, convert the non-standard path to the standard path (C:\\-->C:\), and judge whether the path effectively exists and whether it is the root path. However, it still cannot resist the illegal characters (>|) in the path. I wonder if other brothers have any better solutions?

Finally, I don't know what environment brother uses to write the code? Judging from the comment width reaching 114, brother may be using a wide-screen monitor, so there is no need to worry about the line break browsing problem of comments. However, according to the code posted in the forum and my system environment (17"+XP+EditPlus2), no matter whether the automatic line break function is enabled or not, the excessive width of comments affects the readability of the code.


set _src=
for %%p in ("%source%") do if exist %%p set _src=%%~fp
if "%_src%"=="" for %%p in (%source%) do if exist %%p set _src=%%~fp
if "%_src:~1,2%"==":\" goto forbid
set source=%_src%


[ Last edited by willsort on 2006-5-24 at 02:54 ]
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 10 Posted 2006-05-24 12:48 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Originally posted by willsort at 2006-5-24 02:47:
Re namejm:

Finally, I don't know what environment you use to write code? But from the fact that the width of the comment reaches 114, it seems that you may be using a wide-screen monitor, so there is no need to worry about the line break browsing problem of the comment. However, according to the code posted in the forum and my system environment (17"+XP+EditPlus2), whether the automatic line break function is enabled or not, the excessive width of the comment affects the readability of the code.


The boss really saw the problem at a glance. I am using a wide-screen computer, 14''+XP, with a resolution of 1280×768. I used to be in pain because of the inconsistency between the wide screen and the standard 4:3 screen when learning to make web pages. Now I actually forgot this detail. It really woke me up. It seems that I need to find a 4:3 monitor to modify.
I didn't post the thread specifically for writing this script. It was because when making a CD, I needed to extract specified e-books from the CD to the computer. As my interest became stronger and stronger, and my enthusiasm for batch processing became uncontrollable, I fiddled with many scripts on my own computer to enhance the system's functions for self-entertainment. I just came back from work and still have a bit of a headache. I'll take a nap first. I'll reply when I come back from work in the evening. Please forgive me.
Floor 11 Posted 2006-05-24 17:32 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Can you use for to parse the path, and if there are illegal symbols, jump to the warning interface?
Floor 12 Posted 2006-05-24 23:54 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Originally posted by willsort at 2006-5-24 02:47:
Re namejm:

set _src=
for %%p in ("%source%") do if exist %%p set _src=%%~fp
if "%_src%"=="" for %%p in (%source%) do if exist %%p set _src=%%~fp
if "%_src:~1,2%"==":\" goto forbid
set source=%_src%


After compressing this code into a single line and testing it, I found it works fine and have updated it into the script. The update position is as follows:

set source=
set /p source= Please enter the path of the file to be backed up:
for %%p in (%source%) do set source=%%~fp
for /f "delims=:\" %%i in ("%source%") do set subarea1=%%i


I found an issue and it seems I need to adjust the input prompt.

title Please enter the source file path
echo ╭────────╮
echo ╭─────────┤Select the file to back up├─────────╮
echo │ ╰────────╯ │
echo │ Operation instructions: │
echo │ │
echo │ This program can back up the specified file within the specified time │
echo │ │
echo │ To the specified location. Please drag the file to be backed up to this window │
echo │ │
echo │ (Recommended practice), or manually enter the full path of the file to be backed up │
echo │ │
echo │ (Paths with spaces need to be enclosed in double quotes with half-width at the beginning and end │
echo │ │
echo │ status; do not enter illegal characters; otherwise │
echo │ │
echo │ Unexpected errors will occur), then press Enter to confirm. │
echo │ │
echo ╰────────────────────────────╯
echo.
echo Press 1 to view the existing backup tasks Press 0 to exit
echo ──────────────────────────────


(The content between
 and 
 cannot apply character color settings again, which is quite a pity.)

[ Last edited by namejm on 2006-5-25 at 13:28 ]
Floor 13 Posted 2006-05-25 00:12 ·  中国 山西 大同 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re namejm:

The key issue is that we can't be sure whether there are quotes before and after the path string, so we can't rashly decide whether to enclose the path string with quotes, and thus can't restrict the illegal characters in it. I once had a similar discussion in , and there's no good solution at present.

And any statement that probes and analyzes the path string (whether echoif, for), requires the path string itself to appear in the command line, and if the unconstrained illegal characters obviously disrupt the normal order of the command line. It may even not produce a syntax error prompt, but unexpectedly generate some garbage files.

In your current code, if some users enclose the path with spaces with quotes for safety, the code will obviously have a syntax error and stop. Up to now, we've only tested the legal behaviors of well-meaning users, and haven't really carried out some non-well-meaning extreme boundary tests.

So, it's very difficult to compile a code with strong "robustness", especially in free and flexible batch processing.

The oddity of batch programming: selection of anti-space characters in if statement
http://www.cn-dos.net/forum/viewthread.php?tid=12388#pid100419
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 14 Posted 2006-05-25 18:32 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Originally posted by willsort at 2006-5-24 02:47:
Re namejm:
The following statements can remove possible quotation marks in the path, convert the relative path to the absolute path, convert the non-standard path to the standard path (C:\\-->C:\), and judge whether the path effectively exists and whether it is the root path.


set _src=
for %%p in ("%source%") do if exist %%p set _src=%%~fp
if "%_src%"=="" for %%p in (%source%) do if exist %%p set _src=%%~fp
if "%_src:~1,2%"==":\" goto forbid
set source=%_src%


The above code I inserted into
set source=
set /p source= Please enter the path of the file to be backed up:

and
if not "%source%"=="" set source=%source%
if "%source%"=="" goto input_source
if "%source%"=="0" exit
if "%source%"=="1" goto backup_task

in between cannot remove the quotation marks before and after the path with spaces, but jumps to the interface for prohibiting full disk copying. Is it that I inserted the wrong position?

[ Last edited by namejm on 2006-5-25 at 22:38 ]
Floor 15 Posted 2006-05-25 22:30 ·  中国 山西 临汾 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re namejm:

I'm very sorry! The problem lies in the line "if "%_src:~1,2%"==":\" goto forbid". This line of code was inserted temporarily by me after completing the path test and before reposting. It caused an error because it wasn't tested! Changing it to "if "%_src:~1,3%"==":\" goto forbid" can solve the problem.

But the above code still can't perfectly solve the problem of path strings of non - existing files and folders. Replacing code with code can solve this problem. Of course, the new code still can't defend against illegal characters like "<|>".

Old code

set /p source= Please enter the path of the file to be backed up:
for %%p in (%source%) do set source=%%~fp
for /f "delims=:\" %%i in ("%source%") do set subarea1=%%i
if not "%source%"=="" set source=%source%
if "%source%"=="" goto input_source
if "%source%"=="0" exit
if "%source%"=="1" goto backup_task
echo "%source%"|find "%subarea1%:\" > nul
if "%ERRORLEVEL%"=="1" goto input_source
if "%source%"=="%subarea1%:\" goto forbid
if "%source%"=="%subarea1%:\\" goto forbid
if "%source%"=="%subarea1%:\." goto forbid
if not exist "%source%" goto source_noexist
goto input_destination


New code

set _i=
set source=
set /p source= Please enter the path of the file to be backed up:
for %%s in (%source%) do set /a _i+=1
if "%_i%"=="1" for %%p in (%source%) do set source=%%~p
:: The above two sentences are to judge whether there are quotes in %source%, and if there are, remove them
if "%source%"=="" goto input_source
if "%source%"=="0" goto :eof
if "%source%"=="1" goto backup_task
if not exist "%source%" goto source_noexist
for %%p in ("%source%") do if "%%~fp"=="%%~dp\" goto forbid
goto input_destination
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
1 2 3 8 Next ›
Forum Jump: