使用批处理脚本将行从.csv文件复制到另一个.csv文件

问题描述:

我正在从设备(输出)创建.csv文件,需要将特定数量的行从该文件复制到另一个具有相同格式的.csv文件中.

I am creating .csv files from a device (output) and need to copy a specific number of lines from this file into another .csv file which has the same format.

幸运的是,它们的格式相同,因此每个文件都有相同的行要复制(第68至107行).我使用其他来源的代码进行了此操作,但是到目前为止,我只能复制数据,但无法将其插入其他文件.任何帮助都会很棒!

They are luckily the same format and so each file has the same lines to copy over (line 68 to 107). I have had a go at this using code from other sources but all I have been able to do so far is to copy out the data but am unable to insert it into the other file. Any help would be great!

@echo off

Set "InputFile=C:\*****\Desktop\Dev\Test\Default.csv"
Set "OutPutFile=C:\*****\Desktop\Dev\Test\OutputData.csv"
Set FromLine=68
Set ToLine=107

Call:ExtractLinesFromTextFile "%InputFile%" %FromLine% %ToLine% > 
"%OutPutFile%"

Exit /b

:ExtractLinesFromTextFile <InputFile> <FromLine> <ToLine>
(
echo Wscript.echo(ExtractLinesFromTextFile("%~1",%2,%3^)^)
echo Function ExtractLinesFromTextFile(TextFile,FromLine,ToLine^)
echo If FromLine ^<= ToLine Then
echo With CreateObject("Scripting.FileSystemObject"^).OpenTextFile(TextFile^)
echo Do Until .Line = FromLine Or .AtEndOfStream
echo .SkipLine
echo Loop
echo Do Until .Line ^> ToLine Or .AtEndOfStream
echo ExtractLinesFromTextFile = ExtractLinesFromTextFile ^& (.ReadLine ^& 
vbNewLine^)
echo Loop
echo End With
echo End If
echo End Function
)>"%~n0.vbs"
Cscript /Nologo "%~n0.vbs" "%~1" %~2 %~3
If Exist "%~n0.vbs" Del "%~n0.vbs"
Exit /b

为说明为什么不想批量执行此操作,这是将68至107行复制到VBScript中另一个文件的代码:

To illustrate why you don't want to do this in batch, this is the code to copy lines 68 through 107 to another file in VBScript:

inputFilename  = "C:\path\to\input.csv"
outputFilename = "C:\path\to\output.csv"
fromLine = 68
toLine   = 107

Set fso = CreateObject("Scripting.FileSystemObject")
Set inFile  = fso.OpenTextFile(inputFilename)
Set outFile = fso.OpenTextFile(outputFilename, 2, True)

Do Until inFile.AtEndOfStream Or inFile.Line > toLine
  line = inFile.ReadLine
  If inFile.Line >= fromLine Then outFile.WriteLine line
Loop

inFile.Close
outFile.Close

为说明为什么您也不想在VBScript中执行此操作,这与PowerShell中的操作相同:

To illustrate why you don't want to do this in VBScript either, this is the same operation in PowerShell:

$inputFile  = 'C:\path\to\input.csv'
$outputFile = 'C:\path\to\output.csv'
$fromLine   = 68
$toLine     = 107

$skip       = $fromLine - 1
$numLines   = $toLine - $skip

Get-Content $inputFile | Select-Object -Skip $skip -First $numLines |
    Set-Content $outputFile

可以简化为:

$inputFile  = 'C:\path\to\input.csv'
$outputFile = 'C:\path\to\output.csv'
$skip       = 67
$numLines   = 40

Get-Content $inputFile | Select-Object -Skip $skip -First $numLines |
    Set-Content $outputFile

如果需要,您甚至可以保留CSV标头:

You can even preserve the CSV header if you want:

$inputFile  = 'C:\path\to\input.csv'
$outputFile = 'C:\path\to\output.csv'
$skip       = 66
$numLines   = 40

Import-Csv $inputFile | Select-Object -Skip $skip -First $numLines |
    Export-Csv $outputFile -NoType