How do I remove everything after the first word?

2

I would like to remove everything after the first word in each line of this text document. It works for simple character removal, but does not work when I try to remove the "|" of the text file.

I'mtryingtousethefollowingcode:

setlocalenableextensionsdisabledelayedexpansionset/ptxtfile=TextFileName:Echo.set/p"search=Search for: "
    Echo.
set /p "replace=Replace to: "

    for /f "delims=" %%i in ('type "%txtfile%.txt" ^& break ^> "%txtfile%.txt" ') do (
        set "line=%%i"
        setlocal enabledelayedexpansion
        >>"%txtfile%.tmp" echo(!line:%search%=%replace%!
        endlocal
    )

    ren "%txtfile%".tmp "%txtfile%".txt 

pause

Error:

    
asked by anonymous 29.09.2017 / 00:32

1 answer

0

Unedited is only for files with the same layout as your example, where we have pipe as a delimiter, for another delimiter, edit the % and special characters , I imagine you already know, but post to the other visitors, if they do not know, add the set _delimiter before the character, for example: ^ , ^| , ^& , etc.

@echo off && setlocal enableextensions enabledelayedexpansion && color 0a

set /p "'_delimiter=^|"<nul

echo. && set /p "txtfile=Text File Name: "

copy "%txtfile%" "%temp%\%txtfile%.tmp" /y

type nul >"%temp%\%txtfile%_crop.txt"

for /f "tokens=1 delims=%_delimiter%" %%i in ('type "%temp%\%txtfile%.tmp"') do (

   set "_line=%%~i"
   echo/!_line!>>"%temp%\%txtfile%_crop.txt"

)

move /y "%temp%\%txtfile%_crop.txt" "%txtfile%"

del /q /f  "%temp%\%txtfile%.tmp"

pause
    
26.11.2018 / 05:54