在Windows批处理文件中将反斜杠更改为正斜杠 [英] Change backslash to forward slash in windows batch file
问题描述
我正在尝试在包含文件名和位置的变量中将所有反斜杠()转换为正斜杠(/).我已阅读并看到:
I'm trying to convert all backslashes () to forward slashes (/) in a variable which contains a file name and location. I've read about this and seen:
%variable:str1=str2%
和
set "var=%var:\=/%"
我尝试过的
,但是显然我做错了.
which I've attempted, but I'm obviously not getting it right.
这是我的.bat脚本的相关部分:
Here is the relevant section of my .bat script:
FOR %%f IN ("E:\myfiles\app1\data\*.csv") DO (
echo %%f
set "f=%%f:\=/%"
echo %%f
echo.
)
输出显示每个文件名列出两次.
The output show each filename listed twice.
即这行:
set "f=f:\=/%"
没有按照我想要的去做.有人可以看到我在做什么错吗?
is not doing what I want it to. Can anyone see what I am doing wrong?
推荐答案
在块语句(a parenthesised series of statements)
中,解析整个块并然后执行.块中的任何%var%
都将在解析该块时被该变量的值取代-在执行该块之前-相同的情况适用于FOR ... DO (block)
.
Within a block statement (a parenthesised series of statements)
, the entire block is parsed and then executed. Any %var%
within the block will be replaced by that variable's value at the time the block is parsed - before the block is executed - the same thing applies to a FOR ... DO (block)
.
因此,IF (something) else (somethingelse)
将在遇到IF
时使用%variables%
的值执行.
Hence, IF (something) else (somethingelse)
will be executed using the values of %variables%
at the time the IF
is encountered.
克服此问题的两种常见方法是:1)使用setlocal enabledelayedexpansion
并使用!var!
代替%var%
来访问更改后的var
值,或2)调用子例程以使用更改值.
Two common ways to overcome this are 1) to use setlocal enabledelayedexpansion
and use !var!
in place of %var%
to access the changed value of var
or 2) to call a subroutine to perform further processing using the changed values.
因此请注意使用CALL ECHO %%var%%
来显示更改后的var
值.
Note therefore the use of CALL ECHO %%var%%
which displays the changed value of var
.
您的代码包含两个单独的变量,分别称为f
.
Your code contains two separate variables called f
.
第一个是称为f
的循环控制元变量",并由%%f
引用.
The first is the loop-control 'metavariable' called f
and referenced by %%f
.
第二个是由set "f=..."
语句建立的公共环境变量f
.可以在block
中使用%f%
但来访问此变量,它似乎保留了解析控制for
时的值(实际上,任何%var%
在解析时替换为var
当时的值)
The second is the common environment variable f
which is established by the set "f=..."
statement. This variable can be accessed by using %f%
but within a block
, it will appear to retain the value it had when the controlling for
was parsed (in fact, any %var%
is replaced at parse-time by the value of var
at that time)
metavariables
不能在子字符串或替换之类的字符串操作语句中使用,只能将常见的环境变量用于这些操作,因此需要将元变量f
的值分配给环境变量f
和然后执行环境变量f
的字符串替换任务.
metavariables
cannot be used in string-manipulation statements like substrings or substitutes, only common environment variables can be used for these operations, hence you need to assign the value of the metavariable f
to the environment variable f
and then perform the string-substitution task of the environment variable f
.
当然,有一个转折,就是必须使用delayedexpansion
和!var!
语法来访问块中环境变量的修改后的值.
The twist, of course, is that you must use delayedexpansion
and the !var!
syntax to access the modified value of an environment variable within a block.
所以
setlocal enabledelayedexpansion
for...%%f...do (
echo %%f
set "f=%%f"
set "f=!f:\=/!"
echo !f!
)
echo just for demonstration %f% !f! %%f
这将以必需的方式设置f
的值(当然,您始终可以更改名称以避免混淆...)
This sets the value of f
in the required manner (of course, you could always change the name to avoid confusion...)
最后一行只是为了说明f
所获取的最终值可以以%f%
或!f!
的形式在循环外部进行访问,并且%%f
不在上下文中并显示为%f
.
The last line is simply to show that the final value acquired by f
can be accessed outside of the loop as either %f%
or !f!
, and that %%f
is out-of-context and shown as %f
.
没有delayedexpansion
的另一种方法是
for...%%f...do (
echo %%f
set "f=%%f"
call set "f=%%f:\=/%%"
call echo %%f%%
)
echo just for demonstration %f% !f! %%f
区别在于使用call
和加倍%
,最后一行将显示!f!
就是这样-一个字面值,因为在delayedexpansion
模式之外,!
只是另一个对cmd
没有特殊意义的字符.
the difference being the use of call
and doubling the %
s, and the final line will show !f!
as just that - a literal, since outside of delayedexpansion
mode, !
is just another character with no special meaning to cmd
.
这篇关于在Windows批处理文件中将反斜杠更改为正斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!