How do I get a for loop to work on the same line with comma delimited with spaces

I have an entry in a batch file containing a list of files (this is one line and one of many entries in the bat file):

"\\Server\my directory name\subdir,\\Server\my directory name\subdir2,\\Server\my directory name\subdir3"

I would like to repeat this list and execute the command in each directory in the list. However, when I specify delims =, it treats spaces as delimiters, although the docs say, "Sets the delimiter set. This replaces the default delimiter set and the tab." This does not seem to be a replacement, it just seems. I tried communicating with backq, but this does not seem to work, since the input is already specified.

The closest I can get

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

But I have an uv set of input here, so I can get 12 directories and don’t want to have a duplicate line to execute the command (same line n times in the body of the loop), it seems that the target of the for loop wins.

Related: How to get a for loop to work with a comma delimited string?

+1
source share
2 answers

Using a comma as a delimiter is not a good idea if you do not control the input, as it is also valid in file names. If you can use * or something like that, you can be sure that you can handle all valid paths.

I decided not to fight too much with the FOR command, instead I decided to use a recursive "subfunction"

: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"
+2
source

DOS (cmd.exe), ( - ) vbscript.

Set objFS = CreateObject("Scripting.FileSystemObject")
Set objArgs = WScript.Arguments
strFile = objArgs(0)
Set objFile = objFS.OpenTextFile(strFile)
Do Until objFile.AtEndOfStream
    strLine = objFile.ReadLine
    s = Split(strLine,",")
    For Each strDir In s
        WScript.Echo strDir
    'Set objFolder = objFS.GetFolder(strDir)
    'WScript.Echo strDir & " modified date is " & objFolder.DateLastModified 
    'Set objFolder = Nothing
    Next
Loop

myscript.vbs

C:\test>cscript //nologo myscript.vbs file
\\Server\my directory name\subdir
\\Server\my directory name\subdir2
\\Server\my directory name\subdir3

script, , , . ,

@echo off

for /F "delims=" %%f in ('cscript //nologo test.vbs file') do (
 echo do something with %%f
)
0

Source: https://habr.com/ru/post/1791192/


All Articles