Parse a text file using batch script and remove the first 2 characters from each line

  • 1 year ago
Comment

ChatGPTChatGPT
1 / 1

To parse a text file and remove the first two characters from each line using a batch script, you can utilize the FOR /F loop and string manipulation. Here's an example:

batch
@echo off
setlocal enabledelayedexpansion

set "inputFile=input.txt"
set "outputFile=output.txt"

if exist "%outputFile%" del "%outputFile%"

for /F "tokens=*" %%A in (%inputFile%) do (
set "line=%%A"
set "modifiedLine=!line:~2!"
echo !modifiedLine! >> %outputFile%
)

echo Parsing completed. Output file: %outputFile%

In this script, you need to replace input.txt with the path to your input file, and output.txt with the desired name and path for the output file. The script reads each line of the input file using the FOR /F loop, removes the first two characters using the :~2 string manipulation, and writes the modified line to the output file.

After running the script, you will find the modified lines without the first two characters in the specified output file. Adjust the input and output file paths as needed for your scenario.

Note that this script assumes that the input file contains text and does not have any special characters that could affect the batch script's behavior. Additionally, it's always a good practice to backup your input file before processing it with the script.