For 命令的其他形式
如果启用了命令扩展,将支持如下 for 命令的其他格式:
只限于目录
for /D [%% | %]variable in (set) docommand [command-parameters]
如果 set 包含通配符(* 和 ?),则指定与目录名匹配,而不是文件名。
递归
for /R [[drive :]path] [%% | %]variable in (set) docommand [command-parameters]
进入根目录树[drive:]path,在树的每个目录中执行 for 语句。如果在 /R 后没有指定目录,则假定为当前目录。如果 set 只是一个句号 (.) 字符,则只列举目录树。
迭代
for /L [%% | %]variable in (start,step,end) do command [command-parameters]
集合是一系列按步长量划分的、从头到尾的数字。这样,(1,1,5) 将生成序列 1 2 3 4 5,而 (5,-1,1) 将生成序列 (5 4 3 2 1)。
BAT文件技巧
文章结构
1. 所有内置命令的帮助信息
2. 环境变量的概念
3. 内置的特殊符号(实际使用中间注意避开)
4. 简单批处理文件概念
5. 附件1 tmp.txt
6. 附件2 sample.bat
######################################################################
1. 所有内置命令的帮助信息
######################################################################
ver
cmd /?
set /?
rem /?
if /?
echo /?
goto /?
for /?
shift /?
call /?
其他需要的常用命令
type /?
find /?
findstr /?
copy /?
______________________________________________________________________
下面将所有上面的帮助输出到一个文件
echo ver >tmp.txt
ver >>tmp.txt
echo cmd /? >>tmp.txt
cmd /? >>tmp.txt
echo rem /? >>tmp.txt
rem /? >>tmp.txt
echo if /? >>tmp.txt
if /? >>tmp.txt
echo goto /? >>tmp.txt
goto /? >>tmp.txt
echo for /? >>tmp.txt
for /? >>tmp.txt
echo shift /? >>tmp.txt
shift /? >>tmp.txt
echo call /? >>tmp.txt
call /? >>tmp.txt
echo type /? >>tmp.txt
type /? >>tmp.txt
echo find /? >>tmp.txt
find /? >>tmp.txt
echo findstr /? >>tmp.txt
findstr /? >>tmp.txt
echo copy /? >>tmp.txt
copy /? >>tmp.txt
type tmp.txt
______________________________________________________
######################################################################
2. 环境变量的概念
######################################################################
_____________________________________________________________________________
C:Program Files>set
ALLUSERSPROFILE=C:Documents and SettingsAll Users
CommonProgramFiles=C:Program FilesCommon Files
COMPUTERNAME=FIRST
ComSpec=C:WINNTsystem32cmd.exe
NUMBER_OF_PROCESSORS=1
OS=Windows_NT
Os2LibPath=C:WINNTsystem32os2dll;
Path=C:WINNTsystem32;C:WINNT;C:WINNTsystem32WBEM
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH
PROCESSOR_ARCHITECTURE=x86
PROCESSOR_IDENTIFIER=x86 Family 6 Model 6 Stepping 5, GenuineIntel
PROCESSOR_LEVEL=6
PROCESSOR_REVISION=0605
ProgramFiles=C:Program Files
PROMPT=$P$G
SystemDrive=C:
SystemRoot=C:WINNT
TEMP=C:WINNTTEMP
TMP=C:WINNTTEMP
USERPROFILE=C:Documents and SettingsDefault User
windir=C:WINNT
_____________________________________________________________________________
path: 表示可执行程序的搜索路径. 我的建议是你把你的程序copy 到
%windir%system32. 这个目录里面. 一般就可以自动搜索到.
语法: copy mychenxu.exe %windir%system32.
使用点(.) 便于一目了然
对环境变量的引用使用(英文模式,半角)双引号
%windir% 变量
%%windir%% 二次变量引用.
我们常用的还有
%temp% 临时文件目录
%windir% 系统目录
%errorlevel% 退出代码
输出文件到临时文件目录里面.这样便于当前目录整洁.
对有空格的参数. 你应该学会使用双引号("") 来表示比如对porgram file文件夹操作
C:>dir p*
C: 的目录
2000-09-02 11:47 2,164 PDOS.DEF
1999-01-03 00:47
Program Files
1 个文件 2,164 字节
1 个目录 1,505,997,824 可用字节
C:>cd pro*
C:Program Files>
C:>
C:>cd "Program Files"
C:Program Files>
######################################################################
3. 内置的特殊符号(实际使用中间注意避开)
######################################################################
微软里面内置了下列字符不能够在创建的文件名中间使用
con nul aux / | || && ^ > < *
You can use most characters as variable values, including white space. If you use the special characters <, >, |, &, or ^, you must precede them with the escape character (^) or quotation marks. If you use quotation marks, they are included as part of the value because everything following the equal sign is taken as the value. Consider the following examples:
转载于:https://www.cnblogs.com/xiaoxingchi/archive/2010/01/18/1651042.html