用于删除文本文件中特定行的批处理脚本

时间:2021-12-08 06:00:05

I'm trying to automate some process for a new software we use at work. I have a .zip file that gets downloaded automatically from an FTP every 4 hours. The .zip file is then uncompressed and the .txt file in it is renamed to a .csv using 2 .bat files that I managed to scrap together with my very little knowledge and the resulting .csv is imported in the software. This part works correctly.

我正在尝试为我们在工作中使用的新软件自动化一些过程。我有一个.zip文件,每4小时从FTP自动下载。然后解压缩.zip文件,其中的.txt文件使用2个.bat文件重命名为.csv文件,我设法将这些文件与我的知识一起废弃,并在软件中导入生成的.csv文件。这部分工作正常。

Now the problem is that the .csv has two redundant lines that I need to remove before I can upload it in the software: line 1 and 3. I haven't found an approach from Google searches.

现在的问题是.csv有两条冗余线路需要在我上传软件之前删除:第1行和第3行。我还没有找到谷歌搜索的方法。

Can someone help me with the command that would strip those lines?

有人可以帮助我解决那些会剥夺这些线条的命令吗?

Also, would there be a way to "merge" my multiple .bat together? This is my extract script:

还有,有没有办法将我的多个.bat“合并”在一起?这是我的提取脚本:

@echo off

for /R "C:\FTP Download" %%I in ("*.zip") do (
  "%ProgramFiles(x86)%\7-Zip\7z.exe" x -y -o"%%~dpI" "%%~fI" 
)

and this is my rename script:

这是我的重命名脚本:

@echo off
ren *.txt *.csv

I guess there must be a way to put them in the same .bat but I didn't figure that one out yet.

我想必须有一种方法将它们放在同一个.bat中,但我还没想到它。

2 个解决方案

#1


To remove lines 1 and 3:

要删除第1行和第3行:

Assuming that your input is C:\file.txt,

假设您的输入是C:\ file.txt,

First, lets skip the first line and copy the second line to C:\newfile.txt

首先,让我们跳过第一行并将第二行复制到C:\ newfile.txt

@echo off
for /f "skip=1" %%a in (C:\file.txt) do (
    echo %%a >> C:\newfile.txt   
    goto OutOfLoop
)
:OutOfLoop

Now we can ignore the first three lines of the file completely, and proceed to copy the rest of the wanted text to C:\newfile.txt:

现在我们可以完全忽略文件的前三行,然后将剩余的有用文本复制到C:\ newfile.txt:

for /f "skip=3 delims=*" %%b in (C:\file.txt) do (
    echo %%b >> C:\newfile.txt    
)

This will replace the original file, and remove our temporal copy:

这将替换原始文件,并删除我们的临时副本:

xcopy C:\newfile.txt C:\file.txt /y
del C:\newfile.txt /f /q

NOTE:

  • You need to run this text parser for each of the input files you want to process.
  • 您需要为要处理的每个输入文件运行此文本解析器。

To itarate over the files inside a directory, you can use:

要覆盖目录中的文件,您可以使用:

for /r %%file in (*) do (
    REM Here you can put the code to parse each input file
)
  • Your extraction and renaming code should be able to work correctly in one single batch file:
  • 您的提取和重命名代码应该能够在一个批处理文件中正常工作:

For the renaming to work, first browse to the extracted files' directory.

要使重命名工作,请首先浏览到解压缩文件的目录。

Your final script will look like the following:

您的最终脚本将如下所示:

@echo off

REM Browse to the desired directory
cd C:\FTP Download\

REM Extract all zip files
for /r %%I in (*.zip) do (
  "%ProgramFiles(x86)%\7-Zip\7z.exe" x -y -o"%%~dpI" "%%~fI" 
)

REM Rename all extracted .txt files to .csv
ren *.txt *.csv

REM For each .csv file
for /r %%file in (*.csv) do (

    REM Copy second line to C:\newfile.txt
    for /f "skip=1" %%a in (%%file) do (
        echo %%a >> C:\newfile.txt
        goto OutOfLoop
    )
    :OutOfLoop

    REM Copy fourth to last line to C:\newfile.txt
    for /f "skip=3 delims=*" %%b in (%%file) do (
        echo %%b >> C:\newfile.txt    
    )

    REM replace original file and remove temporary files
    xcopy C:\newfile.txt %%file /y
    del C:\newfile.txt /f /q
)

#2


Start with next one-line command from command line:

从命令行开始下一个单行命令:

for /F "tokens=1* delims=:" %G in ('findstr /N /R "^" xxxx.txt') do @if not "%G"=="1" @if not "%G"=="3" @echo %G %H

In a .bat script:

在.bat脚本中:

for /F "tokens=1* delims=:" %%G in (
   'findstr /N /R "^" xxxx.txt'
) do if not "%%G"=="1" if not "%%G"=="3" echo %%G %%H

No sooner than debugged, replace it with next code snippet:

在调试之后,将其替换为下一个代码段:

type nul>xxxx.csv
>>xxxx.csv (for /F "tokens=1* delims=:" %%G in (
   'findstr /N /R "^" xxxx.txt'
) do if not "%%G"=="1" if not "%%G"=="3" echo(%%H)

Update: merge two code snippets to one script (commented code):

更新:将两个代码段合并到一个脚本(注释代码):

@ECHO OFF >NUL
SETLOCAL enableextensions

rem to main working directory
pushd "C:\FTP Download"

for /R %%I in ("*.zip") do (
  "%ProgramFiles(x86)%\7-Zip\7z.exe" x -y -o"%%~dpI" "%%~fI" 

  rem need change working directory due to recursion in `for /R %%I ...`  
  pushd "%%~dpI"
  for /F "delims=" %%X in ('dir /B *.txt') do (

    type nul>"%%~nX.csv"
    >>"%%~nX.csv" (for /F "tokens=1* delims=:" %%G in (
       'findstr /N /R "^" "%%~X"'
    ) do if not "%%G"=="1" if not "%%G"=="3" echo(%%H)

    rem delete processed text file 
    del "%%~X"
  )
  rem back to main working directory
  popd
  rem archive processed zip file
  move "%%~fI" "%tmp%\done%%~nxI"
)
popd

Resources (required reading):

资源(必读):

#1


To remove lines 1 and 3:

要删除第1行和第3行:

Assuming that your input is C:\file.txt,

假设您的输入是C:\ file.txt,

First, lets skip the first line and copy the second line to C:\newfile.txt

首先,让我们跳过第一行并将第二行复制到C:\ newfile.txt

@echo off
for /f "skip=1" %%a in (C:\file.txt) do (
    echo %%a >> C:\newfile.txt   
    goto OutOfLoop
)
:OutOfLoop

Now we can ignore the first three lines of the file completely, and proceed to copy the rest of the wanted text to C:\newfile.txt:

现在我们可以完全忽略文件的前三行,然后将剩余的有用文本复制到C:\ newfile.txt:

for /f "skip=3 delims=*" %%b in (C:\file.txt) do (
    echo %%b >> C:\newfile.txt    
)

This will replace the original file, and remove our temporal copy:

这将替换原始文件,并删除我们的临时副本:

xcopy C:\newfile.txt C:\file.txt /y
del C:\newfile.txt /f /q

NOTE:

  • You need to run this text parser for each of the input files you want to process.
  • 您需要为要处理的每个输入文件运行此文本解析器。

To itarate over the files inside a directory, you can use:

要覆盖目录中的文件,您可以使用:

for /r %%file in (*) do (
    REM Here you can put the code to parse each input file
)
  • Your extraction and renaming code should be able to work correctly in one single batch file:
  • 您的提取和重命名代码应该能够在一个批处理文件中正常工作:

For the renaming to work, first browse to the extracted files' directory.

要使重命名工作,请首先浏览到解压缩文件的目录。

Your final script will look like the following:

您的最终脚本将如下所示:

@echo off

REM Browse to the desired directory
cd C:\FTP Download\

REM Extract all zip files
for /r %%I in (*.zip) do (
  "%ProgramFiles(x86)%\7-Zip\7z.exe" x -y -o"%%~dpI" "%%~fI" 
)

REM Rename all extracted .txt files to .csv
ren *.txt *.csv

REM For each .csv file
for /r %%file in (*.csv) do (

    REM Copy second line to C:\newfile.txt
    for /f "skip=1" %%a in (%%file) do (
        echo %%a >> C:\newfile.txt
        goto OutOfLoop
    )
    :OutOfLoop

    REM Copy fourth to last line to C:\newfile.txt
    for /f "skip=3 delims=*" %%b in (%%file) do (
        echo %%b >> C:\newfile.txt    
    )

    REM replace original file and remove temporary files
    xcopy C:\newfile.txt %%file /y
    del C:\newfile.txt /f /q
)

#2


Start with next one-line command from command line:

从命令行开始下一个单行命令:

for /F "tokens=1* delims=:" %G in ('findstr /N /R "^" xxxx.txt') do @if not "%G"=="1" @if not "%G"=="3" @echo %G %H

In a .bat script:

在.bat脚本中:

for /F "tokens=1* delims=:" %%G in (
   'findstr /N /R "^" xxxx.txt'
) do if not "%%G"=="1" if not "%%G"=="3" echo %%G %%H

No sooner than debugged, replace it with next code snippet:

在调试之后,将其替换为下一个代码段:

type nul>xxxx.csv
>>xxxx.csv (for /F "tokens=1* delims=:" %%G in (
   'findstr /N /R "^" xxxx.txt'
) do if not "%%G"=="1" if not "%%G"=="3" echo(%%H)

Update: merge two code snippets to one script (commented code):

更新:将两个代码段合并到一个脚本(注释代码):

@ECHO OFF >NUL
SETLOCAL enableextensions

rem to main working directory
pushd "C:\FTP Download"

for /R %%I in ("*.zip") do (
  "%ProgramFiles(x86)%\7-Zip\7z.exe" x -y -o"%%~dpI" "%%~fI" 

  rem need change working directory due to recursion in `for /R %%I ...`  
  pushd "%%~dpI"
  for /F "delims=" %%X in ('dir /B *.txt') do (

    type nul>"%%~nX.csv"
    >>"%%~nX.csv" (for /F "tokens=1* delims=:" %%G in (
       'findstr /N /R "^" "%%~X"'
    ) do if not "%%G"=="1" if not "%%G"=="3" echo(%%H)

    rem delete processed text file 
    del "%%~X"
  )
  rem back to main working directory
  popd
  rem archive processed zip file
  move "%%~fI" "%tmp%\done%%~nxI"
)
popd

Resources (required reading):

资源(必读):