我有一个字符串,用逗号(,)分隔了一些名字,我希望在用逗号(,)分割之后制作make目录
例如,如果字符串是abc,def,ghi
然后我要创建3个名为abc和def和ghi
的文件夹我试过这个
@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(
我可以分割字符串,但我知道如何使用这些名称创建文件夹
任何人都可以帮助我吗?
答案 0 :(得分: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"
由于逗号是for
命令中的分隔符,因此您可以迭代变量内容(第一个选项)
如果要允许文件夹名称中的空格(也是for
命令中的分隔符),可以使用一个简单的技巧,用逗号替换为{{1}来迭代列表引用变量,所以值","
转换为first folder,second folder,third folder
然后重复,再次使用逗号作为分隔符,但保留文件夹名称中的空格
如果文件夹名称可以包含其他特殊字符("first folder","second folder","third folder"
,&
,;
,>
,...)