标题: [文本处理] [分享]批处理把键值格式转换成表格结构另存为csv文件 [打印本页]
作者: Batcher 时间: 2023-7-4 22:25 标题: [分享]批处理把键值格式转换成表格结构另存为csv文件
【问题描述】
原文件数据结构是key:value格式,我想转换成表格结构,文件另存为csv格式,写不下去了。
有12个这样txt文件,我想把每个文件都转换了,另存为csv格式。
国债204001-5245.txt- Date:20221110,SmallNetTur:-3888547812,MidNetTur:-228056163,BigNetTur:-4234456144,SuperNetTur:72916424000
- Date:20221110,SmallNetTur:-3888547812,MidNetTur:-228056163,BigNetTur:-4234456144,SuperNetTur:72916424000
- Date:20221110,SmallNetTur:-3888547812,MidNetTur:-228056163,BigNetTur:-4234456144,SuperNetTur:72916424000
- Date:20221110,SmallNetTur:-3888547812,MidNetTur:-228056163,BigNetTur:-4234456144,SuperNetTur:72916424000
- Date:20221110,SmallNetTur:-3888547812,MidNetTur:-228056163,BigNetTur:-4234456144,SuperNetTur:72916424000
- Date:20221110,SmallNetTur:-3888547812,MidNetTur:-228056163,BigNetTur:-4234456144,SuperNetTur:72916424000
- Date:20221110,SmallNetTur:-3888547812,MidNetTur:-228056163,BigNetTur:-4234456144,SuperNetTur:72916424000
- Date:20221110,SmallNetTur:-3888547812,MidNetTur:-228056163,BigNetTur:-4234456144,SuperNetTur:72916424000
- Date:20221110,SmallNetTur:-3888547812,MidNetTur:-228056163,BigNetTur:-4234456144,SuperNetTur:72916424000
复制代码
作者: Batcher 时间: 2023-7-4 22:25
【解决方案】
test-1.bat- @echo off
- for %%U in ("*5245.txt") do (
- >"%%~nU.csv" echo,Date,SmallNetTur,MidNetTur,BigNetTur,SuperNetTur
- (for /f "tokens=1-10 delims=:," %%a in ('type %%U') do (
- echo,%%b,%%d,%%f,%%h,%%j
- ))>>"%%~nU.csv"
- )
复制代码
作者: Batcher 时间: 2023-7-4 22:26
test-2.bat- @echo off
- setlocal enabledelayedexpansion
- for %%U in ("*5245.txt") do (
- set /p FirstRow=<"%%U"
- (for /f "tokens=1-10 delims=:," %%a in ("!FirstRow!") do (
- echo,%%a,%%c,%%e,%%g,%%i
- ))>"%%~nU.csv"
- (for /f "tokens=1-10 delims=:," %%a in ('type "%%U"') do (
- echo,%%b,%%d,%%f,%%h,%%j
- ))>>"%%~nU.csv"
- )
复制代码
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |