1.@ 与%i相关的变量(bat参数或者for循环的%i)
假设文件为C:\Documents and Settings\jinsun\桌面\ParseSinglePkgs.bat
%0 C:\Documents and Settings\jinsun\桌面\ParseSinglePkgs.bat
%~dp0 C:\Documents and Settings\jinsun\桌面\
%cd% C:\Documents and Settings\jinsun\桌面
%~nx0 ParseSinglePkgs.bat
%~n0 ParseSinglePkgs
%~x0 .bat
2.@ 与%VAR%相关的变量(取子字符串)
%VAR:str1=str2% 会将VAR中的str1替换为str2(str2如果为空则可以达到删除的效果,str1前可以加*,变量%ABC:*B=%是C)
%VAR:~0,-2% 会提取VAR 变量的所有字符,除了最后两个
%VAR:~-2% 会提取VAR 变量的最后两个
%VAR:~2,5% 提取从第2个字符开始的5个字符
set p=aa1bb1aa2bb2 #设置变量p,并赋值为 = 后面的字符串,即aa1bb1aa2bb2
echo %p% #显示变量p代表的字符串,即aa1bb1aa2bb2
echo %p:~6% #显示变量p中第6个字符以后的所有字符,即aa2bb2
echo %p:~6,3% #显示第6个字符以后的3个字符,即aa2
echo %p:~0,3% #显示前3个字符,即aa1
echo %p:~-2% #显示最后面的2个字符,即b2
echo %p:~0,-2% #显示除了最后2个字符以外的其它字符,即aa1bb1aa2b
echo %p:aa=c% #用c替换变量p中所有的aa,即显示c1bb1c2bb2
echo %p:aa=% #将变量p中的所有aa字符串置换为空,即显示1bb12bb2
echo %p:*bb=c% #第一个bb及其之前的所有字符被替换为c,即显示c1aa2bb2
3.转移字符:
^
4.输入回车换行
echo.
for /f "delims= "
5.字符替换:
%var:str1=str2" 变量str1被str2替换
下面为输入一个文件,替换其中的字符
@echo off
rem ***************************************************************************
rem ** replaceChar.bat - command line arguments List **
rem ** **
rem ** %1 - ext_name **
rem ** %2 - characters replaced **
rem ** %3 - new characters **
rem ** **
rem ***************************************************************************
set ext_name=%1
set old_char=%2
set new_char=%3
if "%ext_name%" == "" goto warning
if "%old_char%" == "" goto warning
if "%new_char%" == "" goto warning
setlocal enabledelayedexpansion
set /a a=1
for %%x in (*.%ext_name%) do (
for /f "tokens=*" %%i in (%%x) do (
set "var=%%i"
if not !var!.==. (
set "var=!var:%old_char%=%new_char%!"
echo !var!!>>!a!.xml
)
)
set /a a=!a!+1
)
goto end
:warning
echo the tool replaceChar requires 3 command line arguments:
echo 1) ext name of file in which you want to replace some characters
echo 2) characters you want to be replaced
echo 3) new characters with which you want to replace some characeters
:end
echo ***** end to replace characters *****
echo -
6.获取操作系统
for /f "tokens=1-5,*" %%a in ('ver') do (
for /f "tokens=1-5,* delims=]" %%i in ("%%e") do set Os1=%%i
)
set Os=%Os1:~0,3%
if Os=="5.0" (
set syspath=c:\\winnt\\system32\\
) else (
set syspath=c:\\windows\\system32\\
)
7.用户目录:%userprofile%
8.计算机名 %computername% |
|