|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 16 楼』:
使用 LLM 解释/回答一下
Originally posted by willsort at 2006-5-25 22:30:
Re namejm:
set _i=
set source=
set /p source= 请输入要备份文件的路径:
for %%s in (%source%) do set /a _i+=1
if "%_i%"=="1" for %%p in (%source%) do set source=%%~p
:: 以上两句为判断%source%中是否存在引号,有则剔除
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
当把程序放在带空格的目录下执行时,当输入的路径为 当前盘符+冒号的时候,并不能禁止全盘复制.而放到不带空格的目录下执行,或者是输入的路径为 任意盘符+冒号+\的时候,则没问题,不知道是为什么.
Last edited by namejm on 2006-5-26 at 00:12 ]
Originally posted by willsort at 2006-5-25 22:30:
Re namejm:
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 so, 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
When the program is executed in a directory with spaces, when the entered path is current drive letter + colon, it cannot prevent full disk copying. But when it is executed in a directory without spaces, or when the entered path is any drive letter + colon + \, there is no problem. I don't know why.
Last edited by namejm on 2006-5-26 at 00:12 ]
|
|
2006-5-25 23:33 |
|
|
willsort
元老会员
         Batchinger
积分 4432
发帖 1512
注册 2002-10-18
状态 离线
|
『第 17 楼』:
使用 LLM 解释/回答一下
Re namejm:
这其中的原因在于,路径中的单纯使用盘符+冒号就意味着引用的是所指定磁盘的当前路径。所以在命令行直接使用盘符+冒号,会跳转到所指定磁盘的当前路径,而并非一定是根路径。
所有存在的磁盘都有当前路径,并且同时有效;当我们未用cd或其它方式改变它时,通常为根路径。使用cd "d:\Program files"会改变D盘的当前路径,而不论当前盘是否是D盘;也而使用copy c:*.* d:会将C盘当前路径下所有文件拷贝到D盘当前路径下,而不论当前处于哪个盘。
这应该算是一个固有的路径引用技巧,从DOS到CMD一直存在,如同. 和..一样。如果你想将d:转义为d盘根路径,相当于改变了命令行的固有约定,反而会对某些人造成困扰。我的新代码中有意测试了非当前盘的当前路径非根路径时的表现(E盘下测试当前路径为“C:\Documents and Settings”的C盘),结果我是满意的,它没有跳到forbid。
Re namejm:
The reason for this is that simply using a drive letter + colon in the path means that the current path of the specified disk is being referenced. So using a drive letter + colon directly in the command line will jump to the current path of the specified disk, not necessarily the root path.
All existing disks have a current path, and they are all valid at the same time; when we have not changed it using cd or other methods, it is usually the root path. Using cd "d:\Program files" will change the current path of drive D, regardless of whether the current drive is D or not; and using copy c:*.* d: will copy all files in the current path of drive C to the current path of drive D, regardless of which drive you are currently on.
This should be considered an inherent path reference technique that has existed from DOS to CMD, just like. and.. do. If you want to escape d: to the root path of drive D, it is equivalent to changing the inherent convention of the command line, which will instead cause confusion for some people. In my new code, I intentionally tested the performance when the current path of a non-current drive is not the root path (testing drive E with the current path of drive C being "C:\Documents and Settings"), and the result is satisfactory, it did not jump to forbid.
|

※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得! |
|
2006-5-26 09:02 |
|
|
willsort
元老会员
         Batchinger
积分 4432
发帖 1512
注册 2002-10-18
状态 离线
|
『第 18 楼』:
使用 LLM 解释/回答一下
Re namejm:
根据bagpipe兄在中的提示,将剔除引号的两句代码改为一句,想来自己也曾有过%大于"大于^的优先级推测,却不知灵活运用,而在各种for/if结构间打转,甚感惭愧!
在此多谢bagpipe兄赐教了!
set source=%source:"=%
如何从用户传入的参数中去掉引号?
http://www.cn-dos.net/forum/viewthread.php?tid=20838&fpage=1
Re namejm:
According to the hint from brother bagpipe in , change the two lines of code that remove the quotes into one line. I once also had a speculation about the priority of % being greater than " greater than ^, but I didn't know how to use it flexibly, and was going around in various for/if structures, feeling quite ashamed!
Thanks a lot to brother bagpipe for the teaching!
set source=%source:"=%
How to remove quotes from parameters passed in by the user?
http://www.cn-dos.net/forum/viewthread.php?tid=20838&fpage=1
|

※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得! |
|
2006-5-26 23:21 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 19 楼』:
使用 LLM 解释/回答一下
已经看到了bagpipe兄发的帖子,已经更新了XP文件备份器相关部分的代码,在此感谢两位高手.
I have seen the post from brother bagpipe, and I have updated the code related to the XP file backup tool. Thanks to the two experts here.
|
|
2006-5-27 00:04 |
|
|
无奈何
荣誉版主
      
积分 1338
发帖 356
注册 2005-7-15
状态 离线
|
『第 20 楼』:
使用 LLM 解释/回答一下
To willsort
关于去除参数中首尾的双引号,好象记得与兄一起讨论过。
见: http://www.cn-dos.net/forum/viewthread.php?tid=17785
第 5 楼 1、项。
To willsort
Regarding removing the double quotes at the beginning and end of parameters, I seem to remember discussing it with you.
See: http://www.cn-dos.net/forum/viewthread.php?tid=17785
Item 1 of post 5.
|

☆开始\运行 (WIN+R)☆
%ComSpec% /cset,=何奈无── 。何奈可无是原,事奈无做人奈无&for,/l,%i,in,(22,-1,0)do,@call,set/p= %,:~%i,1%<nul&ping/n 1 127.1>nul
|
|
2006-5-27 00:18 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 21 楼』:
[求助]怎么提取at命令中"命令行"栏目下的路径?
使用 LLM 解释/回答一下
想把at命令中"命令行"栏目下的路径提取出来,先把at命令结果倒出到一文本文件中,发现格式如下:
状态 ID 日期 时间 命令行
-------------------------------------------------------------------------------
1 每月执行日期: 1 2 3 4 5 6 7...12:40 C:\WINDOWS\system32\家易理财数据备份.bat
2 每月执行日期: 1 2 3 4 5 6 7...19:00 C:\WINDOWS\system32\家易理财数据备份.bat
3 每月执行日期: 1 2 3 4 5 6 7...22:00 C:\WINDOWS\system32\家易理财数据备份.bat
其间有几个方块黑点 (直接copy以上内容保存为文本文件不会出现,但是如果是用at>list.txt命令的话,会发现有方块黑点存在),不知道该格式的行与列是用什么分割的,导致无法用for命令来提取.
请问,我该怎么办?
如果能不产生临时文件就能提取的话,就更好了.
上述问题已经找到了一个解决办法,但是还不够完善,因为还是会产生临时文件,代码如下:
@echo off
del /q "%tmp%\list.txt">nul 2>nul
at>"%tmp%\list.txt"
(for /f "skip=2 tokens=4 delims=\" %%i in (%tmp%\list.txt) do (find /i "Code by JM" %systemroot%\system32\%%i >nul 2>nul && echo %%i由特定程序创建))
del /q "%tmp%\list.txt">nul 2>nul
pause>nul
Last edited by namejm on 2006-5-27 at 11:04 ]
I want to extract the path in the "Command Line" column in the at command. First, I export the result of the at command to a text file, and find the format as follows:
Status ID Date Time Command Line
-------------------------------------------------------------------------------
1 Monthly execution date: 1 2 3 4 5 6 7...12:40 C:\WINDOWS\system32\Jiayi Financial Data Backup.bat
2 Monthly execution date: 1 2 3 4 5 6 7...19:00 C:\WINDOWS\system32\Jiayi Financial Data Backup.bat
3 Monthly execution date: 1 2 3 4 5 6 7...22:00 C:\WINDOWS\system32\Jiayi Financial Data Backup.bat
There are several square black dots in between (Directly copying the above content and saving it as a text file will not have them, but if you use the at>list.txt command, you will find that there are square black dots). I don't know what is used to separate the rows and columns in this format, which makes it impossible to use the for command to extract.
What should I do?
It would be even better if I can extract without generating temporary files.
The above problem has found a solution, but it is not perfect because temporary files are still generated. The code is as follows:
@echo off
del /q "%tmp%\list.txt">nul 2>nul
at>"%tmp%\list.txt"
(for /f "skip=2 tokens=4 delims=\" %%i in (%tmp%\list.txt) do (find /i "Code by JM" %systemroot%\system32\%%i >nul 2>nul && echo %%i created by a specific program))
del /q "%tmp%\list.txt">nul 2>nul
pause>nul
Last edited by namejm on 2006-5-27 at 11:04 ]
|
|
2006-5-27 09:13 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 22 楼』:
at命令为什么显示不出指定每周的某几天
使用 LLM 解释/回答一下
用了如下命令:
at 22:00 /every:M,T,W c:\windwos\system32\家易理财数据备份.bat
用at命令查看清单,结果如下:
状态 ID 日期 时间 命令行
-------------------------------------------------------------------------------
1 每月执行日期: 1 2 3 4 5 6 7...12:40 C:\WINDOWS\system32\家易
理财数据备份.bat
2 每月执行日期: 1 2 3 4 5 6 7...19:00 C:\WINDOWS\system32\家易
理财数据备份.bat
3 每月执行日期: 1 2 3 4 5 6 7...22:00 C:\WINDOWS\system32\家易
理财数据备份.bat
4 每月执行日期:... 22:00 c:\windwos\system32\家易理财数
据备份.bat
难道是我的电脑显示不正常吗?
(另:在论坛里用插入图象的功能,似乎只能插入网络上的图片,难道就不能插入本机上的吗?)
The commands used are:
at 22:00 /every:M,T,W c:\windwos\system32\家易理财数据备份.bat
When checking the list with the at command, the result is as follows:
Status ID Date Time Command line
-------------------------------------------------------------------------------
1 Execution date of every month: 1 2 3 4 5 6 7...12:40 C:\WINDOWS\system32\家易理财数据备份.bat
2 Execution date of every month: 1 2 3 4 5 6 7...19:00 C:\WINDOWS\system32\家易理财数据备份.bat
3 Execution date of every month: 1 2 3 4 5 6 7...22:00 C:\WINDOWS\system32\家易理财数据备份.bat
4 Execution date of every month:... 22:00 c:\windwos\system32\家易理财数据备份.bat
Is it that my computer is not displaying normally?
(Another thing: When using the function to insert an image in the forum, it seems that only images on the network can be inserted. Can't I insert local ones?)
|
|
2006-5-27 09:36 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 23 楼』:
如何提取两个文本文件中有差别的内容?
使用 LLM 解释/回答一下
假设1.txt中内容的格式如下:
状态 ID 日期 时间 命令行
-------------------------------------------------------------------------------
1 每月执行日期: 1 2 3 4 5 6 7...12:40 C:\WINDOWS\system32\家易
理财数据备份.bat
2 每月执行日期: 1 2 3 4 5 6 7...19:00 C:\WINDOWS\system32\家易
理财数据备份.bat
2.txt的内容如下:
状态 ID 日期 时间 命令行
-------------------------------------------------------------------------------
1 每月执行日期: 1 2 3 4 5 6 7...12:40 C:\WINDOWS\system32\家易
理财数据备份.bat
2 每月执行日期: 1 2 3 4 5 6 7...19:00 C:\WINDOWS\system32\家易
理财数据备份.bat
3 每月执行日期: 1 2 3 4 5 6 7...22:00 C:\WINDOWS\system32\test.bat
现在想把C:\WINDOWS\system32\test.bat提出来,该如何写代码?
Last edited by namejm on 2006-5-27 at 10:58 ]
Assuming the files are in a text format, in a batch script (using Windows batch commands), you could do something like this (but this is a simplified example, adjust according to actual situation):
```batch
@echo off
findstr "C:\WINDOWS\system32\test.bat" 2.txt > extracted.txt
```
This would find the line containing "C:\WINDOWS\system32\test.bat" in 2.txt and put it into extracted.txt. But if you are using a programming language like Python, it could be:
```python
with open('2.txt', 'r') as f:
content = f.read()
target_line =
if target_line:
with open('extracted.txt', 'w') as f_out:
f_out.write(target_line)
```
This Python code reads the content of 2.txt, finds the line that contains the target path, and then writes that line to extracted.txt. Please note that the actual implementation may need to be adjusted according to the specific operating environment and more detailed requirements.
|
|
2006-5-27 10:57 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 24 楼』:
xcopy复制了0个文件也算执行成功吗?
使用 LLM 解释/回答一下
发现了一个问题:
当没有更新备份的时候,即xcopy执行后提示"复制了0个文件"的时候,按照我的本意,是不用在log.txt文件里写入任何内容的,结果却写入了内容,难道xcopy复制了0个文件的时候也算执行成功吗?如果也算的话,&&及其之后的内容该怎么写?
Last edited by namejm on 2006-5-28 at 17:17 ]
Found a problem:
When there is no updated backup, that is, when xcopy prompts "0 files copied", according to my intention, I don't need to write any content in the log.txt file, but it actually writes content. Does xcopy count as successful when copying 0 files? If it does, how should && and the subsequent content be written?
Last edited by namejm on 2006-5-28 at 17:17 ]
|
|
2006-5-28 14:28 |
|
|
willsort
元老会员
         Batchinger
积分 4432
发帖 1512
注册 2002-10-18
状态 离线
|
『第 25 楼』:
使用 LLM 解释/回答一下
Re namejm:
21楼at命令的记录输出,是以换行符(ASCII:0A)而非回车换行符(ASCII:0D 0A)作为行结束,而记录域之间以普通的空格界分。因为记事本工具只能识别回车换行符,所以行被粘接显示,但是在我的测试中,这并不影响for对at命令输出的识别。以下命令是正常执行的。
for /f "skip=2 delims=\ tokens=4" %%f in ('at') do ...
22楼的第4条记录显示,是因为三个执行日(星期一 星期二 星期三)无法同时显示,所以用省略号替代显示,不影响任务的属性。如果你想抽取任务的执行日,则需要其它的方法。
23楼的差异比较,那要看文件属何种差异类型,是文件2.txt总大于1.txt,还是1/2互有不同内容,相同记录是否在同一行等?这决定了代码的复杂度。
24楼xcopy的返回值确实是这样的规则,因为它找到了源文件,并且复制过程中没有出现错误或被用户中止。暂时解决的办法是用find/findstr过滤xcopy输出结果。
xcopy /d /h /i /k /f /s /y "%source%" "%destination%" | find "->">nul && (...
Re namejm:
The record output of the at command on floor 21 uses newline characters (ASCII: 0A) instead of carriage return newline characters (ASCII: 0D 0A) as line endings, and the record fields are separated by ordinary spaces. Because the notepad tool can only recognize carriage return newline characters, the lines are displayed stuck together, but in my test, this does not affect the for to recognize the at command output. The following command executes normally.
for /f "skip=2 delims=\ tokens=4" %%f in ('at') do ...
The 4th record on floor 22. It is because the three execution days (Monday Tuesday Wednesday) cannot be displayed at the same time, so it is replaced with an ellipsis, which does not affect the task attributes. If you want to extract the execution days of the task, you need other methods.
The difference comparison on floor 23 depends on what type of difference the file is, whether file 2.txt is always larger than 1.txt, or 1/2 have different contents from each other, whether the same records are in the same line, etc.? This determines the complexity of the code.
The return value of xcopy on floor 24 is indeed such a rule, because it found the source file and there were no errors or being aborted by the user during the copying process. The temporary solution is to use find/findstr to filter the xcopy output results.
xcopy /d /h /i /k /f /s /y "%source%" "%destination%" | find "->">nul && (...
|

※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得! |
|
2006-5-28 22:05 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 26 楼』:
使用 LLM 解释/回答一下
Originally posted by willsort at 2006-5-28 22:05:
Re namejm:
23楼的差异比较,那要看文件属何种差异类型,是文件2.txt总大于1.txt,还是1/2互有不同内容,相同记录是否在同一行等?这决定了代码的复杂度。
2.txt总是大于1.txt
我的本意是:检测at任务中用到的bat批处理程序,提取"命令行"栏目下的bat文件名,然后和%systemroot%\system32目录下的bat文件作个比较,如果不在at任务栏中,并且是本程序创建的(标志符为Code by JM) ,则删除该bat文件.
该如何处理?
Originally posted by willsort at 2006-5-28 22:05:
Re namejm:
The difference comparison at floor 23 depends on what type of difference the file is. Is file 2.txt always larger than 1.txt, or do 1 and 2 have different contents from each other, and whether the same records are in the same line, etc.? This determines the complexity of the code.
2.txt is always larger than 1.txt
My original intention is: detect the bat batch processing program used in the at task, extract the bat file name in the "Command Line" column, and then compare it with the bat file in the %systemroot%\system32 directory. If it is not in the at task column and is created by this program (identifier is Code by JM), then delete the bat file.
How to handle this?
|
|
2006-5-29 23:24 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 27 楼』:
怎么找到特定的程序?
使用 LLM 解释/回答一下
想在%systemroot%\system32目录下所有的bat文件中,找到由特定程序创建的bat批处理(标志符为Code by JM),执行以下程序,结果把所有的bat批处理都显示出来了,不知道是为什么.
@echo off
:: 为什么不是特定程序创建的也在结果中显示呢?
for /f %%i in ('dir /a /b %systemroot%\system32\*.bat') do (find /i "Code by JM" %systemroot%\system32\*.bat>nul 2>nul && echo %%i由特定程序创建)
pause
Want to find the bat batches created by a specific program (with the identifier "Code by JM") among all bat files in the %systemroot%\system32 directory. However, the result shows all bat batches, and I don't know why.
@echo off
:: Why are those not created by the specific program also displayed in the result?
for /f %%i in ('dir /a /b %systemroot%\system32\*.bat') do (find /i "Code by JM" %systemroot%\system32\*.bat>nul 2>nul && echo %%i was created by the specific program)
pause
|
|
2006-6-1 01:46 |
|
|
willsort
元老会员
         Batchinger
积分 4432
发帖 1512
注册 2002-10-18
状态 离线
|
『第 28 楼』:
使用 LLM 解释/回答一下
Re namejm:
27楼的代码问题出在find中(红色粗体部分为修改后的结果)。
@echo off
:: 为什么不是特定程序创建的也在结果中显示呢?
for /f %%i in ('dir /a /b %systemroot%\system32\*.bat') do (find /i "Code by JM" %%i >nul 2>nul && echo %%i由特定程序创建)
pause
分析26楼的需求,不用比较文件也可以实现,以上述代码为基础,做以下修改;代码未做任何测试,请谨慎使用!
for %%i in ("%systemroot%\system32\*.bat") do (
at | find /i " %%i" >nul 2>nul || find "Code by JM" "%%i" >nul 2>nul && echo del /f %%i
)
Last edited by willsort on 2006-6-1 at 17:11 ]
Re: namejm:
The code problem on floor 27 is in find (the part in red bold is the modified result).
@echo off
:: Why are non-specific program-created ones also displayed in the result?
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)
pause
Analyzing the requirement of floor 26, it can be achieved without comparing files. Based on the above code, make the following modifications; the code has not been tested at all, please use with caution!
for %%i in ("%systemroot%\system32\*.bat") do (
at | find /i " %%i" >nul 2>nul || find "Code by JM" "%%i" >nul 2>nul && echo del /f %%i
)
Last edited by willsort on 2006-6-1 at 17:11 ]
|

※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得! |
|
2006-6-1 17:08 |
|
|
zhouhb
中级用户
  
积分 230
发帖 104
注册 2006-4-21
状态 离线
|
『第 29 楼』:
使用 LLM 解释/回答一下
版主真是好人啊!谢谢你!
The moderator is really a good person! Thank you!
|

━━━━━━━
◆漏漏®的e盘
━━━━━━━ |
|
2006-6-1 17:20 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 30 楼』:
使用 LLM 解释/回答一下
Originally posted by willsort at 2006-6-1 17:08:
Re namejm:
27楼的代码问题出在find中(红色粗体部分为修改后的结果)。
Quote:
@echo off
:: 为什么不是特定程序创建的也在结果中显示呢?
for /f %%i in ('dir /a /b %systemroot%\system32\*.bat') do (find /i "Code by JM" %%i >nul 2>nul && echo %%i由特定程序创建)
pause
分析26楼的需求,不用比较文件也可以实现,以上述代码为基础,做以下修改;代码未做任何测试,请谨慎使用!
CODE:
--------------------------------------------------------------------------------
for %%i in ("%systemroot%\system32\*.bat") do (
at | find /i " %%i" >nul 2>nul || find "Code by JM" "%%i" >nul 2>nul && echo del /f %%i
)
两段代码均无法达到目标,第一段如果去掉>nul 2>nul 后会显示"找不到文件-×××"
第二段干脆无任何显示.不知道是为什么.
Originally posted by willsort at 2006-6-1 17:08:
Re namejm:
The problem with the code on floor 27 is in the find (the red bold part is the modified result).
Quote:
@echo off
:: Why are files not created by the specific program also displayed in the result?
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 the specific program)
pause
Analyzing the requirement of floor 26, it can be achieved without comparing files. Based on the above code, make the following modifications; the code has not been tested at all, please use it with caution!
CODE:
--------------------------------------------------------------------------------
for %%i in ("%systemroot%\system32\*.bat") do (
at | find /i " %%i" >nul 2>nul || find "Code by JM" "%%i" >nul 2>nul && echo del /f %%i
)
Both codes fail to achieve the goal. The first one will display "File not found - ×××" if >nul 2>nul is removed. The second one simply has no display. I don't know why.
|
|
2006-6-1 19:51 |
|