cmd批处理文件删除字符失败

时间:2016-10-18 14:57:39

标签: powershell azure batch-file

我正在尝试为我们的Azure应用程序部署编写自定义deploy.cmd。我创建了一个test.cmd脚本来测试删除文件路径的第一个和最后一个字符:

"D:\Program Files (x86)\npm\3.10.3\npm.cmd"

原因是如果powershell脚本没有被引用,它会被扰乱,但是cmd文件需要它才能被引用才能正确运行。我的测试脚本是:

echo off
setlocal enabledelayedexpansion

SET DEPLOYMENT_SOURCE=D:\home\site\wwwroot

SET NPM_CMD="D:\Program Files (x86)\npm\3.10.3\npm.cmd"
echo %NPM_CMD%
set NPM_CMD=%NPM_CMD:~1, -1%
echo %NPM_CMD%

pushd "%DEPLOYMENT_SOURCE%"
call "%NPM_CMD%" cache clean
call "%NPM_CMD%" install
pushd "%DEPLOYMENT_SOURCE%"

这样可以正常调用npm。所以我接受了:

set NPM_CMD=%NPM_CMD:~1, -1%

将它放入我的主deploy.cmd文件中:

echo off
setlocal enabledelayedexpansion

:: Setup
:: -----        

echo starting deploy.cmd
echo -------------------
echo %*
SET all=%*
SET NPM_GLOBAL=%1
SET NPM_INFO=%2

echo all: %all%
echo NPM_GLOBAL: %NPM_GLOBAL%
echo NPM_INFO: %NPM_INFO%

SET DEPLOYMENT_SOURCE=D:\home\site\wwwroot

if NPM_GLOBAL=="True" (
    REM install specific version of NPM
    echo installing specific version of NPM @ %NPM_INFO%
    npm install -g npm@%NPM_INFO%
    %NPM_CMD%=npm
) else (    
    SET NPM_CMD=%NPM_INFO%
    set NPM_CMD=%NPM_CMD:~1, -1%
    echo %NPM_CMD%
    echo NPM_CMD set to %NPM_CMD%
)

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

echo Handling node.js deployment.
echo %DEPLOYMENT_SOURCE%
ECHO %DEPLOYMENT_SOURCE%\package.json

echo Install npm packages
IF EXIST "%DEPLOYMENT_SOURCE%\package.json" (
  pushd "%DEPLOYMENT_SOURCE%"
  echo Cleaning NPM cache.
  call "%NPM_CMD%" cache clean
  call "%NPM_CMD%" install
  IF !ERRORLEVEL! NEQ 0 goto error
  popd
)

这将提供以下输出:

PS D:\home\site\wwwroot> .\deploy.cmd False "D:\Program Files (x86)\npm\3.10.3\npm.cmd"

D:\home\site\wwwroot>echo off 
starting deploy.cmd
-------------------
False "D:\Program Files (x86)\npm\3.10.3\npm.cmd"
all: False "D:\Program Files (x86)\npm\3.10.3\npm.cmd"
NPM_GLOBAL: False
NPM_INFO: "D:\Program Files (x86)\npm\3.10.3\npm.cmd"
ECHO is off.
NPM_CMD set to 
'"~1, -1"' is not recognized as an internal or external command,
operable program or batch file.
Handling node.js deployment.
D:\home\site\wwwroot
D:\home\site\wwwroot\package.json
Install npm packages
Cleaning NPM cache.
PS D:\home\site\wwwroot> '"~1, -1"' is not recognized as an internal or external command,
operable program or batch file.
The system cannot find the batch label specified - error

我不明白为什么它不知道如何删除第一个和最后一个字符及其错误原因。我看不出该文件有什么问题。

2 个答案:

答案 0 :(得分:4)

不要照看剥离周围的引号。相反,定义变量而不包含双引号,如下所示:

SET "DEPLOYMENT_SOURCE=D:\home\site\wwwroot"
SET "NPM_CMD=D:\Program Files (x86)\npm\3.10.3\npm.cmd"

然后使用双引号括起来 如有必要,如下所示:

echo %NPM_CMD%

pushd "%DEPLOYMENT_SOURCE%"
call "%NPM_CMD%" cache clean
call "%NPM_CMD%" install

要将上述内容应用于主deploy.cmd文件,请阅读call /?

…
In addition, expansion of batch script argument references (%0, %1,
etc.) have been changed as follows:

    %* in a batch script refers to all the arguments (e.g. %1 %2 %3 %4 %5 ...)

    Substitution of batch parameters (%n) has been enhanced.  You can
    now use the following optional syntax:

        %~1         - expands %1 removing any surrounding quotes (")
        %~f1        - expands %1 to a fully qualified path name
        %~d1        - expands %1 to a drive letter only
        %~p1        - expands %1 to a path only
Press any key to continue . . .

并在必要时应用:

echo starting deploy.cmd
echo -------------------
echo %*
SET all=%*
SET "NPM_GLOBAL=%1"       1st parameter is not in double quotes already
SET "NPM_INFO=%~2"        2nd parameter is stripped of surrounding double quotes

此外,您需要使用!variable!代替%variable%应用延迟扩展,例如:

(    
    SET "NPM_CMD=%NPM_INFO%"
    rem not necessary now set "NPM_CMD=!NPM_CMD:~1, -1!"
    echo !NPM_CMD!
    echo NPM_CMD set to !NPM_CMD!
)

答案 1 :(得分:1)

要删除引用字符串周围的引号,请在参数编号前插入代字号(~)。

%1 to %~1

将批处理文件的开头更改为此;

echo starting deploy.cmd
echo -------------------
echo %*
SET all=%*
SET NPM_GLOBAL=%~1
SET NPM_INFO=%~2