string 在 Windows 批处理文件中将反斜杠更改为正斜杠

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23542453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:16:45  来源:igfitidea点击:

Change backslash to forward slash in windows batch file

windowsstringbatch-fileslash

提问by user3253319

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%

and

set "var=%var:\=/%"

which I've attempted, but I'm obviously not getting it right.

我已经尝试过,但显然我没有做对。

Here is the relevant section of my .bat script:

这是我的 .bat 脚本的相关部分:

FOR %%f IN ("E:\myfiles\app1\data\*.csv") DO (
  echo %%f  
  set "f=%%f:\=/%"
  echo %%f  
  echo.                 
)

The output show each filename listed twice.

输出显示每个文件名列出两次。

i.e. this line:

即这一行:

set "f=f:\=/%"

is not doing what I want it to. Can anyone see what I am doing wrong?

没有做我想做的事。谁能看到我做错了什么?

采纳答案by Magoo

Within a block statement (a parenthesised series of statements), the entireblock is parsed and thenexecuted. 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).

在块语句中(a parenthesised series of statements)整个块被解析然后执行。%var%块中的任何内容都将在解析块时被该变量的值替换- 在执行块之前 - 同样的事情适用于FOR ... DO (block).

Hence, IF (something) else (somethingelse)will be executed using the values of %variables%at the time the IFis encountered.

因此,IF (something) else (somethingelse)将使用遇到%variables%时的值来执行IF

Two common ways to overcome this are 1) to use setlocal enabledelayedexpansionand use !var!in place of %var%to access the changed value of varor 2) to call a subroutine to perform further processing using the changed values.

克服这个问题的两种常见方法是 1) 使用setlocal enabledelayedexpansion和使用!var!代替%var%访问 的更改值var或 2) 调用子例程以使用更改的值执行进一步处理。

Note therefore the use of CALL ECHO %%var%%which displays the changed value of var.

因此请注意CALL ECHO %%var%%which的使用显示了 的更改值var

Your code contains two separate variables called f.

您的代码包含两个单独的变量,称为f.

The first is the loop-control 'metavariable' called fand referenced by %%f.

第一个是由 调用f和引用的循环控制“元变量” %%f

The second is the common environment variable fwhich is established by the set "f=..."statement. This variable can be accessed by using %f%butwithin a block, it will appear to retain the value it had when the controlling forwas parsed (in fact, any %var%is replaced at parse-time by the value of varat that time)

第二个是fset "f=..."语句建立的公共环境变量。此变量可以通过使用被访问%f%内的block,它会出现保留它有当控制值for被解析(事实上,任何%var%通过的值替换在分析时var在那个时候

metavariablescannot 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 fto the environment variable fand thenperform the string-substitution task of the environment variable f.

metavariables不能在子串或替换等字符串操作语句中使用,只能使用公共环境变量进行这些操作,因此需要将元f变量的值赋给环境变量f然后执行环境变量的字符串替换任务f.

The twist, of course, is that you must use delayedexpansionand the !var!syntax to access the modified value of an environment variable within a block.

当然,扭曲是您必须使用delayedexpansion!var!语法来访问块内环境变量的修改值。

So,

所以,

setlocal enabledelayedexpansion
for...%%f...do (
  echo %%f
  set "f=%%f"
  set "f=!f:\=/!"
  echo !f!
)
echo just for demonstration %f% !f! %%f

This sets the value of fin the required manner (of course, you could always change the name to avoid confusion...)

f以所需的方式设置了 的值(当然,您可以随时更改名称以避免混淆......)

The last line is simply to show that the final value acquired by fcan be accessed outside of the loop as either %f%or !f!, and that %%fis out-of-context and shown as %f.

最后一行只是为了表明f可以在循环外以%f%or 或 的形式访问by 获取的最终值!f!,这%%f是上下文之外的并显示为%f

Another way to do this without delayedexpansionis

无需这样做的另一种方法delayedexpansion

for...%%f...do (
  echo %%f
  set "f=%%f"
  call set "f=%%f:\=/%%"
  call echo %%f%%
)
echo just for demonstration %f% !f! %%f

the difference being the use of calland doubling the %s, and the final line will show !f!as just that - a literal, since outside of delayedexpansionmode, !is just another character with no special meaning to cmd.

区别在于使用call和加倍%s,最后一行将显示!f!为 - 文字,因为在delayedexpansion模式之外,!只是另一个对 没有特殊意义的字符cmd

回答by foxidrive

This will change the back-slashes to forward-slashes in a variable:

这会将变量中的反斜杠更改为正斜杠:

set "variable=E:\myfiles\app1\data\*.csv"
set "variable=%variable:\=/%"
echo "%variable%"

回答by dethorpe

This seems to work for me:

这似乎对我有用:

echo off

setlocal enabledelayedexpansion

FOR %%f IN ("C:\tools\workspace\*") DO (
  set old=%%f
  echo !old!
  set new=!old:\=/!
  echo !new!  
  echo.                 
)

Using a seperate variable rather than the loop variable makes the difference, along with enabling delayed expansion as the variable substittion syntex using the loop variable %%f dosn't seem to work.

使用单独的变量而不是循环变量会产生差异,同时启用延迟扩展,因为使用循环变量 %%f 的变量替换语法似乎不起作用。

回答by Lakshya Bhardwaj

This will work for change the backslashe to forward slashes in windows:

这将适用于在 Windows 中将反斜杠更改为正斜杠:

 typedef std::basic_string<TCHAR> tstring;
 tstring pathbasic = tstring(programdata) + _T("\myfile.txt");
 std::replace(pathbasic.begin(), pathbasic.end(), _T('\'), _T('/'));