如何在批处理文件中的脚本参数中删除引号? [英] How to strip quotes from the script arguments in batch files?

查看:124
本文介绍了如何在批处理文件中的脚本参数中删除引号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您正在编写一个批处理脚本,您想在其中删除字符串中的引号。

Suppose you're writing a Batch script in which you want to strip the quotes from a string.

如果字符串存储在常规Batch变量中,则表示会这样做:

If the string is stored in a regular Batch variable, you would do this:

set name="John"
echo %name:"=%

如果字符串存储在位置参数中,则可以执行以下操作:

If the string is stored in a positional parameter, you would do this:

echo %~1

你会怎么走关于实现脚本参数的相同效果,特别是%* 变量吗?当我尝试使用与位置参数相同的符号时,会得到:

How would you go about achieving the same effect for script arguments, specifically the %* variable? When I try to use the same notation I use for positional paramters, I get this:

> echo echo %~* > script.cmd

> script "123" "456"
The following usage of the path operator in batch-parameter
substitution is invalid: %~* 


For valid formats type CALL /? or FOR /?
script was unexpected at this time.

我该如何使它工作呢?有没有特殊的语法F还是我不知道的?

How can I make this work? Is there any special syntax for this that I'm not aware of? Any help would be appreciated, thanks!

推荐答案


假设您正在编写一个批处理脚本,其中您要删除字符串中的引号。

Suppose you're writing a Batch script in which you want to strip the quotes from a string.

如果字符串存储在常规Batch变量中,则可以执行以下操作:

If the string is stored in a regular Batch variable, you would do this:

set name = John

echo%name: =%

您的断言不正确。使用字符串值时,更好的约定是始终使用 variable = value 设置 variable = value c>引用一对,然后在需要时在检索上而不是在 assignment 上显式添加引号。例如:

Your assertion is incorrect. A better convention when working with string values is always to set "variable=value" with the "variable=value" pair quoted, then add quotation marks explicitly when needed upon retrieval, rather than upon assignment. For example:

@echo off
setlocal

set "xml=<node>Text</node>"
for /f "tokens=2 delims=<>" %%I in ("%xml%") do echo Text: %%I

因此,引号标记不包含在变量值中,但是任何引号引起的特殊字符都应避免被不恰当地解释。即使这并不是您真正要问的,它仍然是值得实践的基本约定。

And thus, the quotation marks are not included in the variable value, but whatever special characters are quoted avoid being interpreted inappropriately. Even though this isn't really what you're asking, it's still a fundamental convention that's worth practicing.


字符串存储在位置参数中,您可以这样做:

If the string is stored in a positional parameter, you would do this:

echo%〜1

如何实现脚本参数(尤其是%* 变量)的相同效果?

How would you go about achieving the same effect for script arguments, specifically the %* variable?

是的,可以将波浪号表示法用于带有数字索引的单个参数,但不能用于%* 集合。要解决您的难题,您必须使用 for ... in 或<来遍历%* code> shift ...转到循环。 for ... in 是更有效的选择。这是可行的解决方案的一百种可能的途径之一。

True, you can use tilde notation for individual arguments with a numeric index, but not for the %* collection. To solve your riddle, you'd have to iterate through %* either with a for...in or shift...goto loop. for...in is the more efficient choice. Here's one of a hundred possible paths to a working solution.

@echo off
setlocal enabledelayedexpansion

set "argv[0]=%~0"
set idx=1

for %%I in (%*) do (
    set "argv[!idx!]=%%~I"
    set /a ubound = idx, idx += 1
)

for /L %%I in (0,1,%ubound%) do echo %%I: !argv[%%I]!

rem // or you could also:
rem // set argv[

这篇关于如何在批处理文件中的脚本参数中删除引号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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