20309 发表于 2015-12-25 15:18:32

Command Prompt + Perl 脚本编写笔记

输出到文件字符  >覆盖输出到文件
  >>追加输出到指定文件
  
  连接两个执行
  & 执行命令1然后执行命令2
  &&如果执行命令1成功,则执行命令2
  ||如果执行命令1失败,则执行命令2
  
  CHOICE选择语句
  如以下:
  CHOICE /C:123
  在数字1、2、3中选择操作。
  
  CALL语句
  调用外部批处理程序
  CALL C:"WINDOWS"NEW_BATCHFILE.BAT
  
调用外部程序
  CALL C:"calc.exe
  
  FOR...IN...DO循环语句

这段代码将在(A, B, C)中当获得B时将输出"B is in the set!"
  FOR %%b in (A, B, C) DO IF %%b == B echo B is in the set!
  
将输出C:"windows"desktop下所有文件列表
  FOR %%c in (C:"windows"desktop"*.*) DO echo %%c
  


  GOTO语句
  goto EOF或者goto :EOF都可以执行。
  
  
  IF, IF EXIST, IF NOT EXIST
  IF EXIST C:"tempfile.txt
DEL C:"tempfile.txt
  
  IF NOT EXIST C:"tempfile.txt
COPY C:"WINDOWS"tempfile.txt C:"tempfile.txt
  
  换行操作符
代码太长,用^可以换行,例如:
  copy file.txt file2.txt
  可以更改为:
  copy file.txt^
file2.txt
  
  批处理文件名后跟参数问题
  1. 用%加数字表示,如%1,%2,%3等,但以下只表示命令后的参数,如run -c -d中的-c和-d等参数。
  if (%1) == (-?) goto MENU
  if (%1) == (-1) goto 1
  if (%1) == (-3) goto 2
  if (%1) == (-4) goto 3
  2. 用%0可取批处理文件本身文件名。比如run -c 输出%0,则显示run;若run.bat -c则输出run.bat等。
  
  创建用户自定义变量
  set a=1
  set b=2
  
  判断文件是否存在语句
  IF EXIST, IF NOT EXIST
  IF EXIST C:"tempfile.txt
DEL C:"tempfile.txt
  
  IF NOT EXIST C:"tempfile.txt
COPY C:"WINDOWS"tempfile.txt C:"tempfile.txt
  
  下面是我用来学习的例子(Command Prompt 与 Perl的结合):

@ECHO OFF
REM --------------------------------
REM      Created by Lihua
REM --------------------------------
BREAK=ON
if (%1) == (-?) goto MENU
if (%1) == (-1) goto 1
if (%1) == (-2) goto 2
if (%1) == (-perl) goto perl
if (%1) == (-4)    goto 4
:MENU
ECHO 1 - FORINDO
ECHO 2 - demo
ECHO 3 - Crosses
ECHO 0 - EXIT
goto EOF
:1
FOR %%c in (D:\*.*) DO echo %%c
goto EOF
:2
FOR %%b in (A, B, C) DO IF %%b == B echo B is in the set!
goto :EOF
:perl
echo %~dp0
echo %*
setlocal
perl.exe -w -x %~f0 %*
endlocal
goto EOF
:4
echo I^
am^
boss^
here^
!!!
goto EOF
:EOF
EXIT /b


#!perl
print("\n");
__END__
页: [1]
查看完整版本: Command Prompt + Perl 脚本编写笔记