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



>脚本启动并通过文件夹"C:\ProgramData\WorkingDir"中的文本文件名识别软件将要连接到的当前数据库\一直在使用。有一个菜单选项可以继续,只是像以前一样连接,或者选择其他数据库。只需将当前工作目录内容备份到其自己的文件夹(以便稍后在切换期间重新字符串),并使用其他选择中的文件夹覆盖写入工作目录,即可选择不同的数据库。

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

C:\ProgramData\WorkingDir\Azure.txt

C:\Folder\Azure\Azure.txt

c:\Folder\Oracle\Oracle.txt

c:\Folder\SQL\SQL.txt

所以我的脚本使用机器人复制将所有内容都放到正确的位置。

唯一的问题是菜单切换选项包含当前正在使用的数据库,以切换到看起来很愚蠢的选项。我希望能够在选择中"不存在"正在使用的当前数据库。这是它的样子

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 

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

使用CHOICE命令非常简单。也许我不明白你的部分要求。我没有进行复制文件的练习,因为你说你有那个工作。

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

@ECHO OFF
SET "WORKDIR=C:ProgramDataWorkingDir"
: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

这会自行查找数据库(文件夹),因此我们不必对它们进行硬编码。
我们不需要为每个数据库提供一个子例程。

@echo off
setlocal enabledelayedexpansion
set "progdata=C:ProgramDataCompanyWorkingDir"
set "spdir=C:ProgramDataCompany"
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:TempShortcut.lnk" & goto :eof

|find /v /i "%current%"禁止显示当前数据库的输出。

注意:数据库(文件夹)不应该超过 9 个,因为我们使用数字和choice只能处理 sinlge "键"(如果有/可能超过 9 个,请切换回set /p)。

(我使用了您之前问题中的一些信息)

最新更新