值集/ p循环。在下一步中,它会记住上一步的变量

时间:2017-03-02 11:09:31

标签: loops batch-file

我有一些小代码。问题看起来在下一步程序中记住了上次操作中的值,尽管我写了新值。在下一步,它总是从上一步获得价值。我尝试在任务之后或任务之前清理它,但是值始终是干净的。

:BEGIN
cls
VER > NUL
echo #-------------------------------------------------#
SET /P PROGRAM=# Wybierz interesujacy cie program: 
echo #-------------------------------------------------#
2>NUL CALL :CASE_%PROGRAM%
IF ERRORLEVEL 1 CALL :CASE_DEFAULT

:CASE_6
    setlocal enabledelayedexpansion
    for %%I in (`powershell.exe`) do if "%%~$PATH:I" neq "" (
        set chooser=powershell "Add-Type -AssemblyName System.windows.forms|Out-Null;$f=New-Object System.Windows.Forms.OpenFileDialog;$f.InitialDirectory='%windir%\System32\PSTools';$f.Filter='Executable Files (*.exe)|All Files (*.*)|*.*';$f.showHelp=$true;$f.ShowDialog()|Out-Null;$f.FileName"
    ) else (
    rem :: If not, compose and link C# application to open file browser dialog
        set chooser=%temp%\chooser.exe
        >"%temp%\c.cs" echo using System;using System.Windows.Forms;
        >>"%temp%\c.cs" echo class dummy{
        >>"%temp%\c.cs" echo public static void Main^(^){
        >>"%temp%\c.cs" echo OpenFileDialog f=new OpenFileDialog^(^);
        >>"%temp%\c.cs" echo f.InitialDirectory= "c:\\Windows\\System32\\PSTools\\";
        >>"%temp%\c.cs" echo f.Filter="Executable Files (*.exe)|*.exe|All Files (*.*)|*.*";
        >>"%temp%\c.cs" echo f.ShowHelp=true;
        >>"%temp%\c.cs" echo f.ShowDialog^(^);
        >>"%temp%\c.cs" echo Console.Write^(f.FileName^);}}
        for /f "delims=" %%I in ('dir /b /s "%windir%\microsoft.net\*csc.exe"') do (
            if not exist "!chooser!" "%%I" /nologo /out:"!chooser!" "%temp%\c.cs" 2>NUL
        )
        del "%temp%\c.cs"
        if not exist "!chooser!" (
            echo Error: Please install .NET 2.0 or newer, or install PowerShell.
            goto :EOF
        )
    )

    :: capture choice to a variable
    for /f "delims=" %%I in ('%chooser%') do set "filename=%%I"

    :: Clean up the mess
    del "%temp%\chooser.exe" 2>NUL

    IF "%filename%" == "C:\Windows\System32\PSTools\PsExec.exe" (
        SET /P COMPNAME="# Wybierz nazwe komputera: 
        echo # Podales: %COMPNAME%                             #
        echo #-------------------------------------------------#
        pause
        START "" %filename% -s \\%COMPNAME% cmd
    ) ELSE (
        echo # Inne narzedzia nie sa jeszcze obslugiwane.
        echo # Przepraszam
        #-------------------------------------------------#
        pause
    )
    set "COMPNAME="
    REM set "COMPNAME=%COMPNAME*=%"
    VER > NUL # reset ERRORLEVEL
    GOTO BEGIN

1 个答案:

答案 0 :(得分:0)

    SET /P COMPNAME="# Wybierz nazwe komputera: 
    echo # Podales: !COMPNAME!                             #
    echo #-------------------------------------------------#
    pause
    START "" %filename% -s \\!COMPNAME! cmd

您需要使用!compname!,因为您在代码块中更改了compname的值。