批处理文件转换逗号分隔,以固定长度 [英] Batch file convert comma delimited to fixed length

查看:209
本文介绍了批处理文件转换逗号分隔,以固定长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将文本文件转换为固定长度的文件:

下面是我的code尝试和示例文本文件。

 德尔answer.txt@ECHO上
@setlocal ENABLEDELAYEDEXPANSION
CLS
集空间=
VAR集::通过循环记录
FOR / F令牌= 1-6跳过= 1 delims =%%一中(comma3.txt)做(
回声令牌%%一个%% b %%ç%% d %%已Ë%%˚F
组VAR = %%一%的空间%%% B%空间%%% C%空间%%% d %%已Ë%%˚F
回声VAR%VAR%
回声%VAR%GT;> answer.txt

ENDLOCAL
暂停

输入:

  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

请注意:

设定VAR 命令不存储变量。

帮助非常AP preciated!


解决方案

  @ECHO关闭
SETLOCAL
集columnsizes = 10 14 19 11 4 4
DEL answer.txt 2 - ; NUL::通过循环记录
FOR / Fdelims =%%一中(comma3.txt)做(
SET结果=
SET行= %%一
CALL:进程%columnsizes%

ECHO。======================================
TYPE answer.txt
ECHO。======================================
暂停
GOTO:EOF:处理
REM IF%1==回声%的结果%GT;> answer.txt ECHO%的结果%放大器; GOTO:EOF
IF%1==回声%的结果%GT;> answer.txt和放大器; GOTO:EOF
SET栏=
:colloop
如果没有定义行GOTO endcol
SET CH1 =%行:〜0.1%
SET行=%线:〜1%
如果不是%CH1%==,SET列=%列%% CH1%放大器; GOTO colloop
:endcol
SET列=%列%×
呼叫建立结果=%结果%%%列:〜0%1 %%
转移
GOTO过程

这应该破解的难题。

循环依次在每个行的内容分配给。常规:进程分析艾琳并建立每列,字符由字符,直到找到一个逗号。然后,它添加了大量的空间,以该行的结束 - 的X在接下来的语句会被砍掉,这是一个利特尔难治显示上线尾随空格(有些编辑器扔掉自发)

呼叫设置行使用的解析器的特点。假设结果目前 ABCD 123 ..manyspaces .. X 和%1是5分析器通过它的电流值更换任何%VAR%处理线,然后调用它,所以所谓的

  SET结果= ABCD +%+%列:0.5〜+%

未经+ S当然 - 他们展现解析器如何看待code

这样整齐的LOP关闭不需要的尾随空格和X在;移位删除从参数列表提供给的第一个元素:进程和下一列被处理,直到所有已经完成,并且没有留下参数。写出结果,我们就大功告成了...

How to convert a text file to a fixed length file:

Here is my code attempt and the sample text file.

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

Input:

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

Desired output:

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

Note:

The set var command does not store the variables.

Help GREATLY appreciated!

解决方案

@ECHO off
SETLOCAL
set columnsizes=10 14 19 11 4 4
DEL answer.txt 2>nul

:: loop through records
for /f "delims=" %%a in (comma3.txt) do (
SET "result="
SET line=%%a
CALL :process %columnsizes%
)
ECHO.======================================
TYPE answer.txt
ECHO.======================================
PAUSE 
GOTO :eof

:process
REM IF "%1"=="" echo %result%>>answer.txt ECHO %result%&GOTO :eof
IF "%1"=="" echo %result%>>answer.txt&GOTO :eof
SET "column="
:colloop
IF NOT DEFINED line GOTO endcol
SET ch1=%line:~0,1%
SET line=%line:~1%
IF NOT "%ch1%"=="," SET column=%column%%ch1%&GOTO colloop
:endcol
SET column=%column%                           x
CALL SET result=%result%%%column:~0,%1%%
SHIFT
GOTO process

This should crack the problem.

The FOR loop assigns the contents of each line to line in turn. The routine :process analyses the aline and builds each column, character-by-character until it finds a comma. It then adds a large number of spaces to the end of the line - the "x" will be lopped off in the next statement and it's a litle dificult to show trailing spaces on lines (and some editors throw them away unprompted)

The CALL SET line uses a parser characteristic. Suppose result is currently abcd and column is 123 ..manyspaces.. x and %1 is 5. The parser processes the line by replacing any %var% by its current value, then CALLs it, so what is CALLed is

SET result=abcd  +%+%column:~0,5+%

without the +s of course - they're to show how the parser sees the code.

So that neatly lops off the unwanted trailing spaces and the x in column; the SHIFT removes the first element from the parameter list provided to :process and the next column is processed until all have been done and there are no parameters left. Write out the result, and we're done...

这篇关于批处理文件转换逗号分隔,以固定长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆