Sign in to follow this  
Followers 0
sonicxtacy02

how do you delete a substring from a string?

12 posts in this topic

Is this possible... i tried:

if $check= "2" then

StringReplace($testlist2,$file,"")

but the string doesnt get replaced. I understand why after reading the docs, but is there any other way to remove a substring from a string? The string is a .m3u playlist file, the substring is a music file listed in the playlist.

Share this post


Link to post
Share on other sites



Is this possible... i tried:

if $check= "2" then

StringReplace($testlist2,$file,"")

but the string doesnt get replaced. I understand why after reading the docs, but is there any other way to remove a substring from a string? The string is a .m3u playlist file, the substring is a music file listed in the playlist.

$testlist2 = StringReplace($testlist2,$file,"")

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

You could consider to save the result of StringReplace() to display it

$string = 'MyGreatString'
$result = StringReplace($string, 'Great', 'Replaced')
MsgBox(0, '', $result)

Works fine for me.

:P

Edit:

Fixed typo

Edit:

As gafrost has, use an empty 3rd parameter to clear the substring found.

Edited by MHz

Share this post


Link to post
Share on other sites

You could consider to save the result of StringReplace() to display it

$string = 'MyGreatString'
$result = StringReplace($string, 'Great', 'Replaced')
MsgBox(0, '', $result)

Works fine for me.

:P

Edit:

Fixed typo

Edit:

As gafrost has, use an empty 3rd parameter to clear the substring found.

yes i thought i'd be that simple but its not working for me. I'm gonna go ahead and post the entire script.

;-- set up global varibles
Global $rr_ini_file = "C:\Program Files\Road Runner\rr.ini"
Global $winamppath, $filepathread, $musicpath, $playlistpath
;-- func call
Get_Paths()

If $playlistpath= "0" Then
    $filepath = $musicpath
Else
    $filepath = $filepathread
ENDif

$file = FileReadLine($winamppath&"playing.txt",1)
$testlist1=fileread($filepath&"1 stars.m3u")
$testlist2=fileread($filepath&"2 stars.m3u")
$testlist3=fileread($filepath&"3 stars.m3u")
$testlist4=fileread($filepath&"4 stars.m3u")
$testlist5=fileread($filepath&"5 stars.m3u")



Select
    Case $CmdLineRaw = 1
    ;-- 1 star
        filedelete("C:\program files\Road Runner\Ratings\ratings.txt")
        $check= iniread("C:\program files\Road Runner\Ratings\ratingsconfig.ini","ratings","rated","notfound")
        if $check= "2" then
            StringReplace($testlist2,$file,"")
        elseif $check="3" Then
            StringReplace($testlist3,$file,"")
        Elseif $check="4" Then
            stringreplace($testlist4,$file,"")
        Elseif $check="5" Then
            stringreplace($testlist5,$file,"")
        EndIf
        FileWriteLine("C:\program files\Road runner\Ratings\ratings.txt","1")
    Case $CmdLineRaw = 2
    ;-- 2 stars
        filedelete("C:\program files\Road Runner\Ratings\ratings.txt")
        $check= iniread("C:\program files\Road Runner\Ratings\ratingsconfig.ini","ratings","rated","notfound")
        if $check= "1" then
            StringReplace($testlist1,$file,"")
        elseif $check="3" Then
            StringReplace($testlist3,$file,"")
        Elseif $check="4" Then
            stringreplace($testlist4,$file,"")
        Elseif $check="5" Then
            stringreplace($testlist5,$file,"")
        EndIf
        FileWriteLine("C:\program files\Road runner\Ratings\ratings.txt","2")
    Case $CmdLineRaw = 3
    ;-- 3 stars
        filedelete("C:\program files\Road Runner\Ratings\ratings.txt")
        $check= iniread("C:\program files\Road Runner\Ratings\ratingsconfig.ini","ratings","rated","notfound")
        if $check= "1" then
            StringReplace($testlist1,$file,"")
        elseif $check="2" Then
            StringReplace($testlist2,$file,"")
        Elseif $check="4" Then
            stringreplace($testlist4,$file,"")
        Elseif $check="5" Then
            stringreplace($testlist5,$file,"")
        EndIf
        FileWriteLine("C:\program files\Road runner\Ratings\ratings.txt","3")
    Case $CmdLineRaw = 4
    ;-- 4 stars
        filedelete("C:\program files\Road Runner\Ratings\ratings.txt")
        $check= iniread("C:\program files\Road Runner\Ratings\ratingsconfig.ini","ratings","rated","notfound")
        if $check= "1" then
            StringReplace($testlist1,$file,"")
        elseif $check="2" Then
            StringReplace($testlist2,$file,"")
        Elseif $check="3" Then
            stringreplace($testlist3,$file,"")
        Elseif $check="5" Then
            stringreplace($testlist5,$file,"")
        EndIf
        FileWriteLine("C:\program files\Road runner\Ratings\ratings.txt","4") 
    Case $CmdLineRaw = 5
    ;-- 5 stars
        filedelete("C:\program files\Road Runner\Ratings\ratings.txt")
        $check= iniread("C:\program files\Road Runner\Ratings\ratingsconfig.ini","ratings","rated","notfound")
        if $check= "1" then
            StringReplace($testlist1,$file,"")
        elseif $check="2" Then
            StringReplace($testlist2,$file,"")
        Elseif $check="3" Then
            stringreplace($testlist3,$file,"")
        Elseif $check="4" Then
            stringreplace($testlist4,$file,"")
        EndIf
        FileWriteLine("C:\program files\Road runner\Ratings\ratings.txt","5")
EndSelect


Func Get_Paths();blue zx3's addition
;==================================================================
;=== get playlistpath form rr.ini
;==================================================================
    $num_entery = _FileCountLines($rr_ini_file)+1
    $ln=1
    Do
        $var = FileReadLine($rr_ini_file,$ln)
        $ln = $ln + 1   
        If StringInStr($var,"winamppath=") Then $winamppath = StringMid($var,12)
        If StringInStr($var,"playlistpath=") Then $filepathread = StringMid($var,14)
        If StringInStr($var,"musicpath=") Then $musicpath = StringMid($var,11)
    Until $ln = $num_entery 
    if $filepathread = "" Then $playlistpath = "0"  
EndFunc

Func _FileCountLines($sFilePath)
    Local $N = FileGetSize($sFilePath) - 1
    If @error Or $N = -1 Then Return 0
    Return StringLen(StringAddCR(FileRead($sFilePath, $N))) - $N + 1
EndFunc  ;==>_FileCountLines

Share this post


Link to post
Share on other sites

yes i thought i'd be that simple but its not working for me. I'm gonna go ahead and post the entire script.

CODE

;-- set up global varibles

Global $rr_ini_file = "C:\Program Files\Road Runner\rr.ini"

Global $winamppath, $filepathread, $musicpath, $playlistpath

;-- func call

Get_Paths()

If $playlistpath= "0" Then

$filepath = $musicpath

Else

$filepath = $filepathread

ENDif

$file = FileReadLine($winamppath&"playing.txt",1)

$testlist1=fileread($filepath&"1 stars.m3u")

$testlist2=fileread($filepath&"2 stars.m3u")

$testlist3=fileread($filepath&"3 stars.m3u")

$testlist4=fileread($filepath&"4 stars.m3u")

$testlist5=fileread($filepath&"5 stars.m3u")

Select

Case $CmdLineRaw = 1

;-- 1 star

filedelete("C:\program files\Road Runner\Ratings\ratings.txt")

$check= iniread("C:\program files\Road Runner\Ratings\ratingsconfig.ini","ratings","rated","notfound")

if $check= "2" then

StringReplace($testlist2,$file,"")

elseif $check="3" Then

StringReplace($testlist3,$file,"")

Elseif $check="4" Then

stringreplace($testlist4,$file,"")

Elseif $check="5" Then

stringreplace($testlist5,$file,"")

EndIf

FileWriteLine("C:\program files\Road runner\Ratings\ratings.txt","1")

Case $CmdLineRaw = 2

;-- 2 stars

filedelete("C:\program files\Road Runner\Ratings\ratings.txt")

$check= iniread("C:\program files\Road Runner\Ratings\ratingsconfig.ini","ratings","rated","notfound")

if $check= "1" then

StringReplace($testlist1,$file,"")

elseif $check="3" Then

StringReplace($testlist3,$file,"")

Elseif $check="4" Then

stringreplace($testlist4,$file,"")

Elseif $check="5" Then

stringreplace($testlist5,$file,"")

EndIf

FileWriteLine("C:\program files\Road runner\Ratings\ratings.txt","2")

Case $CmdLineRaw = 3

;-- 3 stars

filedelete("C:\program files\Road Runner\Ratings\ratings.txt")

$check= iniread("C:\program files\Road Runner\Ratings\ratingsconfig.ini","ratings","rated","notfound")

if $check= "1" then

StringReplace($testlist1,$file,"")

elseif $check="2" Then

StringReplace($testlist2,$file,"")

Elseif $check="4" Then

stringreplace($testlist4,$file,"")

Elseif $check="5" Then

stringreplace($testlist5,$file,"")

EndIf

FileWriteLine("C:\program files\Road runner\Ratings\ratings.txt","3")

Case $CmdLineRaw = 4

;-- 4 stars

filedelete("C:\program files\Road Runner\Ratings\ratings.txt")

$check= iniread("C:\program files\Road Runner\Ratings\ratingsconfig.ini","ratings","rated","notfound")

if $check= "1" then

StringReplace($testlist1,$file,"")

elseif $check="2" Then

StringReplace($testlist2,$file,"")

Elseif $check="3" Then

stringreplace($testlist3,$file,"")

Elseif $check="5" Then

stringreplace($testlist5,$file,"")

EndIf

FileWriteLine("C:\program files\Road runner\Ratings\ratings.txt","4")

Case $CmdLineRaw = 5

;-- 5 stars

filedelete("C:\program files\Road Runner\Ratings\ratings.txt")

$check= iniread("C:\program files\Road Runner\Ratings\ratingsconfig.ini","ratings","rated","notfound")

if $check= "1" then

StringReplace($testlist1,$file,"")

elseif $check="2" Then

StringReplace($testlist2,$file,"")

Elseif $check="3" Then

stringreplace($testlist3,$file,"")

Elseif $check="4" Then

stringreplace($testlist4,$file,"")

EndIf

FileWriteLine("C:\program files\Road runner\Ratings\ratings.txt","5")

EndSelect

Func Get_Paths();blue zx3's addition

;==================================================================

;=== get playlistpath form rr.ini

;==================================================================

$num_entery = _FileCountLines($rr_ini_file)+1

$ln=1

Do

$var = FileReadLine($rr_ini_file,$ln)

$ln = $ln + 1

If StringInStr($var,"winamppath=") Then $winamppath = StringMid($var,12)

If StringInStr($var,"playlistpath=") Then $filepathread = StringMid($var,14)

If StringInStr($var,"musicpath=") Then $musicpath = StringMid($var,11)

Until $ln = $num_entery

if $filepathread = "" Then $playlistpath = "0"

EndFunc

Func _FileCountLines($sFilePath)

Local $N = FileGetSize($sFilePath) - 1

If @error Or $N = -1 Then Return 0

Return StringLen(StringAddCR(FileRead($sFilePath, $N))) - $N + 1

EndFunc ;==>_FileCountLines

Try your Case's this way:

Select
    Case $CmdLineRaw = 1
    ;-- 1 star
        filedelete("C:\program files\Road Runner\Ratings\ratings.txt")
        $check= iniread("C:\program files\Road Runner\Ratings\ratingsconfig.ini","ratings","rated","notfound")
        if $check= "2" then
            $testlist2 = StringReplace($testlist2,$file,"")
        elseif $check="3" Then
            $testlist3 = StringReplace($testlist3,$file,"")
        Elseif $check="4" Then
            $testlist4 = stringreplace($testlist4,$file,"")
        Elseif $check="5" Then
            $testlist5 = stringreplace($testlist5,$file,"")
        EndIf
        FileWriteLine("C:\program files\Road runner\Ratings\ratings.txt","1")

The StringReplace() function just returns the modified string, you weren't doing anything with it.

:P


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Could be an misunderstanding here, but when is $CMDLINERAW just a single integer?

AFAIK, $CMDLINERAW passes back all that was passed to the interpreter. So are you saying this is a compiled script when testing?

I cannot see no difference between each of the ini reads either between each conditional $CMDLINERAW selection? The FileWriteLine varies.

Perhaps a logic issue, but hard to say with what I see and little I know of the purpose of the script(I know of music...).

Edit:

Sorry, cannot see the forrest for the trees. PsaltyDS saw it.

Edited by MHz

Share this post


Link to post
Share on other sites

Your problem is that you aren't paying attention. StringReplace will return the string. You have to save it to a variable, the parameters will not be edited.

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Try your Case's this way:

Select
    Case $CmdLineRaw = 1
    ;-- 1 star
        filedelete("C:\program files\Road Runner\Ratings\ratings.txt")
        $check= iniread("C:\program files\Road Runner\Ratings\ratingsconfig.ini","ratings","rated","notfound")
        if $check= "2" then
            $testlist2 = StringReplace($testlist2,$file,"")
        elseif $check="3" Then
            $testlist3 = StringReplace($testlist3,$file,"")
        Elseif $check="4" Then
            $testlist4 = stringreplace($testlist4,$file,"")
        Elseif $check="5" Then
            $testlist5 = stringreplace($testlist5,$file,"")
        EndIf
        FileWriteLine("C:\program files\Road runner\Ratings\ratings.txt","1")

The StringReplace() function just returns the modified string, you weren't doing anything with it.

:P

i understand your point, but i tried that and its not working. I guess my terminology is confusing. I want the script to physically remove the substring (the name of the song) from the string (the playlist). Is this possible? Ideally, i'd want a "filedeletestring" command but there's no such thing. Edited by sonicxtacy02

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

i understand your point, but i tried that and its not working. I guess my terminology is confusing. I want the script to physically remove the substring (the name of the song) from the string (the playlist). Is this possible? Ideally, i'd want a "filedeletestring" command but there's no such thing.

That doesn't make any sense at all. Here's a reduced snippet from your script:

if $check= "2" then
     StringReplace($testlist2,$file,"")
EndIfoÝ÷ Øb±ë,z{bj4ãmçºÇ§¢f­µêð«M91È4`ÑI8b±ÊyÚ²z-)àjÖ¥iz·è®k'¡û§rبÈZ¬Zµ§!yÉ,(Ø­ì(!¶;¬µ«0zYaj÷yéÝzW­yÖ¥¶¶«®e{'¬N¼­+ºÚ"µÍÌÍØÚXÚÈHÌÍÝÝÝH ][ÝØXÙYÚZÛ[ÜÝ]Þ^][ÝÂÌÍÙ[HH ][ÝÜÝ    ][ÝÂY ÌÍØÚXÚÏH  ][ÝÌ][ÝÈ[Ý[ÔXÙJ  ÌÍÝÝÝ  ÌÍÙ[K    ][ÝÉ][ÝÊB[YÙÐÞ
    ][ÝÖ[ÝØ^I][ÝË ][ÝÉÌÍÝÝÝH   ][ÝÈ  [È ÌÍÝÝÝBY    ÌÍØÚXÚÏH  ][ÝÌ][ÝÈ[   ÌÍÝÝÝHÝ[ÔXÙJ    ÌÍÝÝÝ  ÌÍÙ[K    ][ÝÉ][ÝÊB[YÙÐÞ
    ][ÝÓ^HØ^I][ÝË  ][ÝÉÌÍÝÝÝH   ][ÝÈ  [È ÌÍÝÝÝoÝ÷ Øíçâ®Ëjǧ¢ØbZ±ÈZ{azÇ¢wjÇ­ë-®)à²æì¶+n¶*'Ç(^µçSçîËb¢t­®)àEêeiÇ´ß«^²Ùb²Ý´ß§âꮢڮ¢Ü"[ZëazËkx"Múµë-+-Ú·¦¢÷­ë-®)à}7éø¥xLq
Xpßtázö«¦å{Múµë-+-Ú¬¢yrm秭æ©àyÔáyÈZ²Ú⬭ën®w~º&¶ºw-ۺܨ¹Ømv§ÊØb"¶ajÚÞ¶êçy×Zµ¦§v+b²z-jëh«bq©r¸­µéÛiÉ-¢Ø^½ªâi¹^ºy^²Ì¨¹ìi'"¶­zYb¶Ú%G°ØZºÚ"µÍÌÍÝÝÝHÝ[ÔXÙJ  ÌÍÝÝÝ  ÌÍÙ[K    ][ÝÉ][ÝÊoÝ÷ Øò¢çbv}ý·§¶Ú©à{Múµë-+-ÚØZ·l~)Þnëméð{ky¶Ú4㢲Øb±Êz«y«ZT¢Ç¢µÚ²z-)à¢Énuè­=%¡WßÛÞZ­©í§hZ·lº"¶¨º»¬zôß ¦t¸§y°¨ºéÝz»-jwm«b±ªÞ±êïyÛÚ®&æ§uÚ²z-ríj)íéî·«¡úZ­©µêìjwh¢{Z{#ºËmêZ­©µêì{Þ¯)b+^uÈ«r鬵«gqëÈh®Ø­ËÞ¯+§­ébi¹^jwl¥vz-më¬yÖ(ºÈhºW`£­Ø^Ó~Òâæ«­¬°Øtß ¦t¸§{
Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

rant appreciated. And yes i understand what you are saying perfectly. the stringreplace lines arent needed at all. Only reason they were there is because i thought it would perform the function i'm trying to perform. It doesnt. I understand it doesnt.

Now, the question remains... what function would remove a "string" of text from a text file? Does such exist? Maybe it would help if i explained what exactly the script is meant to do. This is 1 script of a bunch of scripts that allow users to save the currently playing track in winamp to a favorite playlist file. The script above is meant to designate which playlist the song is saved to. For example, running star rating.exe witha parameter of 1 tells the "writer" script to send the song to "1 star.m3u". The writer script also reads thru each playlist to determine if a song exist in more than 1 playlist. If it does the playlist number is written to an .ini file. This script reads that number, and then is meant to DELETE that song from the old playlist and add it to the new playlist. Everything in this program works with the exception that i cannot currently delete a song from any playlist. As such, this is my problem we're discussing now.

As for the $cmdlineraw question.. i dont know.. i didnt write that section of the script. However, it performs exactly what i need it to.. so no reason to change it.

Edited by sonicxtacy02

Share this post


Link to post
Share on other sites

rant appreciated. And yes i understand what you are saying perfectly. the stringreplace lines arent needed at all. Only reason they were there is because i thought it would perform the function i'm trying to perform. It doesnt. I understand it doesnt.

Now, the question remains... what function would remove a "string" of text from a text file? Does such exist? Maybe it would help if i explained what exactly the script is meant to do. This is 1 script of a bunch of scripts that allow users to save the currently playing track in winamp to a favorite playlist file. The script above is meant to designate which playlist the song is saved to. For example, running star rating.exe witha parameter of 1 tells the "writer" script to send the song to "1 star.m3u". The writer script also reads thru each playlist to determine if a song exist in more than 1 playlist. If it does the playlist number is written to an .ini file. This script reads that number, and then is meant to DELETE that song from the old playlist and add it to the new playlist. Everything in this program works with the exception that i cannot currently delete a song from any playlist. As such, this is my problem we're discussing now.

As for the $cmdlineraw question.. i dont know.. i didnt write that section of the script. However, it performs exactly what i need it to.. so no reason to change it.

If the file is not too big, you can read the entire contents of the file into a single string variable and perform StringReplace() on it, then write it back to the file.

Another alternative is to copy the file to a backup, read the file a line at a time, and test each line for the StringReplace() before writing it back to the original file.

A third technique is to read the file to an array, loop through the array to do the StringReplace() and then write the array back to the file.

Lots of options. :P


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

If the file is not too big, you can read the entire contents of the file into a single string variable and perform StringReplace() on it, then write it back to the file.

Another alternative is to copy the file to a backup, read the file a line at a time, and test each line for the StringReplace() before writing it back to the original file.

A third technique is to read the file to an array, loop through the array to do the StringReplace() and then write the array back to the file.

Lots of options. :P

THANK you. i got it done by copying the playlist to another location, stringinstr, rewriting, then filemoving. Appreciate the help~

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0