中国DOS联盟论坛

中国DOS联盟

-- 联合DOS 推动DOS 发展DOS --
联盟域名:www.cn-dos.net 论坛域名:www.cn-dos.net/forum
游客 | 登录 | 注册 | 会员 | 搜索 | 中国DOS联盟
中国DOS联盟论坛
现在时间是 2026-10-31 18:58
共 47,815 主题排行 / 349,930 发帖 / 今日 0 篇 / 48,290 会员排行
DOS开发编程 & 发展交流 (开发室) » [原创]在命令行下实现简单的运算
可打印版本  1,386 / 5
第1楼 vptdosme 发表于 2006-01-04 09:39
初级用户 发帖 32 积分 97
[原创]在命令行下实现简单的运算
鉴于一些批处理的需要,我们有时也得使用实现简单的运算,比如,批量修改文件,批量新建文件,文件分割等。由于自己有这方面的需要,加之搜索本论坛后,没有发现有明确的用批处理实现简单的运算的实例(使用外部命令除外,eg:degug),在此提供一个实例与大家共享。(技术不精,还望指教)

@echo off
SET num=0
echo first set num=0
:trytest
echo num=%num%
if not %num%==6 goto cuntnum
goto testover
:cuntnum
SET/a num=1+%num% &echo Now add one, the result is as follows:
goto trytest
:testover
rem code over

[ Last edited by vptdosme on 2006-1-4 at 09:45 ]
第2楼 willsort 发表于 2006-01-07 10:19
元老会员 发帖 1,512 积分 4,432
Re vptdosme:

大略看了看,似乎你的批处理更像循环,而不是命令行运算。

关于命令行的表达式运算可以参看,而关于循环处理,我也在本站解答室也贴出了一些,只是不太好找了。有时间的话,我将整理到。

{16534}2K/XP表达式计算批处理
http://www.cn-dos.net/forum/viewthread.php?tid=16534

{8905]批处理编程的异类
http://www.cn-dos.net/forum/viewthread.php?tid=8905
第3楼 vptdosme 发表于 2006-01-07 23:14
初级用户 发帖 32 积分 97
感谢提醒,用循环是不得以而为之,我只是想抛砖引玉。。。
看了各位高手的贴,收获不小,为此我将自己写的一个垃圾批处理与大家共享:
(还望高手指教)
@echo off
cls
if "%1"=="/?" goto info
if "%1"=="" goto over
if "%2"=="" goto over
if "%3"=="" goto over
if "%1"=="-d" goto nottestafter
if "%1"=="-l" goto nottestafter
if "%4"=="" goto over
if "%5"=="/y" goto normal
if "%5"=="" goto normal
if not "%5"=="/n" goto useerr
goto normal
:normal
if not exist %4\nul md %4
:nottestafter
echo Copyright (R) By HuangShiquan 2005-2010 All Rights Reserved
if exist found$$.dat del found$$.dat>nul
echo Copyright (R) By HuangShiquan 2005-2010 All Rights Reserved>found$$.dat
echo Please waite a while, I am working hard for you ...
for /r %2 %%a in (%3) do echo %%~a>>found$$.dat
echo Have found files for you as follows:
for /f "skip=1 tokens=1 delims=" %%a in (found$$.dat) do echo %%a
if "%1"=="-m" goto domove
if "%1"=="-c" goto docopy
if "%1"=="-d" goto dodelete
if not "%1"=="-l" goto comerr
if "%4"=="" goto over
if "%4"=="/d" goto deldatforlist
if "%5"=="" goto over
if "%5"=="/d" del found$$.dat>nul
goto over
:deldatforlist
del found$$.dat>nul
goto over
:domove
if "%5"=="/y" goto normalquietmove
echo Are you sure to move these file ?
echo You can Key (Ctrl+C) to Cancel the order, But if you put any key will Continue
pause>nul
:normalquietmove
echo Please waite a while, I am working hard for you ...
for /f "skip=1 delims=" %%a in (found$$.dat) do move /y "%%~a" %4
echo Have moved these files Succeed to the directory
goto aboutdat
:dodelete
if "%4"=="/y" goto normalquietdelete
if "%4"=="/d" goto normaldelete
if not "%4"=="/n" goto deleteerr
:normaldelete
echo Are you sure to delete these file ?
echo You can Key (Ctrl+C) to Cancel the order, But if you put any key will Continue
pause>nul
:normalquietdelete
echo Please waite a while, I am working hard for you ...
for /f "skip=1 delims=" %%a in (found$$.dat) do del /f /s /q "%%~a">nul
echo Have deleted these files Succeed to the directory
if "%4"=="" goto over
if "%4"=="/d" goto deldatfordelete
if "%5"=="" goto over
if "%5"=="/d" del found$$.dat>nul
goto over
:deldatfordelete
del found$$.dat>nul
goto over
:docopy
if "%5"=="/y" goto normalquietcopy
echo Are you sure to copy these file ?
echo You can Key (Ctrl+C) to Cancel the order, But if you put any key will Continue
pause>nul
:normalquietcopy
echo Please waite a while, I am working hard for you ...
for /f "skip=1 delims=" %%a in (found$$.dat) do copy /y "%%~a" %4>nul
echo Have copyed these files Succeed to the directory
:aboutdat
if "%6"=="/d" del found$$.dat>nul
goto over
:deleteerr
echo.
echo Error: Can't delete these files for Syntax is illegal or bad used command
goto over
:comerr
echo.
echo Copyright (R) By HuangShiquan 2005-2010 All Rights Reserved
:deleteerr
echo.
echo Error: Syntax is illegal or not use a command Parameter
goto over
:useerr
echo.
echo Copyright (R) By HuangShiquan 2005-2010 All Rights Reserved
echo.
echo Error: option2 must use with option1 or Syntax is illegal
goto over
:info
echo.
echo Copyright (R) By HuangShiquan 2005-2010 All Rights Reserved
echo.
echo Usage: GetTypeFile (-command) (Para1) (Para2) (Para3)
echo.
echo Commands: -m to move these files; -c to copy these files
echo -d to delete these files; -l only list these files
echo Explain : Parameter1 the path for search;
echo Parameter2 the file name, you may then use wildcard (eg:*.*,
echo *.doc,filename.txt and so on);
echo Parameter3 the path for deposit;
echo Option : option1 /y do it quiet; /n inquire always
echo option2 /d not save moving data recorded file
echo Notice : option2 must use with option1,but Excepted when the command is
echo the letter of -d or the letter of -l.
echo Example: 1. "GetTypeFile -m j:\source *.txt e:\targe /y", it will move the
echo files which taken.txt as a suffix in drive(about j to (j:\txt).
echo 2. "GetTypeFile -m j:\source *.txt e:\targe /y /d", it will move
echo the fileswhich taken .txt as a suffix in drive (about j to the
echo directory (about j:\txt),and not save moving data recorded file.
echo 3. "GetTypeFile -l j:\source *.txt /y /d" or "GetTypeFile -l j:\
echo source *.txt /d", it will only list the,files which taken .txt as
echo suffix in drive (about j,and not save moving data recorded file.
echo.
ver

说明:不知为何,有些批处理字符变成了图像,如:“ver”

[ Last edited by vptdosme on 2006-1-7 at 23:38 ]

附件
GetTypeFile.rar (1.3 KiB)
第4楼 willsort 发表于 2006-01-08 10:26
元老会员 发帖 1,512 积分 4,432
Re vptdosme:

这似乎是一个文件管理程序,完成移动、复制、删除、列表批量文件的功能。为何程序名会是 GetTypeFile ?

另外,觉得程序的命令行处理模块有值得斟酌之地,下面是我编辑过的命令行处理主模块,与兄探讨。

下面的代码上下,分别使用了半角的[code]和[/code]标记,因此不必担心其中的某些含冒号的标签被转换为表情图像。点击帖子下面的“回复”按钮,在其中选择“代码”按钮,有相类似的作用。



[ Last edited by willsort on 2006-1-8 at 10:33 ]
第5楼 vptdosme 发表于 2006-01-08 14:16
初级用户 发帖 32 积分 97
Originally posted by willsort at 2006-1-8 10:26:
Re vptdosme:

这似乎是一个文件管理程序,完成移动、复制、删除、列表批量文件的功能。为何程序名会是 GetTypeFile ?

另外,觉得程序的命䠮..



首先,至于为何程序名会是 GetTypeFile,那因为当初写这个批处理只是为了对谋一类文件进行批量移动操作。后来闲着无事,干脆对之进行修理扩充,添加了拷贝,列表,删除,于是程序名会显有些不当(也懒得改它^_^)
另外,你建议开始只检测"%3",确实如此,写成这样是因为我对批处理变量的依存关系不太了解.至于后面的一些结构,只是为了兼顾性和优化执行速度,即使在使用删除、列表功能时输入命令格式有误(或类似与移动、复制格式时),也能做一些错误处理罢了.如果不进行一些错误处理,确实可以省掉很多不必要的检测,但那样的话,可能对于不知内情的使用者,当使用删除或移动的功能会产生意外的错误或危险的后果.

[ Last edited by vptdosme on 2006-1-8 at 14:20 ]
第6楼 vptdosme 发表于 2006-01-08 19:27
初级用户 发帖 32 积分 97
按willsort的建议我对此批处理修改了下并再次优化了一下,并扩充了合并同名文件的功能(因为最近有一堆C++的源代码,每个压缩包内都有一个readme.txt的说明文件,为了归类要一个个的打开readme.txt很烦,于是便扩充了合并同名文件的功能)


附:用
[ 联系联盟系统管理团队 - 中国DOS联盟 - 标准版 ]
Sponsored by ifanr Inc | © 2001–2023