gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 00:35:55 27 4
gpt4 key购买 nike

我正在尝试为我们的azure Web应用程序部署编写自定义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

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

最佳答案

不要注意剥离周围的引号。相反,定义不带双引号的变量,如下所示:

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!
)

关于powershell - cmd批处理文件删除字符失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40111659/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com