Por que a subseqüência do CMD não funciona quando o BAT é chamado de SENDTO?

2

Eu escrevi um arquivo BAT para remover um '@' inicial de um nome de arquivo. (Por favor, não pergunte porquê! ... porque o Windows, porque CMD ...):)

Aqui está o arquivo BAT:

IF EXIST "%~1" (

    FOR /F %%F IN ("%~1") DO (

        CD /D "%%~dpF"
        IF ERRORLEVEL 1 (
            ECHO.  Failed to CD to folder: %%~dpF
        ) ELSE (
            SETLOCAL ENABLEEXTENSIONS
            SET A=%%~nxF

            IF "%A:~0,1%"=="@" (

                ECHO.   RENAME "%A%" "%A:~1%"
                    RENAME "%A%" "%A:~1%"
                REM   RENAME "%%A%" "%%A:~1%"
            ) ELSE (
                ECHO.   File does not start with @:  %A%
            )
            ENDLOCAL
        )
    )
) ELSE (
    ECHO.    File not found:  %1
)

pause

Quando eu o executo a partir da linha de comando em uma janela do DOS, parece funcionar:

18:54:04.97>dir /b *foo*
@foo.txt

18:54:07.97>test@ "%cd%\@foo.txt"

18:54:18.75>IF EXIST "C:\Users\me\lbin\@foo.txt" (FOR /F %F IN ("C:\Users\me\lbin\@foo.txt") DO (
CD /D "%~dpF"
 IF ERRORLEVEL 1 (ECHO.  Failed to CD to folder: %~dpF )  ELSE (
SETLOCAL ENABLEEXTENSIONS
 SET A=%~nxF
 IF "@" == "@" (
ECHO.   RENAME "@foo.txt" "foo.txt"
 RENAME "@foo.txt" "foo.txt"
 REM   RENAME "%Afoo.txt"
)  ELSE (ECHO.   File does not start with @:  @foo.txt )
 ENDLOCAL
)
) )  ELSE (ECHO.    File not found:  "C:\Users\me\lbin\@foo.txt" )

18:54:18.77>(
CD /D "C:\Users\me\lbin\"
 IF ERRORLEVEL 1 (ECHO.  Failed to CD to folder: C:\Users\me\lbin\ )  ELSE (
SETLOCAL ENABLEEXTENSIONS
 SET [email protected]
 IF "@" == "@" (
ECHO.   RENAME "@foo.txt" "foo.txt"
 RENAME "@foo.txt" "foo.txt"
 REM   RENAME "%Afoo.txt"
)  ELSE (ECHO.   File does not start with @:  @foo.txt )
 ENDLOCAL
)
)
   RENAME "@foo.txt" "foo.txt"

18:54:18.80>pause
Press any key to continue . . .
18:54:20.74>

mas quando eu executo o arquivo BAT a partir de um atalho no menu SENDTO, a operação substring não expande corretamente - especificamente, o "IF" não se expande para a direita ( IF "%A:~0,1%"=="@" ( ) - veja? .. .

C:\Users\me\lbin>IF EXIST "C:\Users\me\lbin\@foo.txt" (FOR /F %F IN ("C:\Users\me\lbin\@foo.txt") D
O (
CD /D "%~dpF"
 IF ERRORLEVEL 1 (ECHO.  Failed to CD to folder: %~dpF )  ELSE (
SETLOCAL ENABLEEXTENSIONS
 SET A=%~nxF
 IF "~0,1" == "@" (
ECHO.   RENAME "" "~1"
 RENAME "" "~1"
 REM   RENAME "%A~1"
)  ELSE (ECHO.   File does not start with @:   )
 ENDLOCAL
)
) )  ELSE (ECHO.    File not found:  C:\Users\me\lbin\@foo.txt )

C:\Users\me\lbin>(
CD /D "C:\Users\me\lbin\"
 IF ERRORLEVEL 1 (ECHO.  Failed to CD to folder: C:\Users\me\lbin\ )  ELSE (
SETLOCAL ENABLEEXTENSIONS
 SET [email protected]
 IF "~0,1" == "@" (
ECHO.   RENAME "" "~1"
 RENAME "" "~1"
 REM   RENAME "%A~1"
)  ELSE (ECHO.   File does not start with @:   )
 ENDLOCAL
)
)
   File does not start with @:

C:\Users\me\lbin>pause
Press any key to continue . . .

Idéias?

    
por rich p 01.10.2015 / 01:01

1 resposta

4

Delayed Expansion will cause variables to be expanded at execution time rather than at parse time, this option is turned on with the SETLOCAL command. When delayed expansion is in effect variables can be referenced using !variable_name! (in addition to the normal %variable_name%)

Portanto, use !A! , !A:~0,1! e !A:~1! em vez de %A% , %A:~0,1% e %A:~1% , respectivamente, da seguinte forma:

        SETLOCAL ENABLEEXTENSIONS
        SET A=%%~nxF

        IF "!A:~0,1!"=="@" (

            ECHO.   RENAME "!A!" "!A:~1!"
                    RENAME "!A!" "!A:~1!"
        ) ELSE (
            ECHO.   File does not start with @:  !A!
        )
        ENDLOCAL
    
por 01.10.2015 / 03:44