昨天写了点dos script,总结一下

以下是对于用到的和google/baidu时看到的dos script的总结,顺序不分前后,:)

1) 一些predefined的变量,不同windows版本还不同(红色的windows vista,7,8特有的)

  • %1…%n, 传递的参数
  • NUL, empty

Table I. Some more common predefined environment variables

Variable

Typical value (May vary)

%ALLUSERSPROFILE%

C:\Documents and Settings\All Users

Windows vista, 7, 8

C:\ProgramData

%APPDATA%

C:\Documents and Settings\{username}
\Application Data

Windows vista, 7, 8

C:\Users\(username}\AppData\Roaming

%COMPUTERNAME%

{computername}

%COMSPEC%

C:\Windows\System32\cmd.exe

%HOMEDRIVE%

C:

%HOMEPATH%

\Documents and Settings\{username}

%PATH%

C:\Windows\System32\;C:\Windows\;
C:\Windows\System32\Wbem

%PATHEXT%

.COM; .EXE; .BAT; .CMD; .VBS; .VBE; .JS ; .WSF; .WSH .MSC

%PROGRAMFILES%

Directory containing program files,
usually
C:\Program Files

%PROMPT%

Code for current command prompt format. Code is usually $P$G

%SYSTEMDRIVE%

The drive containing the Windows XP root directory, usually C:

%SYSTEMROOT%

The Windows XP root directory, usually C:\Windows

%TEMP% and %TMP%

C:\DOCUME~1\{username}\LOCALS~1\Temp

%USERNAME%

{username}

%USERPROFILE%

C:\Documents and Settings\{username}

C:\Users\{username}

%WINDIR%

C:\Windows

 

 

Table II. Some dynamic environment variables

Variable

Value

%DATE%

Current date in the format determined by the Date command

%TIME%

Current time in the format determined by the Time command

%CD%

Current directory with its full path

%ERRORLEVEL%

Number defining exit status of a previous command or program

%RANDOM%

Random number between 0 and 32767

 

2)获得系统的盘符

for /f "delims=/:" %%a in ("%SystemRoot%") do set sysDisk=%%a

 

C

 

 

3)获得脚本所在的目录-这个还是比较有用的~

 

3) get script position

for /f "delims=/[" %%a in ("%~dp0") do set curPath=%%a

 

4)判定win版本

ver | find "2003" > nul

if %ERRORLEVEL% == 0 goto ver_2003

 

ver | find "XP" > nul

if %ERRORLEVEL% == 0 goto ver_xp

 

ver | find "NT" > nul

if %ERRORLEVEL% == 0 goto ver_nt

 

if not exist %SystemRoot%\system32\systeminfo.exe goto warnthenexit

 

systeminfo | find "OS Name" > %TEMP%\osname.txt

FOR /F "usebackq delims=: tokens=2" %%i IN (%TEMP%\osname.txt) DO set vers=%%i

vers=%%i

 

echo %vers% | find "Windows 7" > nul

if %ERRORLEVEL% == 0 goto ver_7

 

echo %vers% | find "Windows Server 2008" > nul

if %ERRORLEVEL% == 0 goto ver_2008

 

echo %vers% | find "Windows Vista" > nul

if %ERRORLEVEL% == 0 goto ver_vista

 

5) windows也有管道接口吆

 

 Findstr /b “testString” “c:\test data\test.txt” | find /C “testString”

 

6)  用时间生成一个特殊的字符

for /f "tokens=2-8 delims=/:. " %%A in ("%date%:%time: =0%") do set "UNIQUE=%%C%%A%%B%%D%%E%%F%%G"

 

Fri25201215282148

 

7)  设置数值变量

Set /a val=102

 

8) if…else, goto

i) goto可以完成if...else的多层嵌套 

ii)  数值比较

EQU, NEQ, LSS, LEQ, GTR, GEQ

iii)  exist, not exist

iv) if exist c:\test.txt (goto line_1) else goto line_2

v) if … () else ()

vi) if defined var1 goto line_var_defined

vii)  和 errorlevel一起使用

Copy  c:\test.txt c:\test.txt.bak

If errorlevel == 0 goto line_successful

 

google到的还有很多,时间有限就先整这么多吧,以上主要是用到一点半点的~~

这个网址也不错

http://www.dostips.com/DtCodeBatchFiles.php#Batch.FileList

http://www.dostips.com/DtCodeSnippets.php#Snippets.CountLines

 

 

你可能感兴趣的:(windows,dos,script)