Solved Delete characters after specific text

July 15, 2011 at 04:10:15
Specs: Windows XP
I have a txt file and I need help with a batch file which will search for the string "SUPER:" and delete the first 14 characters (numbers and spaces) after "SUPER:". After the deletion, a new file will be created.
Can you help me?

Thanks in advance.

See More: Delete characters after specific text

Report •

July 15, 2011 at 18:20:43
Gee whizz, how about a bit more info?

How many times is SUPER: likely to appear in the file and is it likely to appear more than once in any line?

Is this a strictly .txt file or is it in another format such as .csv?

Will the string commence only at the beginning of a line or at any location within a line?

How about posting a typical short file showing how SUPER: will appear in the file?

Please come back & tell us if your problem is resolved.

Report •

July 18, 2011 at 01:30:18
I'm sorry, please find the details below.

SUPER: will appear 10 to 15 times in the file, once, at the beginning of a line. The file is strictly .txt. The numbers after SUPER: will be always different. I need to get rid of first 15 characters after SUPER:.

SUPER:0212 2245 5678 0002
DATE / TIME :01/01/1900 00:00:00
THIN OWL :12312312 1234

Report •

July 19, 2011 at 01:03:48
✔ Best Answer
Test this. The first 14 chars after SUPER: are omitted from the output file per your original post, if you wish to omit the first 15 chars as in your latest post change !line:~20! to !line:~21!

Good luck.

@echo off>%temp%\newfile.txt
setlocal enabledelayedexpansion

for /f "tokens=*" %%1 in (datafile.txt) do (
    set line=%%1
    if not "!line:~0,6!" equ "SUPER:" (echo %%1>>%temp%\newfile.txt
       ) else (
          echo !line:~0,6!!line:~20!>>%temp%\newfile.txt

type %temp%\newfile.txt

Please come back & tell us if your problem is resolved.

Report •

Related Solutions

July 19, 2011 at 02:22:27
Thank you so much Wahine! The batch is working great!
Changed 20 with 26 as I needed to omit 15 chars after SUPER:. Also deleted %temp%\ as I needed the new file generated in the same directory.

Additionally, if someone needs (as I do) that the output file has the hour and date for name, you can change newfile.txt with file_%time:~0,2%%time:~3,2%%time:~6,2%_%date:~-10,2%%date:~-7,2%%date:~-4,4%.txt.

Thanks once again!

Report •

July 19, 2011 at 14:38:44
You're more than welcome.

Am surprised that you changed 20 to 26 as there are only 25 characters in the Super line you posted but I guess if you are satisfied with the output then it must be working for you.

You will know that extracting the date-time as you have applies only if the date-time formats are the same as on your system. If any other date-time formats are in use (mine are dd/mm/yyyy-hh:mm {24-hour clock}) the extraction you posted may need to be be modified to suit.

Thanks for coming back to report your success.

Please come back & tell us if your problem is resolved.

Report •

July 20, 2011 at 09:03:28
My bad, I had to change SUPER with a longer word... deh, beginner :D

Date and time work for my settings. I will modify for the systems the script will run on if necessary. Thanks for the tip.


Report •

Ask Question