需求:读取station.ini文件,创建父节点(上、下、客场站集)文件夹到D盘,并根据配置拷贝当前文件夹下各个目录到指定的父节点文件夹。

以下为.bat文件内容:

@echo off & setlocal enabledelayedexpansion
COLOR 0a
FOR /F "eol=; tokens=1*  delims==" %%i IN (G:\Source\station.ini) do (
    set p=%%i
    set q=%%j
    if "!p:~0,1!"=="[" (
        if "!p:~-1!"=="]" (
            rem 获取站名
            set name=!p:~1,-1!
        )
    )else (       
        set fatherNm=!p!
        set fileNm=!q!
        if "!fatherNm!"=="fatherName" (
            rem 获取父节点名称并创建文件夹
            if not exist d:\!fileNm! md d:\!fileNm!           
            set creatFile=!fileNm!         
        )       
    )
    if not "!name!"=="" (
        if not "!creatFile!"=="" (      
            rem 拷贝G:\Source下各个文件夹到对应的站集 遍历G:\Source文件
            for /d %%a in (*) do (
                set sonFileNm=%%a
                if "!sonFileNm!"=="!name!" (               
                    if not exist d:\!creatFile!\!sonFileNm! (
                        rem 以下赋值目的在于剔除末尾空格
                        set covertFileNm=!creatFile: =!
                        xcopy "G:\Source\!sonFileNm!" "d:\!covertFileNm!\!sonFileNm!\" /e/h/r/y                       
                    ) else (
                    echo 存在同名文件&goto :eof
                    )
                )
            )
            set name=
            set creatFile=
         )
    )
)
pause

各个文件存放位置如下图所示:

批处理读取配置文件并作相应处理_批处理 

结果为在D盘生成三个文件夹。

配置文件内容:(station.ini)

;;[SJT]当前站站码缩写
;;stationNm当前站名
;;areaNo当前站场码
;;fatherName归属于某个站集
;;=前后不要有空格
[sjt]
stationNm=上峰尾
areaNo=0x06
fatherName=上行站集

[ssc]
stationNm=上出发
areaNo=0x04
fatherName=上行站集

[ssd]
stationNm=上到达
areaNo=0x02
fatherName=上行站集

[sxf]
stationNm=下峰尾
areaNo=0x05
fatherName=下行站集

[sxc]
stationNm=下出发
areaNo=0x03
fatherName=上行站集

[sxd]
stationNm=下到达
areaNo=0x01
fatherName=上行站集

[snk]
stationNm=客场
areaNo=0x07
fatherName=客场站集