Delete substring selected by position

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
basalt
Posts: 2
Joined: 20 Mar 2016 14:00
Location: Barcelona

Delete substring selected by position

#1 Post by basalt » 20 Mar 2016 15:59

NOTE: I edit my last post.

Finally my problem is just solved ! :D

Explanation:

This subrutine (or script) is able to delete any item you search in any line of a file, only you need to know its position in this line. F.i.:

File: varlog.txt
Line 1: bla bla bla
Line 2: bla bla
Line 3: Computers 234 units sold
Line 4: bla

If you considerer to delete "234" from line 3 replacing with a space, you need to search the position 1 (0=Computers, 1=234, etc.)

To do it, you'll need something like this batch file named 'engine.bat':

Code: Select all

@ECHO OFF
TITLE Engine to Store & Manage Variables and Values

:Main
SET "fich=varlog.txt"     ::File in same folder
SET "var=myvar"          ::Variable to search in Fich
SET "pos=1"                 ::Position of element to delete

CALL :Delete_Item_By_Pos %Fich% %Var% %Pos%
GOTO :EOF

:Delete_Item_By_Pos
REM First, looks the line for the var (pos=0)
SETLOCAL EnableDelayedExpansion
FOR /F "tokens=*" %%i IN ('"FINDSTR %var% %fich%"') DO (
SET line=%%i
)

REM Search the item that you wants to delete after
SET counter=0
FOR %%j IN (%line%) DO (
SET old_dat=%%j
IF "!pos!" EQU "!counter!" GOTO :Deleting
SET /A counter=%counter%+1
)

REM Below: Let's cut that item from the line
REM and after we will restore varlog.txt

:Deleting
SET after=!line:%old_dat%=!
FINDSTR /v "!line!" < !fich! > tmp.txt
ECHO !after! >> tmp.txt
COPY /Y tmp.txt !fich!
ENDLOCAL
GOTO :EOF


Thanks for your patience :roll:
Last edited by basalt on 21 Mar 2016 10:29, edited 1 time in total.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Delete substring selected by position

#2 Post by penpen » 21 Mar 2016 09:55

On a first look it seems that delayed expansion may help you:

Code: Select all

@ECHO OFF
TITLE Engine to Store & Manage Variables and Values

:Main
SET "Fich=varlog.txt"  ::File in same folder
SET "Var=myvar"         ::Variable to search in Fich
SET "Pos=1"                 ::Position of element to delete

CALL :Delete_Item_By_Pos %Fich% %Var% %Pos%
GOTO :EOF

:Delete_Item_By_Pos
setlocal enableDelayedExpansion
SET /P Line= <FIND "%Var%" <%Fich%
SET "Counter=0"
FOR  %%j IN (%Line%) DO (   
      SET /A "Counter+=1"
         IF "%Pos%" EQU "%Counter%" (SET Matched_Data=%%j)
)

REM Delete from Line the matched item found:
 
SET Line=!Line:%Matched_Data%=!
ECHO Now the line contains: %Line%
endlocal
GOTO :EOF
Note that the success of this string operation depends on the content of "Matched_Data"
(for example 'set "Matched_Data=*="' is problematic).


penpen

basalt
Posts: 2
Joined: 20 Mar 2016 14:00
Location: Barcelona

Re: Delete substring selected by position

#3 Post by basalt » 21 Mar 2016 10:35

Yes, absolutely penpen!!. Was necessary to apply SETLOCALS. Also, it was bad writed, etc. Since the last night i did this post success a lot of things.

Finally you'll see soon here the final code. Thanks by your interest

Post Reply