无法解析由分隔符分隔的字符串(包含路径)
我在解析仅包含目录路径的字符串时遇到问题。例如,
我的输入字符串是 Abc\Program Files\sample\
我的输出应该是 Abc//Program Files//sample
该脚本应该适用于任何输入路径长度,即它可以包含任何编号。子目录。 (例如,abc\temp\sample\folder\joe)
我在许多链接中寻求帮助但无济于事。看起来 FOR 命令只提取一整行或一个字符串(当我们在 FOR 语法中使用“token”关键字时),但我的问题是我不知道输入路径长度,因此,没有。的代币。
我的想法是使用 \ 作为分隔符,然后提取它 () 之前和之后的每个单词,并将这些单词与 // 一起放入输出文件,直到到达字符串末尾。
我尝试实现以下内容,但它不起作用:
@echo off
FOR /F "delims=\" %%x in (orig.txt) do (
IF NOT %%x == "" echo.%%x//>output.txt
)
文件 orig.txt 仅包含一行,即 Abc\Program Files\sample\
我得到的输出仅包含: Abc//
上面的输出在 ' 之后也包含空格Abc//'
我想要的输出应该是: Abc//program Files//sample//
谁能帮我解决这个问题?
问候,
科技下一步
I have a problem with parsing a string, which consists only of directory path. For ex.,
My input string is Abc\Program Files\sample\
My output should be Abc//Program Files//sample
The script should work for input path of any length i.e., it can contain any no. of subdirectories. (For ex., abc\temp\sample\folder\joe)
I have looked for help in many links but to no avail. Looks like FOR command extracts only one whole line or a string (when we use ‘token’ keyword in FOR syntax) but my problem is that I am not aware of the input path length and hence, the no. of tokens.
My idea was to use \ as a delimiter and then extract each word before and after it (), and put the words to an output file along with // till we reach the end of the string.
I tried implementing the following but it did not work:
@echo off
FOR /F "delims=\" %%x in (orig.txt) do (
IF NOT %%x == "" echo.%%x//>output.txt
)
The file orig.txt contains only one line i.e, Abc\Program Files\sample\
The output that I get contains only: Abc//
The above output contains blank spaces as well after ‘Abc//’
My desired output should be: Abc//program Files//sample//
Can anyone please help me with this?
Regards,
Technext
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你尝试过模式匹配吗?我知道在perl和Java中你可以做一个简单的替换,找到所有的“\”并将它们替换为“//”
perl中的示例:
只需查找模式匹配或正则表达式
Have you tried pattern matching? I know in perl and Java you can do a simple replace that finds all of the "\" and replaces them with "//"
Example in perl:
Just look up pattern matching or regex
您可以使用简单的字符串替换:
You could use simple string substition:
使用循环和临时文件,以下内容适用于您的场景:
给定此输入,
您将得到以下输出:
非常丑陋,不是吗? ^^
Using a loop and a temporary file, the following works for your scenario :
Given this input
you'll get the following output :
Pretty ugly isn't it ? ^^
感谢您的投入凯拉!我知道这在 Shell 或 Perl 中很容易,但无论如何,我现在已经找到了解决方案。
这是:
type nul>output.txt
谢谢hoang。我正想发帖就看到你的回复了。
Thanks for your input Kyra! I know it's easy in Shell or Perl but anyways, i have now got the solution.
Here it is:
type nul>output.txt
Thanks hoang. I was about to post when I saw your reply.