1.

Solve : How to remove part of a string within a text file?

Answer»

Hi
I have a text FILE with many thousands of LINES in the following format:

crescent.mp3
blackbird.mp3
jay.mp3

I want a dos script to go through the text file LINE by line and remove the '.mp3' part from the end of each NAME. Any ideas??the mp3 PORTION is the file extension...if you remove it they will cease to plat anymore...Quote from: geroido on November 03, 2012, 10:04:13 AM

Hi
I have a text file with many thousands of lines in the following format:

crescent.mp3
blackbird.mp3
jay.mp3

I want a dos script to go through the text file line by line and remove the '.mp3' part from the end of each name. Any ideas??

If you want a new text file with line as in your text file, but with each line having the final 4 characters (.mp3) removed,

a script like this should work

@echo off
setlocal enabledelayedexpansion
set inputfile=birds1.txt
set outputfile=birds-no-extensions.txt
if exist "%outputfile%" del "%outputfile%"
for /f "delims=" %%A in (' type "%inputfile%" ' ) do (
set oldline=%%A
set newline=!oldline:~-0,-5!
echo Old: !oldline! New: !newline!
echo !newline! >> "%outputfile%"
)
echo done
pause

birds1.txt

Golden-bellied Euphonia.mp3
Golden-headed Manakin.mp3
Golden-spangled Piculet.mp3
Golden Collard Macaw.mp3
Golden Parakeet.mp3
Golden-winged Parakeet.mp3
Grassland Sparrow.mp3
Gray-*censored* Sabrewing.mp3
Gray-chested Greenlet.mp3
Gray-*censored* Martin.mp3
Gray-crowned Flycatcher.mp3
Gray-fronted Dove.mp3
Gray Monjita.mp3
Gray Hawk.mp3
Gray Antbird.mp3
Gray Antwren.mp3
Gray Rumped Swift.mp3
Gray-winged Trumpeter.mp3
Gray-headed Kite.mp3
Gray-headed Tanager.mp3
Gray-necked Wood-Rail.mp3
Grayish Mourner.mp3
Grayish Saltator.mp3
Great Jacamar.mp3
Great Kiskadee.mp3
Great Horned Owl.mp3
Great Tinamou.mp3
Great Rhea.mp3
Great Dusky Swift.mp3
Great Egret.mp3
Great Black Hawk.mp3
Great Antshrike.mp3
Greater Ani.mp3
Greater Thornbird.mp3
Greater Yellow-headed Vulture.mp3
Green-tailed Jacamar.mp3

birds-no-extensions.txt

Golden-bellied Euphonia
Golden-headed Manakin
Golden-spangled Piculet
Golden Collard Macaw
Golden Parakeet
Golden-winged Parakeet
Grassland Sparrow
Gray-*censored* Sabrewing
Gray-chested Greenlet
Gray-*censored* Martin
Gray-crowned Flycatcher
Gray-fronted Dove
Gray Monjita
Gray Hawk
Gray Antbird
Gray Antwren
Gray Rumped Swift
Gray-winged Trumpeter
Gray-headed Kite
Gray-headed Tanager
Gray-necked Wood-Rail
Grayish Mourner
Grayish Saltator
Great Jacamar
Great Kiskadee
Great Horned Owl
Great Tinamou
Great Rhea
Great Dusky Swift
Great Egret
Great Black Hawk
Great Antshrike
Greater Ani
Greater Thornbird
Greater Yellow-headed Vulture
Green-tailed Jacamar


Thanks for the help. Works greatCode: [Select]@echo off
set inputfile=birds1.txt
set outputfile=birds-no-extensions.txt
if exist "%outputfile%" del "%outputfile%"
for /f "delims=" %%A in (' type "%inputfile%" ' ) do (
echo %%~nA>>"%outputfile%"
)That's a more compact alternative, and the FOR part could be all on one line

@echo off
set inputfile=birds1.txt
set outputfile=birds-no-extensions.txt
if exist "%outputfile%" del "%outputfile%"
for /f "delims=" %%A in (' type "%inputfile%" ' ) do echo %%~nA>>"%outputfile%"


.


Discussion

No Comment Found