Remove last 4 character from each line of a text file

July 8, 2016 at 00:52:31
Specs: Windows 7
Hi,

I have to remove last 4 characters from each line of a text file.

So i created the Batch script referring to below solution
http://www.computing.net/answers/pr...
But this script is not solving my requirement.

Script used:

@echo off & setlocal EnableDelayedExpansion > "fileOUT.txt"
for /F "delims=" %%j in ('type "fileIN.txt"') do (
set line=%%j
echo line data:%%j
echo.!line:~0,-4!
)
:: End_OF_Batch


Source filename taken as:FileIN.txt
Data as :
Tom1234
Dan2345

expecting to get :
Tom
Dan

But above script resulting me
line:~0,-4
line:~0,-4

Please guide me on this.

message edited by Sunaj


See More: Remove last 4 character from each line of a text file

Report •


#1
July 8, 2016 at 03:34:34
@echo off & setlocal EnableDelayedExpansion > "fileOUT.txt"
for /F "delims=" %%j in ('type "fileIN.txt"') do (
set line=%%j
echo line data:%%j
set new=!line:~0,-4!
echo.!new!
)
:: End_OF_Batch

=====================

M2 Get custom script or take private lessons


Report •

#2
July 8, 2016 at 03:37:46
Seems like it could be done without that extra step.

Maybe IVO will show us how.
:(

=====================

M2 Get custom script or take private lessons


Report •

#3
July 8, 2016 at 07:01:48
IVO's is pretty much refined as much as possible. The only other change I could suggest would be to use "usebackq delims=" %%j in ("FileIN.txt").

Anyways, the problem here is a bug in CMD command's parser. If your input doesn't have any blank lines, you can get away with the following instead:

echo !line:~0,-4!

How To Ask Questions The Smart Way


Report •

Related Solutions

#4
November 9, 2016 at 02:38:47
@echo off
setlocal EnableDelayedExpansion
set count=0
::this is the number of words in the file you want to process
set magicnumber=2
:firstloop
for /F "tokens=*" %%a in (fileIN.txt) do set var!count!=%%a&goto skipfirstloop
:skipfirstloop
echo !var%count%:~0,-4!
:allotherloop
set /a count=!count!+1
if '!count!'=='!magicnumber!' echo DONE!&pause>nul&exit /b
for /F "skip=%count% tokens=*" %%a in (fileIN.txt) do set var!count!=%%a&goto skipallotherloop
:skipallotherloop
echo !var%count%:~0,-4!
goto allotherloop
::you can change
::echo !var%count%:~0,-4!
::to
::echo !var%count%:~0,-4!>>fileOUT.txt
::result is a file called fileOUT.txt with data:
::Tom
::Dan

message edited by hidde663


Report •

Ask Question