从批处理文件中的变量删除双引号会在CMD环境中产生问题 [英] Removing double quotes from variables in batch file creates problems with CMD environment

查看:321
本文介绍了从批处理文件中的变量删除双引号会在CMD环境中产生问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以帮助从批量变量中删除引号的有效和安全的方法?

Can anybody help with effective and safe way of removing quotes from batch variables?

我写了一个批处理文件,成功导入参数列表%1, 2,%3等,并将它们放入命名变量。这些参数中的一些包含多个单词,因此用双引号括起来。

I have written a batch file which successfully imports a list of parameters %1, %2, %3 etc. and places them into named variables. Some of these parameters contain multiple words, and therefor are enclosed in double quotes.

> "Susie Jo" (%1)  
> "Smith Barnes" (%2)  
> "123 E. Main St." (%3)  

这些%变量接下来放在命名变量中:

These %variables are next placed in named variables:

> set FirstName=%1  
> set LastName=%2  
> set ShipAddr=%3  

验证变量是通过echo完成的。

verification of variables is done by echo.


echo。%FirstName%

echo。%LastName%

echo。%ShipAddr%

echo.%FirstName%
echo.%LastName%
echo.%ShipAddr%

结果显示为

"Susie Jo"  
"Smith Barnes"  
"123 E. Main St."  

我需要删除所选变量中包含的引号。例如,FirstName和LastName在其他地方使用,不能包含引号。

I need to eliminate the included quotes on selected variables. For instance, FirstName and LastName are used elsewhere and must not include quotes.

在测试批处理文件中,我使用变量中的〜tilde字符成功消除了引号。 / p>

In a test batch file I was successful at eliminating quotes using the ~tilde character in variables.

> set FirstName=%~1
> set LastName=%~2 



我认为我有解决方案,的批处理文件。突然CMD不能识别长路径的状态。从完整路径正常执行批处理文件

I thought I had the solution, but I soon experienced unusual behavior with execution of batch files. Suddenly CMD is no recognizing long path statments. Normal execution of batch file from full path

> C:\Documents and Settings\Administrator\My Documents\Txt\batchtest\dataout.bat

返回

> 'C:\Documents' is not recognized as an internal or external command....

看起来,向进来的%1%2 ...%n变量中添加符号字符已经引起了一些变化。可能有一些环境变量被修改了吗?

So it would appear that the addition of the ~tilde character to the in-coming %1 %2...%n variables has caused some change. Possibly some environment variables have been altered?

我也尝试使用FOR命令尝试清除变量中的引号。这看起来很尴尬,我无法通过创建一个变量列表来完成这个任务:

I also tried clearing quotes from within variable with various attempts using the FOR command. That seems awkward and I have been unable to learn how to accomplish this by creating a list of variable to perform the task:

这样:

for %%g in (%FirstName% %LastName%) do (
set %%g=%%~g
set %%h=%%~h
set FirstName=%%~g
set LastName=%%h
echo.%FirstName% %LastName%
)

我想我有两个问题。

1)我在传入的%1%2变量(%〜1等)中插入〜tilde的想法似乎影响了一些设置,改变了CMD如何导航长路径名。

1) My 'short and sweet' idea of inserting ~tilde in the incoming %1 %2 variables (%~1, etc) seems to have affected some settings and altered how CMD navigates long pathnames.

2)我仍然在寻找一个干净,简单的方法来消除选定命名变量的引号。

2) I'm still in search of a clean and easy way to eliminate quotes from selected named variables.

任何帮助那些更有经验的人都会非常感激。我在这里的技能结束...需要一些指导。

Any help for those more experienced would be most appreciated. I'm at the end of my skills here... need some guidance please!

编辑12/26/2009 13:36 PST
整个批处理文件

edit 12/26/2009 13:36 PST entire batch file:


Blockquote

:: dataout.bat

:: revision 12/25 / 2009添加到传入%变量以消除嵌入的引号。

::使用命令行参数写入地址列表

::写入QBooks的数据输出列表IIF导入

:: writes RUI的商家订单数据

::用于测试的示例命令行字符串

:: listmail [firstname] [lastname] [address string] [[城市字符串]] [状态] [zip] [订单#] [采购日期] [注册名称] [名字姓氏[交易ID] [付款方式] [总计] [产品编号] [数量] [价格] Shipping[CommissionPmt] [Invoice#]

:: example:dataout Bellewinkle Moose123 Green Forest WayVancouverWA 98664 1004968 05/25/2009Bellewinkle MooseOlive Oyl101738帐户20.67 FK-1P 1 8.95 3.00 1.39 239

@echo off

cls

c:

cd\

cd文件和设定\administrator\my documents\txt\batchtest

回应处理%1%2

:VARISET

::将%n个命令行参数转换为字符串变量

set($ FirstName)=%〜1

set($ LastName)=%〜2

set($ BillingAddress1)=%〜3

set($ BillingCity)=%〜4

set($ BillingState)=%〜5

set ($ BillingPostal)=%〜6

设置($ OrderNumber)=%〜7

设置($ Purch_Date)=%〜8

设置RegistrationName)=%〜9

shift

set($ TransactionID)=%〜9

shift

set($ PaymentMethod) =%〜9

shift

set($ Total)=%〜9

shift
set($ ProductIdentifier)=%〜9 < br>
shift

set($ Quantity)=%〜9

shift

set($ Price_Each)=%〜9

shift

set($ Pack_Prep)=%〜9

shift

set($ Shipping)=%〜9

shift

set($ ServiceFee)=%〜9

shift

set($ Discount)=%〜9

移动

set($ Invoice)=%〜9

shift

set($ UnitPrice)=%〜9

set _ShipCombName =%($ FirstName)%%($ LastName)%

echo ship组合名称为%_ShipCombName%

pause

::将字符串varibables写入日志文件

echo FN%($ FirstName)%LN%($ LastName)%BA%($ BillingAddress1)%%($ BillingCity)%%($ BillingState)%% OrderNumber)%%($ Purch_Date)%%($ RegistrationName)%%($ TransactionID)%%($ PaymentMethod)%%($ Total)%%($ ProductIdentifier)%% %%($ Pack_Prep)%%($ Shipping)%%($ ServiceFee)%%($ Discount)%%($ Invoice)%%($ UnitPrice)%%_ShipCombName%>> d_out_log.txt

::由服务提供商分配帐户

IF / i%($ PaymentMethod)%==亚马逊应收账款SET _QBAcct = Amazon.com

:: 12-25-2009添加第二个Amazon pm't方法的多功能性

IF / i%($ PaymentMethod)%== Amazon SET _QBAcct = Amazon.com

IF / i%($ PaymentMethod)%= = MAST SET _QBAcct = Auth / Net

IF / i%($ PaymentMethod)%== MasterCard SET _QBAcct = Auth / Net

IF / i%($ PaymentMethod)%= = Visa SET _QBAcct = Auth / Net

IF / i%($ PaymentMethod)%== PayPal SET _QBAcct = PayPalPmts

IF / i%($ PaymentMethod)%== On帐户SET _QBAcct =%($ RegistrationName)%

IF / i%($ PaymentMethod)%== Mail SET _QBAcct =%($ RegistrationName)%

IF / i% $ paymentMethod)%== AMER SET _QBAcct = Auth / Net

IF / i%($ PaymentMethod)%== DISC SET _QBAcct = Auth / Net

:: Assign Rep指示符基于QBAccount

IF / i%($ PaymentMethod)%== Amazon应收账款SET _Rep = Amazon

:: 12-25-2009添加了第二个亚马逊pm't方法的多功能性

IF / i%($ PaymentMethod)%== Amazon SET _Rep = Amazon

IF / i%($ PaymentMethod)%== MAST SET _Rep = BlueZap

IF / i%($ PaymentMethod)%== MasterCard SET _Rep = BlueZap

IF / i%($ PaymentMethod)%== Visa SET _Rep = BlueZap

/ i%($ PaymentMethod)%== PayPal SET _Rep = BlueZap

IF / i%($ PaymentMethod)%==帐户SET _Rep = RB

IF / i% ($ PaymentMethod)%== Mail SET _Rep = RB

IF / i%($ PaymentMethod)%== AMER SET _Rep = BlueZap

IF / i%($ PaymentMethod) %== DISC SET _Rep = BlueZap

::检查重复的地址数据

findstr / i / s%_ShipCombName%addrlist.txt

echo错误级别:%errorlevel%

如果错误级别1 goto:ADDRWRITE

如果错误级别0 goto:ADDRFOUND

:ADDRWRITE

echo%_ShipCombName %>> addrlist.txt

echo%($ BillingAddress1)%>> addrlist.txt

echo%($ BillingCity)%%($ BillingState)%%($ BillingPostal) %>> addrlist.txt

echo。 >> addrlist.txt

echo地址文件写入

:ADDRFOUND

echo所选代表是%_Rep%

echo所选帐户是:%_QBAcct%

pause

:: RUI OUT

:: write商家订单ID& RUI订单ID到RUI

::检查writeRUI.txt中的重复RUI数据

cd ..

cd RegKOut

find / i%($ OrderNumber)%writeRUI.txt

echo errorlevel:%errorlevel%

if errorlevel 1 goto:RUIWRITE

if errorlevel 0 goto :IIFWRITE

:RUIWRITE

echo%($ Invoice)%%($ OrderNumber)%>> writeRUI.txt

:: end write RUI

:: IIF OUT

:IIFWRITE

::检查writeIIF.txt中的重复发票数据

find / i%($ OrderNumber)%writeIIF.txt

echo errorlevel:%errorlevel%

如果errorlevel 1 goto:HEADWRITE

如果错误级别0 goto:LINEWRITE

:HEADWRITE

::写标题,船/处理,折扣,委托数据到QB IIF导入文件

echo%($ OrderNumber)%%($ Purch_Date)%发票%($ TransactionID)%%_QBAcct%应收帐款%($总计)%%_Rep%>> writeIIF.txt

echo H / P%($ Pack_Prep)%1? >> writeIIF.txt

echo SHP%($ Shipping)%1? >> writeIIF.txt

echo DISC%($ Discount)%1? >> writeIIF.txt

echo Comm%($ ServiceFee)%1? >> writeIIF.txt

:LINEWRITE

IF / i%($ ProductIdentifier)%equ PH-1 goto WRITE_DEFA ELSE goto WRITE_DISC

echo%($ ProductIdentifier)%

:WRITE_DISC

::写入从自定义变量解析的折扣价格:

echo%($ ProductIdentifier)%%($ Price_Each)%% ($ Quantity)%? >> writeIIF.txt

goto:EOF

:WRITE_DEFA

:写入从产品数据解析的默认价格

echo%($ ProductIdentifier)%%($ UnitPrice)%%($ Quantity)%? >> writeIIF.txt

goto:EOF

:: 3秒延迟

:: TYPE NUL | CHOICE.COM / N / CY / TY,3> NUL

:EOF

Blockquote
:: dataout.bat
:: revision 12/25/2009 add ~tilde to incoming %variables to eliminate embedded "quotation marks.
:: writes address list using command line parameters
:: writes data output list for QBooks IIF import
:: writes Merchant Order data for RUI
:: sample command line string for testing
:: listmail[firstname][lastname]["address string"]["city string"][state][zip][Order#][PurchDate][Regname]["FirstName LastName"][TransactionID][PaymentMethod][Total][ProductID][Qty][Price_Each][PackPrep] [Shipping] [CommissionPmt] [Invoice#]
:: example: dataout Bellewinkle Moose "123 Green Forest Way" "Vancouver" WA 98664 1004968 05/25/2009 "Bellewinkle Moose" "Olive Oyl" 101738 "On Account" 20.67 FK-1P 1 8.95 3.00 1.39 239
@echo off
cls
c:
cd\
cd documents and settings\administrator\my documents\txt\batchtest
echo processing %1 %2
:VARISET
:: Convert %n command line parameters to string variables
set ($FirstName)=%~1
set ($LastName)=%~2
set ($BillingAddress1)=%~3
set ($BillingCity)=%~4
set ($BillingState)=%~5
set ($BillingPostal)=%~6
set ($OrderNumber)=%~7
set ($Purch_Date)=%~8
set ($RegistrationName)=%~9
shift
set ($TransactionID)=%~9
shift
set ($PaymentMethod)=%~9
shift
set ($Total)=%~9
shift set ($ProductIdentifier)=%~9
shift
set ($Quantity)=%~9
shift
set ($Price_Each)=%~9
shift
set ($Pack_Prep)=%~9
shift
set ($Shipping)=%~9
shift
set ($ServiceFee)=%~9
shift
set ($Discount)=%~9
shift
set ($Invoice)=%~9
shift
set ($UnitPrice)=%~9
set _ShipCombName=%($FirstName)% %($LastName)%
echo ship combo name is %_ShipCombName%
pause
:: write string varibables to logfile
echo FN %($FirstName)% LN %($LastName)% BA %($BillingAddress1)% %($BillingCity)% %($BillingState)% %($BillingPostal)% %($OrderNumber)% %($Purch_Date)% %($RegistrationName)% %($TransactionID)% %($PaymentMethod)% %($Total)% %($ProductIdentifier)% %($Quantity)% %($Price_Each)% %($Pack_Prep)% %($Shipping)% %($ServiceFee)% %($Discount)% %($Invoice)% %($UnitPrice)% %_ShipCombName% >> d_out_log.txt
:: Assign Account by Service Provider
IF /i %($PaymentMethod)%==Amazon Receivables SET _QBAcct=Amazon.com
:: 12-25-2009 added second Amazon pm't method for versatility
IF /i %($PaymentMethod)%==Amazon SET _QBAcct=Amazon.com
IF /i %($PaymentMethod)%==MAST SET _QBAcct=Auth/Net
IF /i %($PaymentMethod)%==MasterCard SET _QBAcct=Auth/Net
IF /i %($PaymentMethod)%==Visa SET _QBAcct=Auth/Net
IF /i %($PaymentMethod)%==PayPal SET _QBAcct=PayPalPmts
IF /i %($PaymentMethod)%==On Account SET _QBAcct=%($RegistrationName)%
IF /i %($PaymentMethod)%==Mail SET _QBAcct=%($RegistrationName)%
IF /i %($PaymentMethod)%==AMER SET _QBAcct=Auth/Net
IF /i %($PaymentMethod)%==DISC SET _QBAcct=Auth/Net
:: Assign Rep designator based on QBAccount
IF /i %($PaymentMethod)%==Amazon Receivables SET _Rep=Amazon
:: 12-25-2009 added second Amazon pm't method for versatility
IF /i %($PaymentMethod)%==Amazon SET _Rep=Amazon
IF /i %($PaymentMethod)%==MAST SET _Rep=BlueZap
IF /i %($PaymentMethod)%==MasterCard SET _Rep=BlueZap
IF /i %($PaymentMethod)%==Visa SET _Rep=BlueZap
IF /i %($PaymentMethod)%==PayPal SET _Rep=BlueZap
IF /i %($PaymentMethod)%==On Account SET _Rep=R B
IF /i %($PaymentMethod)%==Mail SET _Rep=R B
IF /i %($PaymentMethod)%==AMER SET _Rep=BlueZap
IF /i %($PaymentMethod)%==DISC SET _Rep=BlueZap
:: check for duplicate address data
findstr /i /s "%_ShipCombName%" addrlist.txt
echo errorlevel: %errorlevel%
if errorlevel 1 goto :ADDRWRITE
if errorlevel 0 goto :ADDRFOUND
:ADDRWRITE
echo %_ShipCombName% >> addrlist.txt
echo %($BillingAddress1)% >> addrlist.txt
echo %($BillingCity)% %($BillingState)% %($BillingPostal)% >> addrlist.txt
echo. >> addrlist.txt
echo Address File Written
:ADDRFOUND
echo selected rep is %_Rep%
echo selected account is: %_QBAcct%
pause
:: RUI OUT
:: write Merchant Order ID & RUI Order ID to RUI
:: check for duplicate RUI data in writeRUI.txt
cd..
cd RegKOut
find /i "%($OrderNumber)%" writeRUI.txt
echo errorlevel: %errorlevel%
if errorlevel 1 goto :RUIWRITE
if errorlevel 0 goto :IIFWRITE
:RUIWRITE
echo %($Invoice)% %($OrderNumber)% >> writeRUI.txt
:: end write RUI
:: IIF OUT
:IIFWRITE
:: Check for duplicate invoice data in writeIIF.txt
find /i "%($OrderNumber)%" writeIIF.txt
echo errorlevel: %errorlevel%
if errorlevel 1 goto :HEADWRITE
if errorlevel 0 goto :LINEWRITE
:HEADWRITE
:: write Header, Ship/Handling, discount, Rep & commission data to QB IIF import file
echo %($OrderNumber)% %($Purch_Date)% Invoice %($TransactionID)% %_QBAcct% Accounts Receivable %($Total)% %_Rep% >> writeIIF.txt
echo H/P %($Pack_Prep)% 1 ? >> writeIIF.txt
echo SHP %($Shipping)% 1 ? >> writeIIF.txt
echo DISC %($Discount)% 1 ? >> writeIIF.txt
echo Comm %($ServiceFee)% 1 ? >> writeIIF.txt
:LINEWRITE
IF /i %($ProductIdentifier)% equ PH-1 goto WRITE_DEFA ELSE goto WRITE_DISC
echo %($ProductIdentifier)%
:WRITE_DISC
::writes discounted prices parsed from custom variable:
echo %($ProductIdentifier)% %($Price_Each)% %($Quantity)% ? >> writeIIF.txt
goto :EOF
:WRITE_DEFA
:writes default prices parsed from Product data
echo %($ProductIdentifier)% %($UnitPrice)% %($Quantity)% ? >> writeIIF.txt
goto :EOF
:: 3-second delay
:: TYPE NUL | CHOICE.COM /N /CY /TY,3 >NUL
:EOF


推荐答案

在结尾处有一个额外的双引号,将其添加回字符串的末尾(在从字符串中删除两个引号之后)。

You have an extra double quote at the end, which is adding it back to the end of the string (after removing both quotes from the string).

输入:

set widget="a very useful item"
set widget
set widget=%widget:"=%
set widget

输出:

widget="a very useful item"
widget=a very useful item

注意:要用双引号替换双引号,请执行以下操作:

Note: To replace Double Quotes " with Single Quotes ' do the following:

set widget=%widget:"='%

注意:要替换World (不区分大小写)执行以下操作:

Note: To replace the word "World" (not case sensitive) with BobB do the following:

set widget="Hello World!"
set widget=%widget:world=BobB%
set widget

输出:

widget="Hello BobB!"


$ b b

至于你的初始问题(将以下代码保存到批处理文件.cmd或.bat并运行):

As far as your initial question goes (save the following code to a batch file .cmd or .bat and run):

@ECHO OFF
ECHO %0
SET BathFileAndPath=%~0
ECHO %BathFileAndPath%
ECHO "%BathFileAndPath%"
ECHO %~0
ECHO %0
PAUSE

输出:

"C:\Users\Test\Documents\Batch Files\Remove Quotes.cmd"
C:\Users\Test\Documents\Batch Files\Remove Quotes.cmd
"C:\Users\Test\Documents\Batch Files\Remove Quotes.cmd"
C:\Users\Test\Documents\Batch Files\Remove Quotes.cmd
"C:\Users\Test\Documents\Batch Files\Remove Quotes.cmd"
Press any key to continue . . .

%0 是脚本名称和路径。

%1 是第一个命令行参数,以此类推。

%0 is the Script Name and Path.
%1 is the first command line argument, and so on.

这篇关于从批处理文件中的变量删除双引号会在CMD环境中产生问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆