|
vptdosme
初级用户
积分 97
发帖 32
注册 2005-12-3
状态 离线
|
『楼 主』:
[原创]在命令行下实现简单的运算
鉴于一些批处理的需要,我们有时也得使用实现简单的运算,比如,批量修改文件,批量新建文件,文件分割等。由于自己有这方面的需要,加之搜索本论坛后,没有发现有明确的用批处理实现简单的运算的实例(使用外部命令除外,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 ]
|
|
2006-1-4 09:39 |
|
|
willsort
元老会员
Batchinger
积分 4432
发帖 1512
注册 2002-10-18
状态 离线
|
|
2006-1-7 10:19 |
|
|
vptdosme
初级用户
积分 97
发帖 32
注册 2005-12-3
状态 离线
|
『第
3 楼』:
感谢提醒,用循环是不得以而为之,我只是想抛砖引玉。。。
看了各位高手的贴,收获不小,为此我将自己写的一个垃圾批处理与大家共享:
(还望高手指教)
@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 [Version 1.00] All Rights Reserved
if exist found$$.dat del found$$.dat>nul
echo Copyright (R) By HuangShiquan 2005-2010 [Version 1.00] 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 [Version 1.00] 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 [Version 1.00] 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 [Version 1.00] All Rights Reserved
echo.
echo Usage: GetTypeFile (-command) (Para1) (Para2) (Para3) [option1] [option2]
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 ]
附件
1: GetTypeFile.rar (2006-1-7 23:41, 1.3 K, 下载附件所需积分 1 点
,下载次数: 7)
|
|
2006-1-7 23:14 |
|
|
willsort
元老会员
Batchinger
积分 4432
发帖 1512
注册 2002-10-18
状态 离线
|
『第
4 楼』:
Re vptdosme:
这似乎是一个文件管理程序,完成移动、复制、删除、列表批量文件的功能。为何程序名会是 GetTypeFile ?
另外,觉得程序的命令行处理模块有值得斟酌之地,下面是我编辑过的命令行处理主模块,与兄探讨。
下面的代码上下,分别使用了半角的[code]和[/code]标记,因此不必担心其中的某些含冒号的标签被转换为表情图像。点击帖子下面的“回复”按钮,在其中选择“代码”按钮,有相类似的作用。
@echo off
cls
if "%1"=="/?" goto info
if "%3"=="" goto over
:: 对命令行而言,%1 %2 %3 有顺序性的依存关系,如果 %3 存在,则 %1 %2 必然同时存在
if "%1"=="-d" goto nottestafter
if "%1"=="-l" goto nottestafter
:: 对 -d -l 而言,target 是不必要的,但因此会使 /y /n 的位置提前至 %3 ,这会对以后的代码产生影响
if "%4"=="" goto over
:: 关于选项 %4 %5 的分析处理建议移动命令执行模块中。 [ Last edited by willsort on 2006-1-8 at 10:33 ]
|
※ Batchinger 致 Bat Fans:请访问 [讨论]批处理编程的异类 ,欢迎交流与共享批处理编程心得! |
|
2006-1-8 10:26 |
|
|
vptdosme
初级用户
积分 97
发帖 32
注册 2005-12-3
状态 离线
|
『第
5 楼』:
Quote: | 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 ]
|
|
2006-1-8 14:16 |
|
|
vptdosme
初级用户
积分 97
发帖 32
注册 2005-12-3
状态 离线
|
『第
6 楼』:
按willsort的建议我对此批处理修改了下并再次优化了一下,并扩充了合并同名文件的功能(因为最近有一堆C++的源代码,每个压缩包内都有一个readme.txt的说明文件,为了归类要一个个的打开readme.txt很烦,于是便扩充了合并同名文件的功能)
@echo off
cls
if "%1"=="/?" goto info
if "%1"=="-d" goto nottestafter
if "%1"=="-l" goto nottestafter
if "%1"=="-b" goto nottestafter
if "%3"=="" goto over
rem this equ "%1","%2","%3"=="" goto over
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 [Version 1.00] All Rights Reserved
if exist found$$.dat del found$$.dat>nul
echo Copyright (R) By HuangShiquan 2005-2010 [Version 1.00] All Rights Reserved>found$$.dat
echo Have found files for you as follows:
for /r %2 %%a in (%3) do echo %%~a &echo %%~a>>found$$.dat
if "%1"=="-m" goto domove
if "%1"=="-c" goto docopy
if "%1"=="-d" goto dodelete
if "%1"=="-b" goto dobinfile
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
:dobinfile
if "%4"=="/y" goto dobinfilequiet
if "%5"=="/y" goto dobinfilequiet
echo Are you sure to Merge these file to %3 ?
echo You can Key (Ctrl+C) to Cancel the order, But if you put any key will Continue
pause>nul
:dobinfilequiet
if not exist %3 echo.>%3
for /f "skip=1 delims=" %%a in (found$$.dat) do echo Merge "%%~a" &© /y %3+%%a %3>nul
echo Have Merged these files Succeed to the %3
if "%4"=="/d" goto deldatforlist
if "%5"=="/d" goto deldatforlist
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
for /f "skip=1 delims=" %%a in (found$$.dat) do move /y "%%~a" %4 &echo Moving "%%~a"
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 [Version 1.00] 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 [Version 1.00] 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 [Version 1.00] All Rights Reserved
echo.
echo Usage: GetTypeFile (-command) (Para1) (Para2) (Para3) [option1] [option2]
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 -b to Merged 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 the letter of -b 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 4. "GetTypeFile -b j:\source readme.txt /y /d" , it will Merge all
echo readme.txt documents in the catalogue and its subdivision at present
echo to big readme.txt documents.
echo.
:over 附:用[code]贴代码,效果确实不错啊!!!
[ Last edited by vptdosme on 2006-1-8 at 19:31 ]
|
|
2006-1-8 19:27 |
|
|