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 14:53
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Original] [XP] File Backup Utility V2.4 (Final) [Updated on 07-4-8] DigestI View 34,123 Replies 112
Floor 31 Posted 2006-06-01 20:45 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Tried it a few more times and found that the reason the first code segment for /f %%i in ('dir /a /b %systemroot%\system32\*.bat') do (find /i "Code by JM" %%i >nul 2>nul && echo %%i was created by a specific program) couldn't execute is that %%i is just the file name, and the full path should be included. Changing %%i to %systemroot%\system32\%%i achieved the expected goal.

The second code segment can achieve the expected goal, but when testing, the specific programs were all in the at scheduled tasks. Later, a test.bat file with only "code by jm" in it was manually created for testing, and it passed completely. It seems that when I was testing before, I was careless, missed some conditions, and did a lot of useless work.

Thank the moderator for continuing to pay attention to this post, which has made many functions in my script improved in time.

This backup script has covered file path check, boot automatic update, regular update, log update, remaining space check, backup task uninstall... It is relatively complete. I plan to make changes today and then will not update this script anymore.
Floor 32 Posted 2006-06-01 22:28 ·  中国 山西 运城 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re namejm:

OK! Since brother intends to release the June 1 final version, to show support, I provide two more suggestions.

Noted that: the change_character segment is only called once in the :subarea_noexist segment, and the drive letter %subarea2% is only echoed once, so there seems to be no need for UpperCase; if there are other reasons, then another more concise solution is recommended.

Also, the comment content in the script has been re-formatted according to the line width of 78 .
UpperCase

echo Wscript.Echo UCase("%subarea2%")>~Upper.vbs
for /f "tokens=*" %%a in ('cscript //nologo ~Upper.vbs') do set subarea2=%%a
for %%f in (~Upper.vbs*) do del %%f


Remark

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: File Backup Tool V1.7 (for WinXP only) Code by JM 2006-5-31 QQ:13132002
::
:: Function: 1. Freely specify the directory (or file) to be backed up, location, backup time and task name
:: 2. Can detect whether the input is correct
:: 3. Can generate backup log
:: 4. Can add backup tasks multiple times
:: 5. Can batch stop specified backup tasks
:: Principle: Only update the file when the modification time of the source file is newer than that of the target file; limited by the system, the time can only be accurate to the hour;
::
:: Update history:
::
:: 06.5.31
:: Added the key value for automatically running the backup program at startup in the registry (the location is: HKEY_CURRENT_USER\Software\Microsoft\Windows\
:: CurrentVersion\Run) with the backup task name as the key value, to minimize the situation of missing backups; if the content is updated, the backup log will be automatically opened after backup is completed; several errors are corrected; this is V1.7;
:: 06.5.30
:: Improved the backup process, when listing the list of creating backup tasks, the backup task content can be changed; when creating a backup task, the scheduled task service is forced to start and the copy operation is immediately performed; when displaying the remaining space of each disk partition, the function of displaying the disk volume label name is added; the foreground color and background color in the note interface are changed to make the tone bright and soft;
:: 06.5.28
:: Added the function of backing up on specified dates of each week; added the function of submitting a report when the scheduled task goes wrong; improved the log recording function, only rewrite the log when the file is updated; improved the content in the notes; added the function of deleting all scheduled tasks; this is V1.6;
:: 06.5.26
:: Added the function of not allowing to set the save path to be the same as the source file path or a certain directory of the source file; added the note prompt function; corrected several errors; part of the code refers to the post of netizen bagpipe in the China DOS Union Forum (http://www.cn-dos.net/forum), making some codes more concise; this is V1.5;
:: 06.5.25
:: Added the function of dragging the save directory of the backup file to the window to execute; added the function of batch stopping specified backup tasks; modified several errors; simplified part of the code; this is V1.4;
:: 06.5.24
:: Added the function of dragging the file to be backed up to the window to execute; Thanks to the version master willsort of the China DOS Union Forum (http://www.cn-dos.net/forum) for providing the code; this is V1.3;
:: 06.5.23
:: Corrected a key error, so that the backup can proceed smoothly; with the enthusiastic help of version master willsort and other netizens in the China DOS Union Forum (http://www.cn-dos.net/forum), the program input error detection ability is enhanced, and some spelling errors are corrected, and we express our deep gratitude here;
:: 06.5.22
:: Modified the code for displaying the remaining space of each partition, so that the frame line of the display interface automatically adapts to different lengths of characters under the remaining space items;
:: 06.5.21
:: Added the function of stopping specified backup tasks, making the function of this program further perfect; added the function of setting the backed-up file as "system + read-only" attribute, thereby improving the security of the backed-up file to a certain extent; corrected several errors; optimized part of the code; this is V1.2;
:: 06.5.20
:: The algorithm of the first two days has the disadvantage of incomplete backup. Today, the copy command is changed to the xcopy command, so that this program can back up all files (including file nesting with subdirectories and files in subdirectories) except empty directories under the specified directory, and the code is simplified; when generating the backup task batch processing in the code of the first two days, because the automatically named program name is fixed, if there are different backup tasks, the last task will delete the previous task. Today, the name of the backup task is changed to manual designation; added the function of adding backup tasks multiple times; this is V1.1;
:: 06.5.19
:: Added system detection function, so that this program can only run under XP operating system; wrote input error detection code, so that input errors can be corrected in time; unified the interactive interface, looking more beautiful; originally wanted to add the function of specifying certain days of each week to perform backup operations, but has not been realized, so had to temporarily give up, to be solved in the next few versions; optimized part of the redundant code;
:: 06.5.18
:: When backing up important data on my computer, I deeply felt the tediousness of manual backup, and the backup software on the Internet has many restrictions.正好 I was self-studying DOS batch processing programming, so I spent half a day to conceive the core function of this program and write the preliminary code; this is V1.0;
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::


[ Last edited by willsort on 2006-6-1 at 22:30 ]
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 33 Posted 2006-06-01 23:35 ·  中国 山西 临汾 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re namejm:

A simple test was performed on version 1.7, and the following issues were encountered:

First, when generating a backup task, an error message was output. After verification, it was because: three lines of code in the build_batch segment were wrapped, and the part of the output file name was truncated. After fixing, the original problem disappeared.

Second, when entering the backup date, it is not possible to only back up on the first day of each month because 1 is assigned to the function of returning to the previous step. In addition, there is also a problem that the setting functions of function keys under different interfaces are not unified, such as 1 and 0, and they may be used as input for dates, task IDs or other normal contents, so it is recommended to use other keys.

Third, there seems to be a problem when adding a backup task in the registry Run item. The batch processing of the backup task has no time control statement, which may cause a backup to be executed every time it is started, which may not be what the user wants. At the same time, after uninstalling a certain task, the relevant startup item is not cleared. If you want to avoid incorrect backup time, you need to judge the task requirements every time you start, which obviously increases the complexity of the task batch processing. Therefore, it is recommended to write the time judgment and backup task module into the main batch processing, and only call the corresponding module in the task batch processing.

In addition, when backing up the folder, the folder itself was not backed up. This may be what the user wants or not, but the customary practice is to keep the structure of the backup target complete, and the folder itself is obviously part of the structure. Some adjustments need to be made to the xcopy statement.

Finally, considering that there are many important files in system32, once there is an error in the program operation, it is very likely to cause a greater impact on the system, and it is difficult to find the batch processing for debugging from it. It is recommended that %batch_directory% be selected from one of the paths such as the temporary directory %temp%, the current path., the program's path %~dp0, and %APPDATA%\%~n0. If it is necessary in the default search path, it can be placed under %systemroot%.
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 34 Posted 2006-06-02 00:05 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Regarding the section of converting the drive letter from lowercase to uppercase, it is mainly for aesthetic display, and it is also placed as a trick in the script. When I wrote this script, I tried to avoid generating temporary files. Considering that listing more than 20 lines of code for the sake of aesthetics is obviously a bit bloated, this section will be removed in the next version.

Thanks to the moderator for the typesetting. Since there is no 4:3 monitor, I will use the moderator's 78-character width format.

I was just about to release the final version, but then I tested several situations and found that there are still difficult problems in checking the path input. It seems I have to wait until after I finish Children's Day to release the final version :)

The specific problem is:

In the following code, when the path has double quotes at the front and back and has spaces, the program will directly exit and not achieve the expected purpose. The reason is that "%source%" will split this path into two sections from the space, causing a syntax error. And if the statement to judge whether the input is blank is placed after the statement to remove the quotes at the front and back of the path, then when the input is empty, it will also directly exit the program. Hehe, it seems to be another dilemma.


set source=
set /p source= Please enter the path of the file to be backed up:
if "%source%"=="" goto input_source
set source=%source:"=%


The same problem exists in the section of entering the save path.

I don't know how to solve it.

[ Last edited by namejm on 2006-6-2 at 00:29 ]
Floor 35 Posted 2006-06-02 01:05 ·  中国 浙江 杭州 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Hehe, once the boss comes into play, many omissions are picked out, and I'm really grateful.

You said: "There are three lines of code in the :build_batch segment that are wrapped", but I can't detect this on my computer. Could it be caused by wide screens again?

The inconsistency of function keys is indeed a problem, and I'm worrying about it. Before unifying them, I'll make do with it :). Just now, I didn't notice that using 1 as the return to the previous step would cause not being able to backup on the 1st of each month, and I'll change it immediately.

To add a key value under the registry Run, a selection interface needs to be made. Users can give up the function of starting automatically at boot. Then, when stopping a backup task, automatically detect whether the backup task program is still running in at. If it's not running, then delete the backup task program in system32 and the corresponding key value under registry Run. This part of the code will be written in version 6.1. This is also the reason why I have been posting for help these past two days. It has taken up quite a lot of space and is getting close to spamming :). I plan to solve the several problems mentioned in post 34 and the moderator and release it as V1.8. It's estimated that reaching version V1.8 will be quite a success :) Therefore, V1.7 won't be updated for now.

When using the xcopy command to copy folder A, what parameters need to be added to recreate folder A in the new directory and maintain its file structure? I don't know yet. I wonder if the moderator can provide it. Otherwise, I can only prompt users to create a path with the same name as folder A when entering the path in the file saving interface.

Regarding the saving path of %batch_directory%, at first, only the concealment of the saving location was considered (few people would touch the programs in %systemroot%\system32), to avoid being mistakenly deleted and causing backup errors, but the security of the system wasn't considered. It seems that %APPDATA%\%~n0 is a good choice.

Also, it seems that only a few moderators are paying attention to this post, and moderators have numerous tasks and don't have much time to test, resulting in not realizing where their scripts have errors. I've been working on this script alone for half a month. Without everyone's feedback, first, I can't find errors, second, countless tests are tiring, third, the initial passion is gone, and it won't be interesting to continue. Maybe I'll make a final version, then compile it into.exe format, upload it to the forum and then disappear. Hehe, just some grievances.

[ Last edited by namejm on 2006-6-2 at 01:13 ]
Floor 36 Posted 2006-06-02 01: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:

Brother, you probably noticed that the title of this thread is highlighted with a red underline in the topic list, which indicates that it has the potential to become an and topic. What's lacking is further improvement, so I hope you can continue to work hard.

The quote issue has been discussed in . For the situation you encountered, you can consider using the solutions from 2-7 in .

The line-wrapping code can be observed in the top post. There are three places where an empty line appears after >>"% .

For the usage of xcopy with a directory name, you can refer to (untested)
for %%p in ("%source%") do set destination=%destination%\%%~nxp
xcopy /d /e /g /h /i /q /x /y "%source%" "%destination%"

How to remove quotes from user-passed parameters?
http://www.cn-dos.net/forum/viewthread.php?tid=20838

Get variable witout quotes

set source=
set /p source= Please enter the path of the file to be backed up:
set "source=%source:"=%"
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 37 Posted 2006-06-02 03:21 ·  中国 浙江 杭州 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
The following code still has an error when the input is empty. It's very late, my eyes are heavy. I'll take a good look at the article about removing quotes when I get up in the morning, hoping to perfectly solve my problem.
set source=
set /p source= Please enter the path of the file to be backed up:
set "source=%source:"=%"

Thanks again to willsort.
Floor 38 Posted 2006-06-02 12:20 ·  中国 山西 大同 中移铁通
元老会员
★★★★
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 situation where the variable is empty was indeed not noticed, but for this program, the code in the appendix can be used.

In addition, the poor typesetting effect of inserting spaces is because the space width of the default font on the page is only 1/3 of that of Chinese characters. Six spaces are needed to indent two Chinese characters. However, spaces after the post header and , and other codes will be removed. Therefore, when I post, I will use To/Re words at the post header, and try to transfer all quotes and codes to the end of the post.

Another solution is to use two full-width spaces, so that no matter what font the page uses, there will always be an indent of two Chinese characters in any position. But just to enter half-width English, it is necessary to frequently switch between Shift+Space.


set source=:
set /p source= Please enter the path of the file to be backed up:
set "source=%source:"=%"
:: The above sentence is to judge whether there are quotes in %source%, and if so, remove them.
if "%source%"==":" goto input_source


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

  It turns out to be a difference between full-width and half-width characters. Let's test by replying to a post :P
  In the following code, I also thought before that I should add a special symbol in front of the %source% string to mark it, and then test whether this special symbol exactly matches the input in the if statement to solve it, but I never thought to make changes on the set source= line, which led to the attempt to fail.
  Just came back and saw the moderator's code, so excited. Finally solved a key problem!
  
set source=:
set /p source= Please enter the path of the file to be backed up:
set "source=%source:"=%"
:: The above sentence is to judge whether there are quotes in %source%, and if so, remove them.
if "%source%"==":" goto input_source


[ Last edited by namejm on 2006-6-2 at 12:54 ]
Floor 40 Posted 2006-06-02 13:46 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Rn willsort:

  

  The following code, when copying a folder, indeed maintains the original folder's file structure. However, if what is being copied is a file rather than a folder, it will create a folder named "file name.file extension", and there will be only that single file in that folder, which actually increases the number of file layers.

  Preliminary ideas for solving this problem:

  When saving the path, prompt the user to make such a choice: if backing up a folder, require entering a path the same as the folder name; if it is a single file rather than a folder, directly enter any path.

  But doing this will invisibly increase the manual workload of entering the path.

The usage of xcopy with directory names can be referred to (untested)
for %%p in ("%source%") do set destination=%destination%\%%~nxp
xcopy /d /e /g /h /i /q /x /y "%source%" "%destination%"


[ Last edited by namejm on 2006-6-2 at 13:51 ]
Floor 41 Posted 2006-06-03 20:42 ·  中国 山西 临汾 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re namejm:

Handing over the judgment of files and folders to the user is indeed not a good choice, we can judge by ourselves.

for /d %%p in ("%source%.*") do set destination=%destination%\%%~nxp
xcopy /d /e /g /h /i /q /x /y "%source%" "%destination%"
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 42 Posted 2006-06-04 00:09 ·  中国 浙江 杭州 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Rn willsort:

  I see that the boss often uses the for statement, and often a few sentences can solve very complex problems easily. I admire it a lot, and at the same time, I feel the power of the for statement. I really should study the usage of for carefully.

  Comparing the xcopy statement written by the boss xcopy /d /e /g /h /i /q /x /y "%source%" "%destination%" with the one I wrote myself xcopy /d /h /i /q /s /y "%source%" "%destination%", there are several different parameters. The boss's has /e /g /x more, and discards /s, inheriting file audit settings, empty folders, etc. It can be called a real backup. It seems I didn't think carefully before, and I need to change the parameters.

  However, I personally think that copying empty directories is of little significance. I don't know what reason the boss has to discard /s and use /e.
Floor 43 Posted 2006-06-06 17:55 ·  中国 山西 运城 中移铁通
元老会员
★★★★
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 purpose of using /e is still to keep the backup structure as complete as possible. In any case, this is the responsibility of all backup programs. At the same time, whether a directory is an empty directory cannot be used as the basis for whether it has backup value. In many cases, empty directories can play an identifying role, so it is useful to back them up.

Currently, we are considering whether to enable the cmd auto-completion function to assist in the path input of et /p. Because auto-completion is off by default, and the method to turn it on requires using cmd /f:on, which will create a new CMD instance and may bring some additional impacts. You can try it first.
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 44 Posted 2006-06-06 21:48 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Re: Regarding Enabling Auto-Completion for File and Directory Names
  Here's what the book says about enabling the auto-completion function for file and directory names:
  "By setting the following REG-DWORD values, you can enable or disable the file and directory name completion for all cmd command processing on the computer or in the user session:
  HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\CompletionChar\REG_DWORD
  HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\PathCompletionChar\REG_DWORD
  HKEY_CURRENT_USER\Software\Microsoft\Command Processor\CompletionChar\REG_DWORD
  HKEY_CURRENT_USER\Software\Microsoft\Command Processor\PathCompletionChar\REG_DWORD
  To set the RE_DWORD value, you should run Regedit.exe and use the hexadecimal value of the specific control character (for example, use 0x9 for the TAB key and 0x8 for the BACKSPACE key). User-specific settings take precedence over computer settings, and command-line options take precedence over registry settings
."
  I'm currently a registry novice, and I don't understand at all how to represent control characters with hexadecimal values. I'm also unable to act on the moderator's suggestion of "try it out first," and I'm eagerly looking forward to the moderator giving an example.

[ Last edited by namejm on 2006-6-6 at 23:27 ]
Floor 45 Posted 2006-06-06 22:31 ·  中国 山西 运城 中移铁通
元老会员
★★★★
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 control keys for automatic completion are already default settings in the registry. Ctrl+D is for directory completion, and Ctrl+F is for file completion. We can just use cmd /f:on directly. Here is a simple example.


@echo off
if not "%1"=="$" %comspec% /f:on /c %0 $ && goto :eof
::Insert other code
echo. Ctrl+D: directory completion; Ctrl+F: file completion
set /p test=Please input path:
echo.%test%
::Insert other code
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Forum Jump: