带开关的批处理文件

时间:2015-01-07 04:44:35

标签: batch-file

在最近的休息期间,我查看了我的mirror.bat。

这是多年来拼凑在一起的。

这不漂亮。它需要一个非常好的整理。

它目前适用于robocopy作业文件,例如:
> mirror.bat / Mirror“镜像硬盘到USB”

它的工作方式是允许我使用多个开关。

例如:

  1. 镜像我的硬盘,清理缓存,卸载所有驱动器和休眠,输入
    会很不错 > mirror.bat / Mirror“Mirror HDD to USB”/ Clean / Dismount / Hibernate

  2. 清理缓存和卸载
    > mirror.bat / Clean / Dismount

  3. 我选择的开关是:/ Clean / Dismount / Hibernate / Mirror / Help

    这些应改为:/ C / D / H / M / H /?

    一些小型公用事业有助于: •http://www.nirsoft.net/utils/nircmd.html
    http://uwe-sieber.de/drivetools_e.html

    因此,重要的组成部分是:
    •呼叫配置文件标准化:颜色,%temp%等。
    •定义开关
    •创建菜单
    •创建错误屏幕
    •使用robocopy作业文件
    •清除缓存,临时等。
    •卸下所有非系统驱动器
    •休眠

    感谢期待一场精彩的讨论。

    mirror.bat

    :: ----------------
    :: Start common setting - should be called from a common.bat
    :: ----------------
    @echo off && cls
    EndLocal && SetLocal EnableExtensions EnableDelayedExpansion
    pushd %~dp0
    
    set profile="All Stuff"
    set comment="Mirror to USB, clean cache, hibernate"
    set slash=Clean Dismount Hibernate Mirror Help
    set colour=color F0
    set errclr=color 4F
    set div=úúúúúúúúúúúúúúúú
    set rule=ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ
    
    set favicon="\favicon.ico"
    set drives=C D E F G H I J K L M N O P Q R S T U V W X Y Z
    set eject=G H I J K L M N O P Q R S T U V W X Y Z
    set dvd=D
    set symlink=MMC SDCard
    set exfldr="MSOCache" "$RECYCLE.BIN" "RECYCLED" "RECYCLER" "System Volume Information" "TEMP" "TMP" "%tmp%"
    set exext="*.~*" "*_list.txt" "*.log" "*.tmp" "*.xlk"
    
    set app.1="%SystemRoot%\System32\choice.exe"
    set app.2="\PortableApps\NirSoft\NirCmd.exe"
    set opt.1=-1 100
    set opt.2=-1 20
    set opt.3=30
    set app.3="\PortableApps\Uwe-Sieber\RemoveDrive.exe"
    set app.4="\PortableApps\Uwe-Sieber\EjectMedia.exe"
    set app.6="%SystemRoot%\System32\robocopy.exe"
    set ext.1=.RCJ
    set swt.1=/job:
    set app.7="%SystemRoot%\System32\shutdown.exe"
    
    %colour%
    set no=1
    :TEST
    set test=!app.%no%!
    if NOT exist !test! set app=!test! && goto :ERROR
    if exist !test! for %%a in (!test!) do (
      set app.!no!=%%~fa
      set /a no+=1
    ) else (
      set app.!no!=
      goto :NEXT
    )
    goto :TEST
    :NEXT
    for /f "tokens=1-4 delims=/ " %%i in ('time /t') do (set time=%%i %%j)
    for /f "tokens=1-4 delims=/ " %%i in ('date /t') do (set date=%%i %%j/%%k/%%l)
    set title=%profile:"=% %~nx0 Ä %date%  %time%
    set lock="%tmp%\%profile:"=%_%~n0.lock"
    if defined lock if exist %lock% goto :ERROR
    set endwait=%app.2% wait %opt.3%00
    set endbeep=%app.2% speak text "bong" %opt.1%
    set bing=%app.2% speak text "bing" %opt.1%
    set hipip=%app.2% speak text "bang" %opt.1%
    set lopip=%app.2% speak text "bung" %opt.1%
    :: ----------------
    :: End common setting - should be called from a common.bat
    :: ----------------
    set message="¯ Press C to continue, D to "delay" for %opt.3% seconds or B to break "%~nx0": "
    set choices=cdb
    set default=c
    set select=%app.1" /t %opt.3% /c:%choices% /d %default% /n /m %message%
    set sel.2=%opt.3% second delay
    set sel.1=continue
    set sel.0=break (terminate) the batch file
    :CONDITIONS
    if {%1}=={} set s.act=Help && goto :/HELP
    if {%1}=={/} set s.act=Help && goto :/HELP
    set speak=%app.2% %bing%
    set arg.1=%1
    if NOT {%arg.1%}=={} (
      set ref.1=%arg.1:"=%
    )
    if {%ref.1%}=={/?} set s.act=Help && goto :/HELP
    if {%ref.1:~,1%}=={/} set s.act=%ref.1%
    if NOT {%s.act%}=={} (
      set s.act=Help 
      set speak=%app.2% speak text "%s.act%" %opt.1%
      for %%a in (%slash%) do if "%s.act%"=="/%%a" (
        set s.act=%%a
        set speak=%app.2% speak text "%%a" %opt.1%
      )
      goto :/%s.act%
    )
    
    :/CLEAN
    set comment=deleting temporary files and folders
    title %s.act% Ä %title%
    SetLocal EnableDelayedExpansion
    cls && echo. && echo   %s.act% Ä %comment% && echo   %rule%
    set cleaner=
    for %%C in (
      "%app.2% script "~$folder.nircmd$\NirCmd.ncl""
    ) do set cleaner=!cleaner! %%C
    :CLEANGO
    echo. && echo   Cleaners for %UserName% on %ComputerName%
    set /a nr=0
    set clist=
    for %%C in (%cleaner%) do (
      set /a nr+=1
      set clist=!clist! %%C
      echo. && echo      !nr!. %%~C
    )
    echo. && echo   %rule%
    :CLEAN_loop
    %speak% && %select%
    if %errorlevel% EQU 3 @echo ÿÿItem %errorlevel% Ä %sel.0% && echo. && goto :BREAK
    if %errorlevel% EQU 2 @echo ÿÿItem %errorlevel% Ä %sel.2% && echo. && goto :CLEAN_loop
    if %errorlevel% EQU 1 @echo ÿÿItem %errorlevel% Ä %sel.1% && echo. && goto :CLEAN_end
    if %errorlevel% EQU 0 @echo ÿÿItem %errorlevel% Ä %sel.0% && echo. && goto :END
    :CLEAN_end
    cls
    echo. && echo   %s.act% Ä %comment% && echo   %rule%
    echo. && echo   Cleaning %UserName% on %ComputerName%
    set /a nr=0
    for %%C in (%clist%) do (
      set /a nr+=1
      %%~C
      echo. && echo      !nr!. %%~C
    )
    echo.
    for %%s in (
      s.act comment cleaner nr clist speak
    ) do set %%s=
    SetLocal DisableDelayedExpansion
    echo   %rule% && %endbeep% && %endwait% && goto :END
    
    :/DISMOUNT
    set comment=safely remove portable drives and devices
    title %s.act% Ä %title%
    SetLocal EnableDelayedExpansion
    cls && echo. && echo   %s.act% Ä %comment% && echo   %rule%
    :DISMOUNTGO
    echo. && echo   %s.act% Ä %comment% && echo   %rule%
    echo. && echo   Drives on \\%ComputerName%\%UserName%
    echo.
    set /a nr=0
    set dlist=
    for %%d in (%drives%) do (
      if exist %%d:\nul (
        echo @echo off > volume.bat
        echo set getvol_= >> volume.bat
        echo set getvol_=%%3:   %%5 %%6 %%7 %%8 %%9 >> volume.bat
        vol %%d: | find "Volume" > volume.cmd
        call volume.cmd
        if exist volume.* del volume.*
        set dlist=!dlist! %%d
        set /a nr+=1
        if !nr! LEQ  9 (
          echo      !nr!.  !getvol_!
        ) else (
          echo     !nr!.  !getvol_!
        )
      )
    )
    echo. && echo   %rule%
    :DISMOUNT_loop
    %speak% && %select%
    if %errorlevel% EQU 3 @echo ÿÿItem %errorlevel% Ä %sel.0% && echo. && goto :BREAK
    if %errorlevel% EQU 2 @echo ÿÿItem %errorlevel% Ä %sel.2% && echo. && goto :DISMOUNT_loop
    if %errorlevel% EQU 1 @echo ÿÿItem %errorlevel% Ä %sel.1% && echo. && goto :DISMOUNT_end
    if %errorlevel% EQU 0 @echo ÿÿItem %errorlevel% Ä %sel.0% && echo. && goto :END
    :DISMOUNT_end
    cls
    echo. && echo   %s.act% Ä %comment% && echo   %rule%
    echo. && echo   Dismounting removable devices for %UserName% on %ComputerName%
    echo.
    set /a nr=0
    if exist volume.* del volume.*
    for %%d in (%dlist%) do (
      if exist removing.* del removing.*
      if exist %%d:\nul (
        echo @echo off > removing.bat
        echo set get_vol= >> removing.bat
        echo set get_vol=%%2 %%3 %%4 %%5 %%6 %%7 %%8 %%9 >> removing.bat
        %app.3% %%d: > removing.txt
        if !errorlevel! EQU  0 (
          type removing.txt | find "Removing" > removing.cmd
          call removing.cmd
          if exist removing.* del removing.*
          set /a nr+=1
          if !nr! LEQ  9 (
            echo      !nr!.  !get_vol! && %hipip%
          ) else (
            echo     !nr!.  !get_vol! && %hipip%
          )
          if !errorlevel! EQU  4 echo           code !errorlevel! : temporary copy created, executed and deleted
          if !errorlevel! EQU  3 echo           code !errorlevel! : -- code not defined --
          if !errorlevel! EQU  2 echo           code !errorlevel! : INVALID device or parameter
          if !errorlevel! EQU  1 echo           code !errorlevel! : not found - NOT removed
          if !errorlevel! EQU  0 echo           code !errorlevel! : REMOVED successfully
          echo.
        )
      )
    )
    if exist removing.* del removing.*
    for %%e in (%dvd%) do (
      if exist ejecting.* del ejecting.*
      if exist %%e:\nul (
        echo @echo off > ejecting.bat
        echo set get_vol= >> ejecting.bat
        echo set get_vol=%%3 %%4 %%5 %%6 %%7 %%8 %%9 >> ejecting.bat
        %app.4% %%e: > ejecting.txt
        if !errorlevel! EQU  0 (
          type ejecting.txt | find "Ejecting" > ejecting.cmd
          call ejecting.cmd
          if exist ejecting.* del ejecting.*
          set /a nr+=1
          echo      !nr!.   !get_vol! && %hipip%
          if !errorlevel! EQU  4 echo           code !errorlevel! : temporary copy created, executed and deleted
          if !errorlevel! EQU  3 echo           code !errorlevel! : -- code not defined --
          if !errorlevel! EQU  2 echo           code !errorlevel! : INVALID device or parameter
          if !errorlevel! EQU  1 echo           code !errorlevel! : not found - NOT ejected
          if !errorlevel! EQU  0 echo           code !errorlevel! : EJECTED successfully
          echo.
        )
      )
    )
    if !nr! EQU 0 echo      !nr!  removable devices dismounted && echo. && %lopip%
    if exist ejecting.* del ejecting.*
    for %%s in (
      s.act comment title nr dlist drives get_vol dvd
    ) do set %%s=
    SetLocal DisableDelayedExpansion
    echo   %rule% && %endbeep% && %endwait% && goto :/CLEAN
    
    :/MIRROR
    set comment=file replication
    title %s.act% Ä %title%
    SetLocal EnableDelayedExpansion
    cls && echo. && echo   %s.act% Ä %comment% && echo   %rule%
    if NOT exist "%~2%ext.1%" goto :MIRRORERROR
    if NOT /==/%2 if NOT %2/==/?/ goto :MIRRORGO
    :MIRRORERROR
    %errclr%
    :NORCJ_loop
    if NOT /==/%2 if NOT %2/==/?/ if NOT exist "%~2%ext.1%" (
      echo ž   Source: "%~2%ext.1%"        MISSING && echo             %div% && %lopip%
    )
    shift /2
    if %2'==' goto :NORCJ_End
    goto :NORCJ_loop
    :NORCJ_end
    echo. && echo ¯   Syntax: "%~nx0%" /%s.act% "source"
    echo. && echo   Required: "source" Ä "%s.act% job file name" (without "%ext.1%")
    echo. && echo      Notes: labels with spaces must be "double quoted"
    echo. && echo   Executed: %~nx0 %*
    goto :MIRRORSTOP_loop
    :MIRRORSTOP_loop
    echo   %rule%
    %speak% && %select%
    if %errorlevel% EQU 3 @echo ÿÿItem %errorlevel% Ä %sel.0% && echo. && goto :BREAK
    if %errorlevel% EQU 2 @echo ÿÿItem %errorlevel% Ä %sel.2% && echo. && goto :MIRRORSTOP_loop
    if %errorlevel% EQU 1 @echo ÿÿItem %errorlevel% Ä %sel.1% && echo. && goto :END
    if %errorlevel% EQU 0 @echo ÿÿItem %errorlevel% Ä %sel.0% && echo. && goto :END
    :MIRRORSTOP_end
    :MIRRORGO
    echo. && echo   %s.act% job list...
    set /a nr=0
    set /a step=0
    set joblist=
    :MIRROR_loop
    set /a nr+=1
    set jobfile=%2
    set rcjob="%jobfile:"=%"
    %app.2% speak text %jobfile% %opt.1%
    echo. && echo     %nr%. %rcjob%
    set joblist=%joblist% %rcjob%
    for /f "tokens=2* delims=:" %%s in ('find "/sd:" "%~2%ext.1%"') do set srcdir=%%s:%%t
    for /f "tokens=1* delims=   " %%s in ("%srcdir%") do set srcdir=%%s
    if exist "%srcdir%*" (
      echo. && echo        ¯ %srcdir%        ready && set /a step+=1 && %hipip%
      for %%j in (%symlink%) do (
        if NOT exist %srcdir:~,3%%%~j\* (
          echo. && echo          ž %srcdir:~,3%%%~j        NOT mounted
                && echo          %div% && %lopip%
          set swt.1=/xd "%%~j" !swt.1!
        )
      )
    ) else (
      echo. && echo        ž %SrcDir%        MISSING 
            && echo        %div% && %lopip%
    )
      for /f "tokens=2* delims=:" %%d in ('find "/dd:" "%~2%ext.1%"') do set destdir=%%d:%%e
      for /f "tokens=1* delims= " %%d in ("%destdir%") do set destdir=%%d
      if exist "%destdir%*" (
        echo. && echo        ¯ %destdir%        ready && set /a step+=1 && %hipip%
        for %%k in (%symlink%) do (
          if NOT exist %destdir:~,3%%%~k\* (
            echo. && echo          ž %destdir:~,3%%%~k        NOT mounted 
                  && echo          %div% && %lopip%
            set swt.1=/xd "%%~k" !swt.1!
          )
        )
      ) else (
        echo. && echo        ž %destdir%        MISSING 
              && echo        %div% && %lopip%
      )
      shift /2
      if %2'==' goto :MIRROR_end
      goto :MIRROR_loop
    :MIRROR_end
    echo.
    set /a total=2*%nr%
    if %step% LSS %total% (
      echo ž Problem: %step% out of %total% steps ready Ä check connections, etc.
      goto :MIRRORSTOP_loop
    )
    echo   %rule%
    :MIR_loop
    %speak% && %select%
    if %errorlevel% EQU 3 @echo ÿÿItem %errorlevel% Ä %select0% && echo. && goto :BREAK
    if %errorlevel% EQU 2 @echo ÿÿItem %errorlevel% Ä %select2% && echo. && goto :MIR_loop
    if %errorlevel% EQU 1 @echo ÿÿItem %errorlevel% Ä %select1% && echo. && goto :MIR_end
    if %errorlevel% EQU 0 @echo ÿÿItem %errorlevel% Ä %select0% && echo. && goto :END
    :MIR_end
    cls
    echo. && echo   %s.act% Ä %comment% && echo   %rule%
    echo. && echo   Mirroring data for %UserName% on %ComputerName%
    echo.
    set /a nr=0
    for %%j in (%joblist%) do (
      set /a nr+=1
      title %%f Ä %s.act% Ä %date%  %time%
      echo     !nr!. %%j
      %app.6% %swt.1%%%f 
      echo. echo   %rule% && %endbeep% && %endwait%
    )
    for %%s in (
      s.act comment nr step joblist jobfile rcjob opt.1 srcdir symlink swt.1 destdir speak
    ) do set %%s=
    SetLocal DisableDelayedExpansion
    goto :/CLEAN
    
    :/HIBERNATE
    set comment=hibernate Windows
    title %s.act% Ä %title%
    SetLocal EnableDelayedExpansion
    cls && echo. && echo   %s.act% Ä %comment% && echo   %rule%
    set hibernate=%app.7% /h 
    echo. && echo   %rule%
    :HBNGO
    echo. && echo   Hibernate %UserName% on %ComputerName%
    echo. && echo   ¯ %Hibernate%
    echo. && echo   %rule%
    :HBNTASK_loop
    %Speak% && %select%
    if %errorlevel% EQU 3 @echo ÿÿItem %errorlevel% Ä %sel.0% && echo. && goto :BREAK
    if %errorlevel% EQU 2 @echo ÿÿItem %errorlevel% Ä %sel.2% && echo. && goto :HBNTASK_loop
    if %errorlevel% EQU 1 @echo ÿÿItem %errorlevel% Ä %sel.1% && echo. && goto :HBNTASK_end
    if %errorlevel% EQU 0 @echo ÿÿItem %errorlevel% Ä %sel.0% && echo. && goto :END
    :HBNTASK_end
    cls
    echo. && echo   %s.act% Ä %comment% && echo   %rule%
    echo. && echo   Hibernate for %UserName% on %ComputerName%
    %hibernate%
    echo.
    for %%s in (
      s.act comment hibernate speak
    ) do set %%s=
    SetLocal DisableDelayedExpansion
    echo   %rule% && %endbeep% && %endwait% && goto :END
    
    :/HELP
    set comment= ber batch file
    title %s.act% Ä %title%
    %errclr%
    cls && echo. && echo   %s.act% Ä %comment% && echo   %rule%
    echo. && echo       File: "%~f0"
    echo. && echo ¯   Syntax: "%batch%" /Switch "Command" "Command" ...
    echo. && echo   Switches: (case sensitive)
    for %%s in (%slash%) do echo             /%%s
    echo. && echo  More help: "%batch%" /Switch /?
    echo. && echo      Notes: "Command" Ä some switches require an extra "command" or 2
    echo             - labels with spaces must be "double quoted"
    echo. && echo   Executed: %~n0 %*
    goto :STOP_loop
    :STOP_loop
    echo   %ruleoff%
    %speak% && %select%
    if %errorlevel% EQU 3 @echo ÿÿItem %errorlevel% Ä %sel.0% && echo. && goto :BREAK
    if %errorlevel% EQU 2 @echo ÿÿItem %errorlevel% Ä %sel.2% && echo. && goto :STOP_loop
    if %errorlevel% EQU 1 @echo ÿÿItem %errorlevel% Ä %sel.1% && echo. && goto :END
    if %errorlevel% EQU 0 @echo ÿÿItem %errorlevel% Ä %sel.0% && echo. && goto :END
    :STOP_end
    
    :BREAK
    echo ¯ Press any key to quit this window.
    pause > nul
    goto :END
    
    :END
    popd
    for %%s in (
      profile comment slash colour errclr div rule
      favicon drives eject dvd symlink exfldr exext
      app.1 app.2 app.3 app.4 app.5 app.6 app.7
      opt.1 opt.2 opt.3 ext.1 swt.1
      no test time date title lock endwait endbeep bing hipip lopip
      message choices default select sel.0 sel.1 sel.2
    ) do set %%s=
    EndLocal
    
    EXIT
    

    将硬盘镜像到USB.RCJ

    /sd:E:\
    /dd:J:\
    /xd
        E:\FILES\Home Videos
        J:\FILES\Home Videos
    /s
    /copy:DAT
    /dcopy:T
    /timfix
    /purge
    /fft
    /r:12
    /w:5
    /np
    /tee
    /ts
    /log+:..\Logs\Mirror.LOG
    /job:Global Exclusions.RCJ
    

0 个答案:

没有答案