David Fronapfel
David Fronapfel

Reputation: 11

CMD line read text file and remove characters

As part of another batch script, I am generating text files where each has a list of numbers, and need to normalize the number of characters. They have leading zeros, but show as follows:

008

009

0010 etc.

How do I read all lines of the text file into the command line and output to a file with all of them 3 characters (by stripping off the leading character if needed)? So far I have this, but it's not outputting the second text file:

(FOR /F "tokens=*" %%Z IN (C:\Temp\NumberList.txt) DO @ECHO %%Z)
FOR /L %%Z in (1,1,%numFiles%) DO SET Z=%Z:~-3%) > C:\Temp\NumberList2.txt

Upvotes: 1

Views: 6341

Answers (1)

Endoro
Endoro

Reputation: 37569

Try this:

@echo off
setlocal enabledelayedexpansion
for /f %%i in (C:\Temp\NumberList.txt) do (
    set "var=%%i"
    set "var=!var:~-3!"
    >>"C:\Temp\NumberList2.txt" echo.!var!
)
endlocal

Upvotes: 1

Related Questions