bat-MD文件转CSV文件

2022-11-08,,,

目录
1. bat文件里面写死文件名
2. 拖入文件


注意:每个单元格不能出现字符【|、$、;】

1. bat文件里面写死文件名

@echo off && setlocal enabledelayedexpansion

SET mdfile=MD-CSV.md
SET csvfile=csv-md.csv :: 当前目录下创建文件
cd. >%csvfile% set /a index=0 set "nativesplit=|"
set "splitstr=$"
set "tmpsplit=,"
set end=0
set one=0
set "stepstr="
set "splitstrempty=$ "
set inforsum=30
set inforindex=0
set "teststring="
set "enter=;" for /f "delims=" %%a in (%mdfile%) do (
set "tmpstr="
set /a index+=1
set data1=%%a if !index! NEQ 2 (
if "!data1:~-1!"==" " (
set "data1=!data1:~0,-1!"
) set "data1=!data1:~1,-1!" set "teststring=!teststring!!enter!!data1!"
)
) set "teststring=!teststring:|=$!" :splitend
echo !teststring!| findstr !splitstrempty! >nul && (
set "teststring=!teststring:%splitstrempty%=%splitstr%!"
set /a inforindex+=1
if !inforindex! == !inforsum! (
set inforindex=0
goto splitstart
)
goto splitend
) || (
set inforindex=0
goto splitstart
) :splitstart
set "splitstrempty= $"
echo !teststring!| findstr !splitstrempty! >nul && (
set "teststring=!teststring:%splitstrempty%=%splitstr%!"
set /a inforindex+=1
if !inforindex! == !inforsum! (
set inforindex=0
goto headsplit
)
goto splitstart
) || (
set inforindex=0
goto headsplit
) :headsplit
set "teststring=!teststring:$=,!"
set teststring=!teststring:~1,-1!
echo !teststring! :stringLOOP
if "!teststring!" EQU "" (
goto END
) :: 注意:切割符号
for /f "delims=;" %%a in ("!teststring!") do (
set substring=%%a
)
if "!substring:~-1!"==" " (
set "substring=!substring:~0,-1!"
)
if "!substring:~-1!"==" " (
set "substring=!substring:~0,-1!"
)
if "!substring:~0,1!"==" " (
set "substring=!substring:~1!"
)
echo !substring!>>%csvfile% :striploop
set stripchar=!teststring:~0,1!
set teststring=!teststring:~1! :: 如果为空字符串,就结束
if "!teststring!" EQU "" (
goto END
) if "!stripchar!" NEQ "!enter!" (
set "substring="
goto striploop
) goto stringloop
) :END pause

2. 拖入文件

生成的csv文件格式为MD2CSV-MD文件名.csv

@echo off && setlocal enabledelayedexpansion

set nativemdfile=%~1
set mdfile=%~n1
SET "csvfile=MD2CSV-!mdfile!.csv" :: 当前目录下创建文件
cd. >%csvfile% set /a index=0 set "nativesplit=|"
set "splitstr=$"
set "tmpsplit=,"
set end=0
set one=0
set "stepstr="
set "splitstrempty=$ "
set inforsum=30
set inforindex=0
set "teststring="
set "enter=;" for /f "delims=" %%a in (%nativemdfile%) do (
set "tmpstr="
set /a index+=1
set data1=%%a if !index! NEQ 2 (
if "!data1:~-1!"==" " (
set "data1=!data1:~0,-1!"
) set "data1=!data1:~1,-1!" set "teststring=!teststring!!enter!!data1!"
)
) set "teststring=!teststring:|=$!" :splitend
echo !teststring!| findstr !splitstrempty! >nul && (
set "teststring=!teststring:%splitstrempty%=%splitstr%!"
set /a inforindex+=1
if !inforindex! == !inforsum! (
set inforindex=0
goto splitstart
)
goto splitend
) || (
set inforindex=0
goto splitstart
) :splitstart
set "splitstrempty= $"
echo !teststring!| findstr !splitstrempty! >nul && (
set "teststring=!teststring:%splitstrempty%=%splitstr%!"
set /a inforindex+=1
if !inforindex! == !inforsum! (
set inforindex=0
goto headsplit
)
goto splitstart
) || (
set inforindex=0
goto headsplit
) :headsplit
set "teststring=!teststring:$=,!"
set teststring=!teststring:~1,-1! :stringLOOP
if "!teststring!" EQU "" (
goto END
) :: 注意:切割符号
for /f "delims=;" %%a in ("!teststring!") do (
set substring=%%a
)
if "!substring:~-1!"==" " (
set "substring=!substring:~0,-1!"
)
if "!substring:~-1!"==" " (
set "substring=!substring:~0,-1!"
)
if "!substring:~0,1!"==" " (
set "substring=!substring:~1!"
)
echo !substring!>>%csvfile% :striploop
set stripchar=!teststring:~0,1!
set teststring=!teststring:~1! :: 如果为空字符串,就结束
if "!teststring!" EQU "" (
goto END
) if "!stripchar!" NEQ "!enter!" (
set "substring="
goto striploop
) goto stringloop
) :END pause

bat-MD文件转CSV文件的相关教程结束。

《bat-MD文件转CSV文件.doc》

下载本文的Word格式文档,以方便收藏与打印。