如何得到一个for循环就用空格一行逗号分隔的字符串工作 [英] how do I get a for loop to work on a single line comma delimited string with spaces

查看:673
本文介绍了如何得到一个for循环就用空格一行逗号分隔的字符串工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个输入到一个批处理文件,它包含文件的列表(这是所有一行和许多投入到bat文件一个):

I've got an input to a batch file that contains a list of files (this is all one line and one of many inputs to the bat file):

\\\\服务器\\我的目录名\\子目录,\\\\服务器\\我的目录名\\ subdir2,\\\\服务器\\我的目录名\\ subdir3

我想遍历这个列表和列表中的每个目录中执行命令。然而,当我指定delims =,它把空间作为分隔符,即使文档说指定设置的分隔符。这将替代默认分隔符集的空间和标签。似乎并没有被替换,只是似乎增加。我已经试过搞乱与周围backq但似乎并没有工作,由于输入已经被引用。

I'd like to iterate this list and perform a command on each directory in the list. However, when I specify delims=, it treats the spaces as delimiters, even though the docs say "Specifies a delimiter set. This replaces the default delimiter set of space and tab." Doesn't seem to be replacing, just seems to be adding to. I've tried messing around with backq but that doesn't seem to work, since the input is already quoted.

我能得到的最接近的是

for /f "tokens=1-8 delims=," %%d in ("%destPath%") do (
echo %%d 
echo %%e
echo . 
    ) 

不过,我有一个UKNOWN组输入在这里,所以我原本可以得到12目录进来,不希望有(在循环体中同一行n次)的命令执行重复线,似乎击败的宗旨,为循环。

But I have an uknown set of inputs here, so I could be getting 12 directories coming in and don't want to have a repeated line for the command execution (same line n times in the loop body), seems to defeat the purpose of a for loop.

相关报道:<一href=\"http://stackoverflow.com/questions/2112694/how-do-i-get-a-for-loop-to-work-with-a-comma-delimited-string\">http://stackoverflow.com/questions/2112694/how-do-i-get-a-for-loop-to-work-with-a-comma-delimited-string

推荐答案

使用逗号作为分隔符不是一个好主意,如果因为它也是有效的文件名,你不控制输入。如果你可以用*或类似的东西,你可以确保您能够处理所有有效路径。

Using comma as a separator is not a good idea if you don't control the input since it is also valid in filenames. If you could use * or something like that, you could be sure that you are able to handle all valid paths.

我决定不打FOR命令太多了,而不是我选择了一个递归子功能

I decided not to fight the FOR command too much, instead I opted for a recursive "sub function"

:printThem
for /F "tokens=1,* delims=," %%A in ("%~1") DO (
    echo.Path: %%A
    call :printThem "%%~B"
)
@goto :EOF

call :printThem "\\Server\my directory name\subdir,\\Server\my directory name\subdir2,\\Server\my directory name\subdir3"

这篇关于如何得到一个for循环就用空格一行逗号分隔的字符串工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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