如何将文本文件转换为固定长度的文件:批量文件转换逗号分隔固定长度
这里是我的代码的尝试和示例文本文件。
del answer.txt
@ECHO on
@setlocal ENABLEDELAYEDEXPANSION
cls
set space=
set var
:: loop through records
for /f "tokens=1-6 skip=1 delims=," %%a in (comma3.txt) do (
echo tokens %%a %%b %%c %%d %%e %%f
set var=%%a%space%%%b%space%%%c%space%%%d%%e%%f
echo var %var%
echo %var% >> answer.txt
)
endlocal
pause
输入:
1116559,P1303251287,20130325225906CD,13013822,1,0000
1104220,P1303250282,20130325070119CD,,1,0000
1064743,P1303251094,20130325191600CD,,0,0000
1100819,P1303250369,20130325091722CD,,0,0000
1101405,P1303250051,20130325010740CD,,0,0000
所需的输出:
1116559 P1303251287 20130325225906CD 13013822 1 0000
1104220 P1303250282 20130325070119CD 1 0000
1064743 P1303251094 20130325191600CD 0 0000
1100819 P1303250369 20130325091722CD 0 0000
1101405 P1303250051 20130325010740CD 2 0000
注:
的set var
命令不存储变量。
帮助非常感谢!
你有PowerShell的安装? Powershell可以格式化类似于'printf'的文本。如果PowerShell是不是一种选择,它可能仍然是可能的空间,填补了一些变量来模拟固定宽度列。 – rojo 2013-03-28 01:59:35
我相信我有PowerShell的,但从来没有做过任何事的。如果它是一个简单,可靠的方案,我将开始在该产品educationg自己。我发现这些网站经常引用它。 – Stumped 2013-03-28 14:38:54
然后看到我的答案。 (: – rojo 2013-03-28 15:06:27