Delete substring selected by position
Posted: 20 Mar 2016 15:59
NOTE: I edit my last post.
Finally my problem is just solved !
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':
Thanks for your patience
Finally my problem is just solved !

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
