1

I have a string containing some names in comma , separated and I want to make make directories after splitting by comma ,

For example if the string is abc,def,ghi

Then I want to create 3 folders named as abc and def and ghi

I tried like this

@echo off&setlocal 
set "data_set_path=def,ghi,jkl"
REM first split by commas
for /f "tokens=1-4 delims=," %%i in ("%data_set_path%") do set "pc1=%%i"& set "pc2=%%j"& set "pc3=%%k"&set "pc4=%%l"
<nul set/p"=1st split: %pc1% %pc2% %pc2% %pc4%"&echo( 

I can split the string but I do know how to create folders with those names

bad_coder
  • 11,289
  • 20
  • 44
  • 72
SpringLearner
  • 13,738
  • 20
  • 78
  • 116

1 Answers1

2
@echo off
    setlocal enableextensions disabledelayedexpansion

    rem The easy one
    set "data_set_path=def,ghi,jkl"
    for %%a in (%data_set_path%) do if not exist "%%a" md "%%a"

    rem A more "elaborated" to allow spaces in the folders
    set "data_set_path=first folder,second folder,third folder"
    for %%a in ("%data_set_path:,=","%") do if not exist "%%a" md "%%~a"

As the comma is a delimiter inside for commands, you can just iterate over the variable contents (first option)

If you want to allow for spaces inside the folder names (that are also a delimiter in for commands), a simple trick can be used, iterating over the list with the the commas replaced with "," and the variable quoted, so the value first folder,second folder,third folder is converted to "first folder","second folder","third folder" and then iterated, using the commas as delimiters again but keeping the spaces inside the folder names

This second approach is also needed if the folder names can include other special characters (&, ;, >, <, ...)

MC ND
  • 69,615
  • 8
  • 84
  • 126
  • Thanks for the answer.Is there any way to ignore the existing folders?In the present answer if a folder presents then it shows *A subdirectory or file first folder already exists.* – SpringLearner Oct 08 '14 at 09:46
  • @SpringLearner, yes, check for existence of the folder (now in the answer) or directly ignore the creation error using `md "%%a" 2>nul` to hide errors – MC ND Oct 08 '14 at 09:49
  • I was reading one of your [answers](http://stackoverflow.com/questions/20782734/open-a-url-without-using-a-browser-from-a-batch-file) ,you are passing the url as an argument.Can you please tell me how to use without passing the url as an arguement.I want to keep the url name inside the .cmd file. – SpringLearner Mar 12 '15 at 14:01
  • @SpringLearner, `%1` is the first argument, so replace references to it. Remove the `if not "%~1"=="" ` and change the following line into something like `wscript //E:JScript "%~dpnx0" "http://www.somewhere.com"` – MC ND Mar 12 '15 at 14:50
  • Thanks for answering.Is it possible to run a url after every n seconds.I mean I will execute a batch file only once and after that the url mentioned inside the cmd file should be repetedly after 1 hour/1day or what ever time I mention. I wanted to know if this can be done using script.If yes tell me I will put a new question in stackoverflow so that you can answer – SpringLearner Mar 12 '15 at 16:22
  • @SpringLearner, is there any reason not to use the OS scheduler? – MC ND Mar 12 '15 at 16:24
  • there is no specific reasons.I am doing this for the 1st time.I dont know which is effective either using batch or task scheduler. My only aim is to call a url after certain interval of time.If you know any other ways then you are most welcome.If you are free then please come my [chat room](http://chat.stackoverflow.com/transcript/message/22049349#22049349) – SpringLearner Mar 12 '15 at 16:40