|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 31 楼』:
使用 LLM 解释/回答一下
又试了几次,发现第一段代码for /f %%i in ('dir /a /b %systemroot%\system32\*.bat') do (find /i "Code by JM" %%i >nul 2>nul && echo %%i由特定程序创建)不能执行的原因是%%i只是文件名,应该带上完整的路径,改%%i为%systemroot%\system32\%%i后达到预期目标.
而第二段代码是能够达到预期目标的,只是在试的时候,特定程序都在at计划任务中,后来手动建立了一个内容只有code by jm的test.bat文件测试,完全通过,看来是自己的测试的时候,丢三落四的,遗漏了一些条件,做了很多无用功.
感谢版主对本帖的持续关注,使得我的脚本中很多功能得到及时的完善.
这个备份脚本已经涵盖了文件路径检查、开机自动更新、定时更新、日志更新、剩余空间检查、备份任务卸载……做得比较完善,打算今天更改完之后,将不再对这个脚本作更新.
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.
|
|
2006-6-1 20:45 |
|
|
willsort
元老会员
         Batchinger
积分 4432
发帖 1512
注册 2002-10-18
状态 离线
|
『第 32 楼』:
使用 LLM 解释/回答一下
Re namejm:
OK!既然兄打算出六一终结版,为表示支持,再提供两点建议。
注意到:change_character段仅在:subarea_noexist段中被调用了一次,而盘符%subarea2%也只是简单的回显了一次,似乎没有UpperCase的必要;如果有其他理由,那么推荐另外一种更简洁的方案。
另外,将脚本中的注释内容按照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
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: 文件备份器V1.7(for WinXP only) Code by JM 2006-5-31 QQ:13132002
::
:: 功能:1、自由指定要备份的目录(或文件)、位置、备份时间和任务名称
:: 2、能检测输入是否正确
:: 3、能生成备份日志
:: 4、能多次添加备份任务
:: 5、能批量停止指定的备份任务
:: 原理:只有当源文件修改时间比目标文件的修改时间新时才更新文件;受系统限制,
:: 时间只能精确到小时;
::
:: 更新历史:
::
:: 06.5.31
:: 在注册表中( 位置是:HKEY_CURRENT_USER\Software\Microsoft\Windows\
:: CurrentVersion\Run) 增添了开机自动运行备份程序的键值( 以备份任务名称作
:: 为键值) ,尽可能减少错过备份的情况;如果更新了内容,则备份完毕后自动打
:: 开备份日志;修正了几处错误;此为V1.7;
:: 06.5.30
:: 完善了备份流程,在列出创建备份任务的清单时,可更改备份任务内容;在
:: 创建备份任务的同时,强制启动计划任务服务,并立即作复制操作;显示各磁盘
:: 分区剩余空间情况的时候,增加了显示磁盘卷标名的功能;更改了注意事项界面
:: 中的前景色和背景色,使色调显得明快而柔和;
:: 06.5.28
:: 增加了在每周的指定日期进行备份的功能;添加了当计划任务出错时提交报
:: 告的功能;完善了日志记录功能,只有当文件更新时才会改写日志;完善了注意
:: 事项中的内容;增加了删除所有计划任务的功能;此为 V1.6 ;
:: 06.5.26
:: 增加了不允许把保存路径设置为与源文件路径相同、或者为源文件某一目录
:: 的功能;添加了注意事项提示功能;修正了几处错误;部分代码参照中国DOS 联
:: 盟论坛(http://www.cn-dos.net/forum) 网友bagpipe 所发帖子,使一些代码得
:: 到精简;此为V1.5;
:: 06.5.25
:: 增加了把备份文件的保存目录拖曳到窗口执行的功能;增加了批量停止指定
:: 备份任务的功能;修改了几处错误;精简了部分代码;此为V1.4;
:: 06.5.24
:: 增加了把要备份的文件拖曳到窗口执行的功能;感谢中国DOS 联盟论坛
:: (http://www.cn-dos.net/forum)版主willsort提供代码;此为V1.3;
:: 06.5.23
:: 修正了一处关键错误,使得备份能顺利进行下去;在中国DOS 联盟论坛
:: (http://www.cn-dos.net/forum)版主willsort等网友的热心帮助下,增强了程
:: 序输入错误检测能力,并修正了一些拼写错误,在此深表谢意;
:: 06.5.22
:: 修改了显示各分区剩余空间的代码,使显示界面的框线自动适应剩余空间条
:: 目下不同长度的字符;
:: 06.5.21
:: 增添了停止指定备份任务的功能,使本程序的功能进一步完善;增添了将备
:: 份后的文件设置为" 系统+ 只读" 属性的功能,从而在一定程度上提高了备份后
:: 的文件的安全;修正了几处错误;优化了部分代码;此为V1.2;
:: 06.5.20
:: 头两天的算法存在备份不完全的弊端,今天把copy命令更改为xcopy 命令,
:: 从而使本程序能够备份指定目录下除空目录之外的所有文件( 包括目录之下还有
:: 子目录、子目录内含有文件的文件嵌套) ,并使代码得到了精简;头两天的代码
:: 生成备份任务批处理的时候,因为自动命名的程序名称是固定的,如果有不同的
:: 备份任务的话,最后一次任务将把上一次的任务删除,今天把备份任务的命名改
:: 为手动指定;增添了多次添加备份任务的功能;此为V1.1;
:: 06.5.19
:: 增加了系统检测功能,使得该程序只能运行于XP操作系统下;编写输入错误
:: 检测代码,使输入错误能得到及时更改;统一了交互界面,看上去更加美观;本
:: 来想增加指定每周的某几天进行备份操作的功能,但是一直未能实现,只好暂时
:: 放弃,留待下几个版本解决;优化了部分冗余代码;
:: 06.5.18
:: 在对本人电脑上重要资料进行备份的时候,深感手工备份的烦琐,而网上的
:: 备份软件又有诸多限制,正好本人在自学DOS 批处理编程,于是花了半天的时间
:: 来构思本程序的核心功能,编写初步代码;此为V1.0;
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Last edited by willsort on 2006-6-1 at 22:30 ]
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:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得! |
|
2006-6-1 22:28 |
|
|
willsort
元老会员
         Batchinger
积分 4432
发帖 1512
注册 2002-10-18
状态 离线
|
『第 33 楼』:
使用 LLM 解释/回答一下
Re namejm:
对1.7版进行了简单的测试,遇到以下问题:
首先,在生成备份任务时,有错误信息输出,经查证,是因为:build_batch段中有三行代码被折行,其中的输出的文件名部分被截断。经修复后,原问题消失。
其次,在输入备份日期时,无法只在每月1日备份,因为1分配给了返回到上一部的功能。另外,也存在着不同界面下功能键的设定功能不统一的问题,比如1和0,而他们有可能作为日期、任务ID或者其他正常内容的输入,所以建议换用其它的键。
再次,在注册表Run项中添加备份任务似存在问题,备份任务的批处理没有时间控制语句,有可能会导致每次启动时执行一次备份,而这未必是用户所希望的。同时,在卸载某个任务后,相关的启动项并没有被清除。如果想避免错误备份时间,就需要在每次启动时,对任务要求进行判断,这显然增大了任务批处理的复杂程度,所以建议将时间判断和备份任务模块写入到主批处理中,而只在任务批处理中调用相应的模块。
另外,在备份文件夹时,并未将文件夹本身备份,这可能是用户所希望的,也可能是所不希望的,但是习惯上的做法是保持备份目标的结构完整,而文件夹本身显然也是结构的一部分,这需要在xcopy语句上作一些调整。
最后,考虑到system32中有许多重要文件,一旦程序运行出现差错,很可能对系统造成较大影响,从其中找到批处理进行调试也很困难,建议%batch_directory%从临时目录%temp%,当前路径.,程序所在路径%~dp0和%APPDATA%\%~n0 等路径中选择其一。如果却需要在默认搜索路径,或可放在%systemroot%下。
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:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得! |
|
2006-6-1 23:35 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 34 楼』:
使用 LLM 解释/回答一下
关于盘符小写转换为大写一节,主要是为了显示的美观,同时也作为一个技巧放在脚本里.我写这个脚本的时候,尽量避免产生临时文件.考虑到为了美观而罗列了20多行代码,显然有点臃肿,在下个版本中将去除该节.
感谢版主的排版,因为没4:3的显示器,那就采用版主78行宽的格式吧.
正打算出终结版本呢,结果又测试了几种情况,发现对路径输入的检查还有难以解决的问题,看来得等我把儿童节过完之后才能出终结版本:)
具体问题是:
在下面的代码中,当路径带前后双引号且带空格时,将直接退出程序,达不到预期的目的,原因是当"%source%"会将此路径从空格处分成两节,造成语法错误,而如果将判断是否输入空白的语句放到去除路径前后引号的语句之后,则当输入为空时同样会直接退出程序.呵呵,好象又是一个两难的选择.
set source=
set /p source= 请输入要备份文件的路径:
if "%source%"=="" goto input_source
set source=%source:"=%
输入保存路径一段也存在同样的问题.
不知道该如何解决.
Last edited by namejm on 2006-6-2 at 00:29 ]
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 ]
|
|
2006-6-2 00:05 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 35 楼』:
使用 LLM 解释/回答一下
嘿嘿,老大一出马,就挑出了很多疏漏之处,感激不尽.
你说":build_batch段中有三行代码被折行",在我的电脑上检查不出来,难道这又是宽屏惹的祸?
功能键不统一确实是个问题,正在头痛,在未能统一之前,将就用一下吧:)只是把1作为返回上一步而导致不能在每月的1号备份,当时确实没注意到,马上就改.
在注册表Run下添加键值,得做个选择界面,用户可以放弃开机自启动的功能,然后在停止某项备份任务的时候,自动检测备份任务程序是否还在at中运行,如果不再运行的话,则删除system32下的备份任务程序和注册表Run下相应的键值,这部分的代码6.1的时候才写出来,这也是我这两天接连发帖求助的原因.占用了不少版面,离灌水已经很近了:)打算把34楼及版主提到的几个问题一并解决,做成V1.8再放出来.估计做到V1.8版本就已经功德圆满了:)因此V1.7暂时不作更新.
xcopy命令复制A文件夹时,要加什么参数才能在新的目录下重建A文件夹并保持其下的文件结构,我现在还不得而知,不知道版主能不能提供.要不然,就只能在保存文件的界面中输入路径的时候,提示用户建立一个与A文件夹同名的路径了.
关于%batch_directory%保存路径问题,当初只考虑到保存位置的隐蔽性(很少有人去动%systemroot%\system32下的程序),以免被错误删除导致备份错误,没考虑到系统的安全性问题,看来%APPDATA%\%~n0 是个不错的选择.
另外,这个帖好象就只几个版主在关注,而版主们事务繁杂,没什么时间测试,导致自己的脚本什么地方出了错都浑然不觉.这个脚本自己一个人陆续弄了半个月,没有大家的反馈,一是找不到什么错误,二是无数次的测试也挺累的,三是没有了当初的激情,再做下去就没什么意思了.可能到时候会弄个final版,然后编译成.exe格式,传到坛子上之后就溜之大吉了.呵呵,牢骚话.
Last edited by namejm on 2006-6-2 at 01:13 ]
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 ]
|
|
2006-6-2 01:05 |
|
|
willsort
元老会员
         Batchinger
积分 4432
发帖 1512
注册 2002-10-18
状态 离线
|
『第 36 楼』:
使用 LLM 解释/回答一下
Re namejm:
兄大概注意到了,在主题列表中本主题的标题被红色下划线高亮,这表明它有成为和主题的潜质,所欠缺的只是进一步完善,所以希望兄能继续努力。
引号问题在中已经讨论过了,对于你遇到的情形,可以考虑使用其中2-7的方案。
折行代码在顶楼帖中可以观察出来,有三处在>>"%之后出现一个空行。
xcopy带目录名的用法,可以参考(未经测试)
for %%p in ("%source%") do set destination=%destination%\%%~nxp
xcopy /d /e /g /h /i /q /x /y "%source%" "%destination%"
如何从用户传入的参数中去掉引号?
http://www.cn-dos.net/forum/viewthread.php?tid=20838
Get variable witout quotes
set source=
set /p source= 请输入要备份文件的路径:
set "source=%source:"=%"
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:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得! |
|
2006-6-2 01:30 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 37 楼』:
使用 LLM 解释/回答一下
下面这段代码在输入为空的时候仍然会出错.天色已很晚了,眼皮在打架,早上起来再好好看一下去除引号的文章,希望能完美地解决我的问题.
set source=
set /p source= 请输入要备份文件的路径:
set "source=%source:"=%"
再次感谢willsort.
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.
|
|
2006-6-2 03:21 |
|
|
willsort
元老会员
         Batchinger
积分 4432
发帖 1512
注册 2002-10-18
状态 离线
|
『第 38 楼』:
使用 LLM 解释/回答一下
Re namejm:
确未注意到变量为空的情形,不过,对于本程序来说,可以使用附录的代码。
另外,插入空格的排版效果不佳,是因为页面中缺省字体的空格宽度只有汉字的1/3,需要使用6个空格,才能空出两个汉字的缩进。但是,在帖首和、 等代码后的空格会被剔除,因此我在发帖时帖首会使用To/Re字样,所有的引用和代码尽量转移到帖尾。
另外的解决的方法是,使用两个全角空格,这样不论页面使用何种字体,在任何位置都会保持两个汉字的缩进。只是为了输入半角的英文,需要进行频繁的Shift+空格切换。
set source=:
set /p source= 请输入要备份文件的路径:
set "source=%source:"=%"
:: 上面这句为判断%source%中是否存在引号,有则剔除。
if "%source%"==":" goto input_source
Last edited by willsort on 2006-6-2 at 12:24 ]
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:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得! |
|
2006-6-2 12:20 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 39 楼』:
使用 LLM 解释/回答一下
re willsort:
原来是全角和半角的差异,回个帖子测试一下:P
在下面的代码中,以前也想过应该在%source%字符串前加个特殊符号来标注一下,然后在if语句中测试这个特殊符号与输入是否完全匹配来解决,但是一直没想到要在set source=这句上作改动,导致尝试以失败告终。
刚回来就看到了版主的代码,好激动。终于把一个关键问题给解决掉了!
set source=:
set /p source= 请输入要备份文件的路径:
set "source=%source:"=%"
:: 上面这句为判断%source%中是否存在引号,有则剔除。
if "%source%"==":" goto input_source
Last edited by namejm on 2006-6-2 at 12:54 ]
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 ]
|
|
2006-6-2 12:50 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 40 楼』:
使用 LLM 解释/回答一下
Rn willsort:
以下代码复制文件夹的时候,确实能保持原有文件夹的文件结构,但是,如果要复制的是文件而非文件夹的时候,就会建立一个以"文件名.后缀名"为名字的文件夹,该文件夹下将只有孤零零的一个文件,反倒加深了文件的层数。
解决此问题的初步设想:
在保存路径的时候,提示用户作如此选择:如果要备份的是文件夹时,要求输入与文件夹名相同的路径;如果是单个的文件而非文件夹时,直接输入任意路径。
但是这样做的话,无形中会加大手动输入路径的工作量。
xcopy带目录名的用法,可以参考(未经测试)
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 ]
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 ]
|
|
2006-6-2 13:46 |
|
|
willsort
元老会员
         Batchinger
积分 4432
发帖 1512
注册 2002-10-18
状态 离线
|
『第 41 楼』:
使用 LLM 解释/回答一下
Re namejm:
把文件和文件夹的判断转交给用户,的确不是一个好的选择,我们可以自行判断。
for /d %%p in ("%source%.*") do set destination=%destination%\%%~nxp
xcopy /d /e /g /h /i /q /x /y "%source%" "%destination%"
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:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得! |
|
2006-6-3 20:42 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 42 楼』:
使用 LLM 解释/回答一下
Rn willsort:
看到老大经常用for语句,往往一两句就能使很复杂的问题迎刃而解,万分PF,同时也感到for语句的强大,真该好好揣摩一下for的用法了。
对比了一下老大写的xcopy语句xcopy /d /e /g /h /i /q /x /y "%source%" "%destination%"和我自己写的xcopy /d /h /i /q /s /y "%source%" "%destination%",有好几个参数不太一样,老大的比我多了/e /g /x,舍弃了/s,把文件的审核设置、空文件夹等都继承过来了,称得上是真正的备份,看来自己考虑不周,得该一下参数了。
不过,个人觉得复制空目录意义不大,不知道老大是基于什么原因要舍弃/s而用/e。
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.
|
|
2006-6-4 00:09 |
|
|
willsort
元老会员
         Batchinger
积分 4432
发帖 1512
注册 2002-10-18
状态 离线
|
『第 43 楼』:
使用 LLM 解释/回答一下
Re namejm:
使用/e的目的仍然在于尽可能地保持备份结构的完整,无论如何,这是所有备份程序的责任。同时,一个目录是否为空目录,并不能作为它是否具有备份价值的依据,在很多情况下,空目录可以起到标识性的作用,所以备份它是有用的。
目前,正在考虑是否启用cmd自动完成功能来辅助et /p的路径输入,因为自动完成缺省是关闭的,而打开的方法需要使用cmd /f:on,这会创建一个新的CMD实例,可能会带来一些附加的影响,你可以先行使用一下。
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:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得! |
|
2006-6-6 17:55 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 44 楼』:
使用 LLM 解释/回答一下
Re willsort:
关于启用文件和目录名的自动完成功能,书上是这样说的:
“通过设置下述REG-DWORD值,可以在计算机上或用户会话中启用或禁用所有cmd命令处理的文件和目录名完成:
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
要设置RE_DWORD值,应运行Regedit.exe并使用特定功能的控制字符的十六进制值(例如,用0×9表示TAB键,用0×8表示BACKSPACE键)。用户特定设置优先于计算机设置,并且命令行选项优先于注册表设置。"
本人目前还属于注册表菜鸟,并且对用16进制值来表示控制字符一窍不通,对版主提出来的"先行使用一下"的建议也是心有余而力不足,急切盼望版主能给个示例。
Last edited by namejm on 2006-6-6 at 23:27 ]
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 ]
|
|
2006-6-6 21:48 |
|
|
willsort
元老会员
         Batchinger
积分 4432
发帖 1512
注册 2002-10-18
状态 离线
|
『第 45 楼』:
使用 LLM 解释/回答一下
Re namejm:
注册表中的已经缺省设置自动完成的的控制键,Ctrl+D为目录完成,Ctrl+F为文件完成。我们可以不用管它,直接使用cmd /f:on 即可,下面是一个简单的示例。
@echo off
if not "%1"=="$" %comspec% /f:on /c %0 $ && goto :eof
::插入其它的代码
echo. Ctrl+D: directory completion; Ctrl+F: file completion
set /p test=Please input path:
echo.%test%
::插入其它的代码
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:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得! |
|
2006-6-6 22:31 |
|
|