0

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

这是我的代码尝试和示例文本文件。

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命令不存储变量。

帮助非常感谢!

4

4 回答 4

1

这是解决您所有问题的单线器。:)

>answer.txt powershell "Get-Content comma3.txt | %{'{0,-10}{1,-14}{2,-19}{3,-11}{4,-4}{5}' -f $_.split(',')}"

如果您将其放入批处理脚本中,请务必在该行中替换%为。%%

answer.txt 的内容:

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

无力的解释:

对 powershell 更熟悉的人可能会比我更好地解释这一点,因为我需要大量的试验和错误来编写该行。但基本上,据我了解,它的含义如下:

  • 将 powershell 命令的输出发送到answer.txt
  • comma3.txt
  • the%是简写for each line
  • 返回类似于用于字符串参数的printf "%-10s %-14s %-19s etc."格式化line.split(",")

可能有一个Write-Content用于 powershell 的命令,但是让命令控制台重定向 powershell 的输出更容易。如果您正在处理非常大的 csv 文件并且此命令运行速度太慢,Write-Content则可能值得研究。

于 2013-03-28T15:00:26.763 回答
0
@ECHO on
@setlocal ENABLEDELAYEDEXPANSION
cls

:: loop through records
for /f "tokens=1-6 delims=," %%a in (comma3.txt) do (
set var=%%a %%b %%c %%d %%e %%f
REM  The double comma is a problem without a good solution
REM     so if the last token is null, drop it and indent. 
if '%%f' == '' set var=%%a %%b %%c          %%d %%e
REM  !'s should be used with ENABLEDELAYEDEXPANSION
echo !var! >> answer.txt
)
endlocal
于 2013-03-28T00:37:58.913 回答
0
@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

这应该可以解决问题。

循环依次FOR分配每一行的内容。line该例程:process分析一行并逐个字符地构建每一列,直到找到一个逗号。然后它会在行尾添加大量空格 - “x”将在下一个语句中被删除,并且在行上显示尾随空格有点困难(并且一些编辑器会自动将它们丢弃)

CALL SET行使用解析器特性。假设result当前是abcd并且column123 ..manyspaces.. x并且 %1 是 5。解析器通过将任何 %var% 替换为其当前值来处理该行,然后调用它,所以调用的是

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

当然没有 +s - 他们将显示解析器如何查看代码。

这样可以巧妙地去掉不需要的尾随空格和 x in column; SHIFT 从提供的参数列表中删除第一个元素,:process然后处理下一列,直到全部完成并且没有剩余参数。写出来result,我们就完成了...

于 2013-03-28T05:25:20.860 回答
0

扩展 Rojos 的答案,我创建了一个使用第 1 行列宽的 excel 宏,将文件导出为 csv,调用的 powershell 转换为空格分隔,如下所示:

runCommand = "echo Please Wait . . . "

'更改为正确的驱动器和目录:

runCommand2 = Left(SaveDirectory, 2) <br>
runCommand3 = "cd " & SaveDirectory<br>

'删除' excel 可能插入的标记

 runCommand7 = "powershell ""(get-content '" & SaveDirectory & "\" &     SaveName & ".csv" & "') -replace('""""','') | out-file '" & SaveDirectory & "\" & SaveName & "2.csv'"""

'遍历电子表格第一行的单元格以创建具有正确列宽的 powershell 命令

runCommand4 = "powershell ""Get-Content '" & SaveName & "2.csv'" & " | %{'"

x = 1
c = 0

Do Until Cells(1, x) = ""
    runCommand4 = runCommand4 & "{" & c & ",-" & Cells(1, x).Value & "}"
    x = x + 1
    c = c + 1

Loop

runCommand4 = runCommand4 & "' -f $_.split(',')} | Out-File Answer2.txt"""

runCommand5 = "cls"
runCommand6 = "pause"

删除具有列宽的行,以便它们不会被导出:

Rows("1:1").Select
Selection.Delete Shift:=xlUp

查找并替换任何逗号和制表符,因为这些可能会导致问题:

Cells.Replace What:=",", Replacement:="", LookAt:=xlPart, SearchOrder:= _
    xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Cells.Replace What:=Chr(9), Replacement:="", LookAt:=xlPart, SearchOrder:= _
    xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False

将工作簿另存为 CSV:

ActiveWorkbook.SaveAs Filename:=SaveDirectory & "\" & SaveName, FileFormat _
    :=xlCSV, CreateBackup:=False

将所有命令传递给 cmd

Call Shell("C:\Windows\System32\cmd /c" & runCommand & "&" & runCommand2 & "&" & runCommand3 & "&" & runCommand7 & "&" & runCommand4 & "&" & runCommand6, vbNormalFocus)

ActiveWorkbook.Close

如果您将 csv 导入 excel,这可能是一个方便的解决方案。

于 2015-03-02T16:30:18.860 回答