批处理脚本提示需要多个条目

时间:2015-11-28 23:05:26

标签: windows batch-file

所以我有这个子程序,我想从批处理文件中的另一个位置调用。这些函数可以正常工作,但由于某些原因我无法确定,提示希望用户输入TWICE,然后才能接受任何内容。

说,如果我输入“0”,回到上一个菜单,它会立即回到提示符,我必须再次输入“0”才会真正返回上一个菜单(其他地方)在我的主要剧本中。)

我可以说,输入“w”(或任何其他值),然后第二次输入我实际想要使用的那个,它最终会完成。

这让我疯了。

:subfullbackup
cls

if exist "%current%\Backup\Full_Backup" (
  Echo  Backup folder already exists
  Echo.
  Echo  [o]  Overwrite local device files with existing local files
  Echo  [w]  Wipe current local backup and start fresh
  Echo.
  set /p choice=Select: 


  if %choice% == o (
    Echo.
    Echo  Depending on how much data you have,
    Echo  this could take a couple hours.
    Echo.
    Echo  Backing up...
    adb pull /sdcard/ "%current%\Backup\Full_Backup" >nul 2>&1
    Echo.
    Echo  -= BACKUP COMPLETE =-
    Pause
    Goto :backup
  )


  if %choice% == w (
    Echo.
    Echo  Removing all current local backup files in 'Full_Backup'
    rmdir /S /Q "%current%\Backup\Full_Backup" >nul 2>&1
    Echo.
    Echo  Depending on how much data you have,
    Echo  this could take a couple hours.
    Echo.
    Echo  Backing up...
    adb pull /sdcard/ "%current%\Backup\Full_Backup" >nul 2>&1
    Echo.
    Echo  -= BACKUP COMPLETE =-
    Pause
    Goto :backup
  )


  if not %choice% == o goto subfullbackup
  if not %choice% == w goto subfullbackup
) else (
    Echo.
    Echo  Depending on how much data you have,
    Echo  this could take a couple hours.
    Echo.
    Echo  Backing up...
    adb pull /sdcard/ "%current%\Backup\Full_Backup" >nul 2>&1
    Echo.
    Echo  -= BACKUP COMPLETE =-
    Pause
    Goto :backup
)
Goto :eof

1 个答案:

答案 0 :(得分:1)

使用延迟扩展的批处理代码,在批处理脚本的顶部使用命令 setlocal 启用,它还会创建所有环境变量的副本,并记住当前目录以恢复变量列表,当前目录和 endlocal 上的命令扩展和延迟扩展的当前状态或离开批处理:

@echo off
setlocal EnableDelayedExpansion
set "current=%CD%"

:FullBackup
cls

if exist "%current%\Backup\Full_Backup" (
  Echo  Backup folder already exists
  Echo.
  Echo  [o]  Overwrite local device files with existing local files
  Echo  [w]  Wipe current local backup and start fresh
  Echo.
  set "UserChoice="
  set /p "UserChoice=Select: "


  if /I "!UserChoice!" == "o" (
    Echo.
    Echo  Depending on how much data you have,
    Echo  this could take a couple hours.
    Echo.
    Echo  Backing up...
    adb.exe pull /sdcard/ "%current%\Backup\Full_Backup" >nul 2>&1
    Echo.
    Echo  -= BACKUP COMPLETE =-
    Pause
    Goto DoBackup
  )


  if /I "!UserChoice!" == "w" (
    Echo.
    Echo  Removing all current local backup files in 'Full_Backup'
    rmdir /S /Q "%current%\Backup\Full_Backup" >nul 2>&1
    Echo.
    Echo  Depending on how much data you have,
    Echo  this could take a couple hours.
    Echo.
    Echo  Backing up...
    adb.exe pull /sdcard/ "%current%\Backup\Full_Backup" >nul 2>&1
    Echo.
    Echo  -= BACKUP COMPLETE =-
    Pause
    Goto DoBackup
  )

  goto FullBackup

) else (
    Echo.
    Echo  Depending on how much data you have,
    Echo  this could take a couple hours.
    Echo.
    Echo  Backing up...
    adb.exe pull /sdcard/ "%current%\Backup\Full_Backup" >nul 2>&1
    Echo.
    Echo  -= BACKUP COMPLETE =-
    Pause
    Goto DoBackup
)
Goto :EOF

:DoBackup

但您的批处理代码也可以在没有延迟扩展的情况下编写,并且更紧凑,避免重复的代码行:

@echo off
set "current=%CD%"

:FullBackup
cls

if exist "%current%\Backup\Full_Backup" goto PromptBackup

:OverwriteBackup
Echo.
Echo  Depending on how much data you have,
Echo  this could take a couple hours.
Echo.
Echo  Backing up...
adb.exe pull /sdcard/ "%current%\Backup\Full_Backup" >nul 2>&1
Echo.
Echo  -= BACKUP COMPLETE =-
Pause
Goto DoBackup

:PromptBackup
Echo  Backup folder already exists
Echo.
Echo  [o]  Overwrite local device files with existing local files
Echo  [w]  Wipe current local backup and start fresh
Echo.
set "UserChoice="
set /p "UserChoice=Select: "

if /I "%UserChoice%" == "o" goto OverwriteBackup

if /I not "%UserChoice%" == "w" goto FullBackup

Echo.
Echo  Removing all current local backup files in 'Full_Backup'
rmdir /S /Q "%current%\Backup\Full_Backup" >nul 2>&1
goto OverwriteBackup

:DoBackup

关于代码中的小变化的一些注释:

  1. choice(SS64文章)是标准的Windows命令。因此,建议避免choice(Microsoft文章)作为环境变量或标签的名称。使用UserChoice(CamelCase拼写以便于阅读)代替选择

  2. backup(SS64文章)不是标准的Windows命令,而是标准的SQL命令。因此,建议避免将备份作为环境变量或标签的名称。在上面的批处理代码中使用了DoBackup

  3. 建议在提示用户之前为环境变量定义默认值。用户只能点击 RETURN ENTER ,在这种情况下环境变量会保持其值。

    在提示用户之前,使用set "UserPoint="清除环境变量,因此当用户不输入任何内容时,该变量不存在。

    可能还会set "UserPoint=o"set "UserPoint=w"定义有效的默认值。

  4. 应始终使用双引号来比较字符串与用户输入,以避免在用户输入任何内容时因语法错误而退出批处理。

    当用户输入任何语法错误并导致命令处理器破坏批处理时,

    if %choice% == w (变为if == w (

    当用户在代码中输入任何内容时,

    if /I "%UserChoice%" == "w" (变为if /I "" == "w",上面的代码仍然是有效的批处理代码,因此可以进行处理。

    注意:用户现在可以通过输入"w"来中断批处理 但在此可以预期用户在被要求ow时不会输入一个或多个双引号。

  5. 在比较用户输入的字符串与预定义字符串时,如果字母包含在比较字符串中,建议不要进行不区分大小写的字符串比较。

    选项/I将字符串比较从区分大小写更改为不区分大小写。

    现在,用户还可以输入OW,这可以解释为ow