批处理文件中的上下文感知菜单

时间:2017-08-25 20:10:01

标签: batch-file command-line cmd

脚本启动并通过文件夹'C:\ ProgramData \ WorkingDir'中的文本文件名识别,该软件将连接到\当前正在使用的数据库。有一个菜单选项可以继续,只需按原样连接,或者选择其他数据库。可以简单地通过将当前workingdir内容备份到其自己的文件夹(稍后在切换期间用于restring)以及用其他选项中的文件夹写入workingdir来选择不同的数据库。

所有子文件夹和名为相同的文件 - 内容不同,以启用不同的数据库访问。

C:\ ProgramData \工作目录\ Azure.txt

C:\文件夹\天青\ Azure.txt

c:\ Folder \ Oracle \ Oracle.txt

c:\ Folder \ SQL \ SQL.txt

所以我使用robocopy将脚本正常运行到正确的位置。

唯一的问题是菜单切换选择包含当前正在使用的数据库选项到swtich看起来很愚蠢。我希望能够使用当前正在使用的数据库“缺席”。这是它的样子

Your current database is Azure 
choose 1 to continue 
choose 2 to choose a different database 

extra menu 
Press 1 for Azure 
Press 2 for Oracle 
Press 3 for SQL 

what it needs to do is 'not' give a choice of the current database as it's pointless 

so..(ideally)

Your current database is Azure 
choose 1 to continue 
choose 2 to choose a different database 

extra menu
Press 1 for Oracle
Press 2 for SQL

or 

Your current database is SQL 
choose 1 to continue 
choose 2 to choose a different database 

extra menu
Press 1 for Oracle
Press 2 for Azure

or 


Your current database is ORACLE
choose 1 to continue 
choose 2 to choose a different database 

extra menu
Press 1 for SQL
Press 2 for Azure

NOT 

Your current database is ORACLE
choose 1 to continue 
choose 2 to choose a different database 

Press 1 for SQL
Press 2 for Azure
Press 3 for Oracle 

所有文件夹复制和输入命令都很好,只是这个菜单问题。 我之前试过问这个问题,但过于复杂了,有些人接近了'数组'。

2 个答案:

答案 0 :(得分:0)

使用CHOICE命令非常简单。也许我不理解你的部分要求。我没有完成复制文件的练习,因为你说你有这个工作。

至于没有将当前数据库显示为可能的选择,为什么不让它出现呢?如果他们选择它,代码将识别情况而不是将其复制。

@ECHO OFF
SET "WORKDIR=C:\ProgramData\WorkingDir"

:MenuHead
SET "CURDB=unknown"
IF EXIST "%WOKDIR%\Azure.txt" (SET "CURDB=Azure")
IF EXIST "%WORKDIR%\Oracle.txt" (SET "CURDB=Oracle")
IF EXIST "%WORKDIR%\SQL.txt" (SET "CURDB=SQL Server")
ECHO Your current database is %CURDB%

CHOICE /C AOSX /M "Choose A=Azure O=Oracle S=SQL Server X=Exit menu"
IF ERRORLEVEL 4 GOTO OutaHere
IF ERRORLEVEL 3 GOTO DoSQLServer
IF ERRORLEVEL 2 GOTO DoOracle
IF ERRORLEVEL 1 GOTO DoAzure

ECHO NB: Unknown selection %ERRORLEVEL%
GOTO MenuHead

:DoSQLServer
IF NOT EXIST "%WORKDIR%\SQL.txt" (
    CALL:BackupCurrent
    ECHO copy SQL Server in
)
GOTO MenuHead

:DoOracle
IF NOT EXIST "%WORKDIR%\Oracle.txt" (
    CALL:BackupCurrent
    ECHO copy Oracle in
)
GOTO MenuHead

:DoAzure
IF NOT EXIST "%WORKDIR%\Azure.txt" (
    CALL:BackupCurrent
    ECHO copy Azure in
)
GOTO MenuHead

:OutaHere
EXIT /B 0

REM ==================

:BackupCurrent

IF EXIST "%WORKDIR%\Azure.txt" (
    echo save off Azure
)
IF EXIST "%WORKDIR%\Oracle.txt" (
    ECHO save off Azure
)
IF EXIST "%WORKDIR%\Azure.txt" (
    ECHO save off SQL Server
)
GOTO :EOF

答案 1 :(得分:0)

它可以自己查找数据库(文件夹),因此我们不必对它们进行硬编码 我们不需要每个数据库的子程序。

@echo off
setlocal enabledelayedexpansion
set "progdata=C:\ProgramData\Company\WorkingDir"
set "spdir=C:\ProgramData\Company"
REM get current database:
for %%a in (*.txt) do set current=%%~na
REM get possible databases [folders]:
set x=0
set "c="
for /f "delims=" %%a in ('dir /b /ad "%progdata%\*.txt"') do (
  set /a x+=1
  set "m[!x!]=%%a"
  set "c=!c!!x!"
)
REM print menu:
for /l %%a in (1,1,%x%) do (
  echo %%a - !m[%%a]!|find /v /i "%current%"
)
REM ask for new selection:
choice /n /c %c% /m "Select new database: "
set new=!m[%errorlevel%]!
if %new%==%current% (
  echo same - no switching 
  goto :eof
)
REM do the moving:
robocopy "%progdata%" "%spdir%\%current%" /MIR >nul 2>&1 & robocopy "%spdir%\%new%" "%progdata%" /MIR >nul 2>&1
start "" "C:\Temp\Shortcut.lnk" & goto :eof

|find /v /i "%current%"会抑制当前数据库的输出。

注意:不应超过9个数据库(文件夹)因为我们使用数字而choice只能处理sinlge“key”(如果有/可能超过9,则切换回{{ 1}})。

(我使用了previous question

中的一些信息