trim off some characters of lines in txt file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
goodywp
Posts: 250
Joined: 31 Jul 2017 09:57

trim off some characters of lines in txt file

#1 Post by goodywp » 01 Aug 2017 09:49

Hi all forks,

So glad to be in this group and got some replies from you regarding this topic
"have a list of folder containing one file for each folder, need to find this file path in another folder/subfolder"

Now I had a solution for it but I need to trim off all the ==> for each line in one txt file as below:

==> 500007011000.S3S
==> 500011011000.S3S
==> 500016011000.S3S
==> 500072010400.S3S
==> 500136010100.S3S
==> 500022010300.S3S
==> 500007011000.S3S
==> 500008011000.S3S
==> 500019011200.S3S
==> 500008011000.S3S
==> 500009011000.S3S
==> 500012011000.S3S
==> 500014011000.S3S
==> 500028011201.S3S
==> 500135010100.S3S
==> 500143010002.S3S
==> 500142010100.S3S
==> 500144010100.S3S
==> 500006011000.S3S
==> 500007011000.S3S
==> 500008011000.S3S
==> 500009011000.S3S
==> 500010011000.S3S
==> 500011011000.S3S
==> 500012011000.S3S
==> 500014011000.S3S
==> 500016011000.S3S
==> 500134010100.S3S
==> 500028011201.S3S
==> 500129010200.S3S
==> 500072010400.S3S
==> 500135010100.S3S
==> 500143010002.S3S

I need to be as this in new txt file

500007011000.S3S
500011011000.S3S
500016011000.S3S
500072010400.S3S
500136010100.S3S
500022010300.S3S
500007011000.S3S
500008011000.S3S
500019011200.S3S
500008011000.S3S
500009011000.S3S
500012011000.S3S
500014011000.S3S
500028011201.S3S
500135010100.S3S
500143010002.S3S
500142010100.S3S
500144010100.S3S
500006011000.S3S
500007011000.S3S
500008011000.S3S
500009011000.S3S
500010011000.S3S
500011011000.S3S
500012011000.S3S
500014011000.S3S
500016011000.S3S
500134010100.S3S
500028011201.S3S
500129010200.S3S
500072010400.S3S
500135010100.S3S
500143010002.S3S

Thanks so much again for all your kind replies

William

goodywp
Posts: 250
Joined: 31 Jul 2017 09:57

Re: trim off some characters of lines in txt file

#2 Post by goodywp » 01 Aug 2017 12:53

Problem solved ! instead of using % I used ! it works! Could anyone tell me why ?

Code: Select all

@echo off 

setlocal enabledelayedexpansion

for /f "delims=" %%i in (sch_repl_list_after.txt) do (

    set line=%%i 

    echo !line:~6, 16! >>removed_str.txt
    
)

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

Re: trim off some characters of lines in txt file

#3 Post by penpen » 01 Aug 2017 14:16

The command line interpreter expands percentage variables (%var%) before executing a command.
So a block ("( command+ )") is executed after the percentage variable was expanded.
Setting the variables value won't affect the result of the earlier percentage expansion.
The exclamation mark variables are expanded right before an atomic command is executed (delayed expansion), so it could expand to the right value, set with a previous atomic command.


penpen

goodywp
Posts: 250
Joined: 31 Jul 2017 09:57

Re: trim off some characters of lines in txt file

#4 Post by goodywp » 02 Aug 2017 08:39

Thanks Penpen! How to make the above code works by using % if possible

elzooilogico
Posts: 128
Joined: 23 May 2016 15:39
Location: Spain

Re: trim off some characters of lines in txt file

#5 Post by elzooilogico » 02 Aug 2017 09:24

Code: Select all

echo !line:~6,16!

can be

Code: Select all

call echo %%line:~6:16%%

note double percent sign to force call evaluate the term inside. but performance will be worst than using delayed expansion (think about a large file to process)

also, if performance is in your mind

Code: Select all

for ... do (
  echo whatever>>somefile.txt
)

will open and close the file every time, where

Code: Select all

>somefile.txt (
  for ... (
      echo whatever
   )
)
will open the file before the block, and close it after, so only one open/close. this may improve performance a lot.

goodywp
Posts: 250
Joined: 31 Jul 2017 09:57

Re: trim off some characters of lines in txt file

#6 Post by goodywp » 03 Aug 2017 08:11

Thanks so much elzooilogicofor your detailed explanation with sample. that helps us a lot in terms of why that works that way... :D

Post Reply