无法解析由分隔符分隔的字符串(包含路径)

发布于 2024-09-06 10:10:19 字数 750 浏览 13 评论 0原文

我在解析仅包含目录路径的字符串时遇到问题。例如,

我的输入字符串是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

韬韬不绝 2024-09-13 10:10:19

你尝试过模式匹配吗?我知道在perl和Java中你可以做一个简单的替换,找到所有的“\”并将它们替换为“//”

perl中的示例:

my $var = "Abc\Program Files\sample\"
$var  =~ tr/\\/\/\//; 

只需查找模式匹配或正则表达式

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:

my $var = "Abc\Program Files\sample\"
$var  =~ tr/\\/\/\//; 

Just look up pattern matching or regex

番薯 2024-09-13 10:10:19

您可以使用简单的字符串替换:

C:\>set myvariable=Abc\Program Files\sample\    

C:\>echo %myvariable%
Abc\Program Files\sample\

C:\>set myvariable=%myvariable:\=//%

C:\>echo %myvariable%    
Abc//Program Files//sample//

You could use simple string substition:

C:\>set myvariable=Abc\Program Files\sample\    

C:\>echo %myvariable%
Abc\Program Files\sample\

C:\>set myvariable=%myvariable:\=//%

C:\>echo %myvariable%    
Abc//Program Files//sample//
何其悲哀 2024-09-13 10:10:19

使用循环和临时文件,以下内容适用于您的场景:

@echo off > output.txt
FOR /F "tokens=1,* delims=\" %%x in (orig.txt) do ( 
IF NOT %%y=="" echo %%x//%%y>>output.txt
)
:loop
@echo off > temp.txt
set /a stop=1
FOR /F "tokens=1,* delims=\" %%x in (output.txt) do (
IF NOT "%%y"=="" (
echo %%x//%%y>>temp.txt
set /a stop=0
) ELSE (echo %%x>>temp.txt)
)
IF NOT %stop%==1 (
move /Y temp.txt output.txt
) ELSE (goto end)
goto loop
:end
move /Y temp.txt output.txt

给定此输入,

Abc\Program Files\sample\
Another\Example\\\

您将得到以下输出:

Abc//Program Files//sample
Another//Example

非常丑陋,不是吗? ^^

Using a loop and a temporary file, the following works for your scenario :

@echo off > output.txt
FOR /F "tokens=1,* delims=\" %%x in (orig.txt) do ( 
IF NOT %%y=="" echo %%x//%%y>>output.txt
)
:loop
@echo off > temp.txt
set /a stop=1
FOR /F "tokens=1,* delims=\" %%x in (output.txt) do (
IF NOT "%%y"=="" (
echo %%x//%%y>>temp.txt
set /a stop=0
) ELSE (echo %%x>>temp.txt)
)
IF NOT %stop%==1 (
move /Y temp.txt output.txt
) ELSE (goto end)
goto loop
:end
move /Y temp.txt output.txt

Given this input

Abc\Program Files\sample\
Another\Example\\\

you'll get the following output :

Abc//Program Files//sample
Another//Example

Pretty ugly isn't it ? ^^

假扮的天使 2024-09-13 10:10:19

感谢您的投入凯拉!我知道这在 Shell 或 Perl 中很容易,但无论如何,我现在已经找到了解决方案。

这是:


type nul>output.txt

FOR /F "usebackq tokens=*" %%x in ("orig.txt") do (
    set LINE=%%x
    call set LINE=%%LINE:\=//%%
    call echo:%%LINE%%
) >>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

FOR /F "usebackq tokens=*" %%x in ("orig.txt") do (
    set LINE=%%x
    call set LINE=%%LINE:\=//%%
    call echo:%%LINE%%
) >>output.txt

Thanks hoang. I was about to post when I saw your reply.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文