无法解析批处理文件中的变量

时间:2015-07-05 17:59:36

标签: batch-file

我试图写一个将要搜索的批处理文件,取决于用户工作的Windows上的版本,SIP文件夹并将其删除。

我已经有这样的事情:

@echo off
setlocal enabledelayedexpansion
set count=0

cls

echo -------------------------------
choice /c yn /t 1 /d n /M "Automode will start in 5 sec..."

if "%ERRORLEVEL%" == "2" goto auto_no
if "%ERRORLEVEL%" == "1" goto auto_yes



::========== MANUAL ============================================================
::========== CHOICE ============================================================
:auto_no
cls

echo -------------------------------
echo Starting Manual Mode...


:: ver | findstr /i "5\.1\." > nul
:: IF %ERRORLEVEL% EQU 0 goto auto_no_winxp

ver | findstr /i "10\.0\." > nul
IF %ERRORLEVEL% EQU 0 goto auto_no_winxp

ver | findstr /i "6\.0\." > nul
IF %ERRORLEVEL% EQU 0 goto auto_no_winvista
ver | findstr /i "6\.1\." > nul
IF %ERRORLEVEL% EQU 0 goto auto_no_win7
ver | findstr /i "6\.3\." > nul
IF %ERRORLEVEL% EQU 0 goto auto_no_win81
:: ver | findstr /i "10\.0\." > nul
:: IF %ERRORLEVEL% EQU 0 goto auto_no_Win10


:auto_no_winxp
echo WINDOWS XP Detected..
echo Switching to direct choose..
GOTO auto_no_direct

:auto_no_winvista
echo WINDOWS Vista Detected..
echo WINDOWS 10 Detected..
systeminfo | findstr /B /C:"OS Name" /C:"OS Version" /C:"System Manufacturer" /C:"System Type" /C:"Windows Directory" /C:"System Directory" /C:"Domain" /C:"Logon Server"

GOTO end

:auto_no_win7
echo WINDOWS 7 Detected..
systeminfo | findstr /B /C:"OS Name" /C:"OS Version" /C:"System Manufacturer" /C:"System Type" /C:"Windows Directory" /C:"System Directory" /C:"Domain" /C:"Logon Server"

GOTO end

:auto_no_win81
echo WINDOWS 8.1 Detected..
echo WINDOWS 10 Detected..
systeminfo | findstr /B /C:"OS Name" /C:"OS Version" /C:"System Manufacturer" /C:"System Type" /C:"Windows Directory" /C:"System Directory" /C:"Domain" /C:"Logon Server"

GOTO end

:auto_no_win10
echo WINDOWS 10 Detected..
systeminfo | findstr /B /C:"OS Name" /C:"OS Version" /C:"System Manufacturer" /C:"System Type" /C:"Windows Directory" /C:"System Directory" /C:"Domain" /C:"Logon Server"

GOTO end

:auto_no_direct
set source="%UserProfile%\AppData\Local\Microsoft\Communicator\"
IF EXIST "%UserProfile%\AppData\Local\Microsoft\Communicator\" (
echo Ms Lync Communicator directory found..
echo ^-^> MS Lync 2010
echo Profiles:


for %%x in (*.txt) do (
  set /a count=count+1
  set choice[!count!]=%%x
)
for /l %%x in (1,1,!count!) do (
   echo %%x] !choice[%%x]!
)


set /p select=Type profile then press ENTER:
echo Pointer_procent: %select%
echo Pointer_wykrzyk: !select!

echo Selected: !choice[%select%]!






) ELSE (
echo There not found Communicator directory
echo ^-^> No MS Lync installed..
echo ^-^> Terminating..Press any key
)

GOTO end



GOTO end
::========== MANUAL ============================================================


::========== AUTO ==============================================================
::========== CHOICE ============================================================
:auto_yes
cls
echo Consipz - Lync Profile Reset
echo -------------------------------
echo Starting Auto Mode...
GOTO end
::========== AUTO ==============================================================


:end
pause > nul

我在本节中遇到了问题:

for %%x in (*.txt) do (
  set /a count=count+1
  set choice[!count!]=%%x
)
for /l %%x in (1,1,!count!) do (
   echo %%x] !choice[%%x]!
)


set /p select=Type profile then press ENTER:
echo Pointer_procent: %select%
echo Pointer_wykrzyk: !select!

echo Selected: !choice[%select%]!

我试图询问用户选择的选项。 但我无法表明他选择了哪一个。例如: 用户看到以下选项后:

1] choice1.txt
2] choice2.txt

并选择..可能是2 应该看看:

Selected: choice2.txt

我几乎尝试了所有事情,我知道SETLOCAL可能存在问题。

请帮忙;) PS:尝试解析用户使用的Windows时跳过代码。 我故意使用W10

切换标签目标

编辑: 这是有帮助的:) 我用解决方案解决了我的问题:

if *condition* (
goto yes
)else(
goto no
)
:yes
REM menu as files

:no
REM terminate

现在我站在另一个问题面前。如何使* .txt文件不作为选项而是文件夹。这些文件夹的名称由“sip”启动,如sip_john@doe.com?

当我尝试这段代码时:

for %%x in ('dir /b /s /a:d "%UserProfile%\AppData\Local\Microsoft\Communicator\*"') do (
  set /a count=count+1
  set choice[!count!]=%%x
)

我看到这样的结果:

Profiles:
1] 'dir
2] /b
3] /s
4] /a:d
Type profile then press ENTER:

3 个答案:

答案 0 :(得分:0)

问题是指示的代码放在if内。因此,您需要!select!变量的延迟展开以及!choice[...]! 数组的延迟展开。由于!choice[!select!]!之类的东西无法正确解析,因此需要一个技巧

for %%r in (!select!) do echo !choice[%%r]!

将索引变量的值存储在for可替换参数中,我们可以得到两个没有语法问题的延迟变量。

答案 1 :(得分:0)

尝试下一步(// This checks to see if the question is mandatory and needs a answer [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)] public class CheckMandatoryAttribute : ValidationAttribute, IClientValidatable { public bool IsMandatory { get; private set; } protected override ValidationResult IsValid(object value, ValidationContext validationContext) { bool isMandatory = (bool)validationContext.ObjectType.GetProperty("IsMandatory").GetValue(validationContext.ObjectInstance, null); this.IsMandatory = isMandatory; if (isMandatory && value == null) { return new ValidationResult(ErrorMessage); } return ValidationResult.Success; ; } public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context) { var rule = new ModelClientValidationRule { ErrorMessage = ErrorMessage, ValidationType = "checkmandatory" }; rule.ValidationParameters.Add("ismandatory", IsMandatory); yield return rule; } } 子程序):

public class ConductQuestionAnswerViewModel : BaseModel
{       
    public bool IsMandatory { get; set; }

    [CheckMandatory(ErrorMessage = "Your response is mandatory.")]
    [CheckEval(ErrorMessage = "You need to make an evaluation for your response.")]
    [UIHint("Radio")]
    public int? YourResponse { get; set; }
}

答案 2 :(得分:0)

总而言之,我尝试了这段代码:

FOR /F "tokens=*" %%x in ('dir /on /b /a:d /p "%UserProfile%\AppData\Local\Microsoft\Communicator\*"') do (
  set /a count=count+1
  set choice[!count!]=%%x
)

感谢您的帮助!