Sign in to follow this  
Followers 0
pollop

Raise the last digit of a string

20 posts in this topic

Hi,

I'm trying to make a function that raise the last digit found in a string.

I think I have to use the RegExp but I'm not getting what I want :/

Exemple : The string is "CSI_Miami_7x04.avi"

I would like the function to return "CSI_Miami_7x05.avi"

I tried :

$sFileName = StringRegExpReplace($sFileName, '(\d)', '$1+1')

$sFileName = StringRegExpReplace($sFileName, '(\d)', '$1'+1)

But none of them are working :/

Can you help me a little ? :D

Thanks a lot !

( Sorry for my poor english ^.^ I'm doing my best :huggles: )

Share this post


Link to post
Share on other sites



You can delete the "MsgBox" Functions, they are just for examples. :D

MsgBox(0, "", Raise("CSI_Miami_7x04.avi"))
MsgBox(0, "", Raise("CSI_Miami_3x01.avi"))
MsgBox(0, "", Raise("CSI_Miami_4x00.avi"))

Func Raise($String)
    Local $Return

    For $Count = StringLen($String) - 1 To 1 Step -1
        If StringIsDigit(StringMid($String, $Count, 1)) = 1 Then
            $Return = StringLeft($String, $Count - 1) & StringMid($String, $Count, 1) + 1 & StringRight($String, $Count - (StringLen($String) - 1))
            ExitLoop (1)
        EndIf
    Next

    Return $Return
EndFunc   ;==>Raise

Share this post


Link to post
Share on other sites

$string = "CSI_Miami_7x04.avi"
$replacement = StringRegExp($string,"(\d)(?=\.avi)",3)
$new_string = StringRegExpReplace($string,"(\d)(?=\.avi)",$replacement[0]+1)
MsgBox(0,"",$new_string)

I don't know if anybody can do it in just a single line.


Hi ;)

Share this post


Link to post
Share on other sites

$string = "CSI_Miami_7x04.avi"
$replacement = StringRegExp($string,"(\d)(?=\.avi)",3)
$new_string = StringRegExpReplace($string,"(\d)(?=\.avi)",$replacement[0]+1)
MsgBox(0,"",$new_string)

I don't know if anybody can do it in just a single line.

Ooooh, a challenge! It may take a bit of time, but I think I can do it in a single line. Can't be that hard.

The Wheel of Time turns, and Ages come and pass, leaving memories that become legend. Legend fades to myth, and even myth is long forgotten when the Age that gave it birth comes again.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Hehehe, I did it a different way, but it was a similarly long line :D

$new_string = StringTrimRight($string, 5) & StringTrimLeft(StringTrimRight($string, 4), StringLen(StringTrimRight($string, 4)) - 1) + 1 & StringTrimLeft($string, StringLen($string) - 4)
Edited by Hawkwing

The Wheel of Time turns, and Ages come and pass, leaving memories that become legend. Legend fades to myth, and even myth is long forgotten when the Age that gave it birth comes again.

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

:D It's that easy, huh? :huggles:

My single line version:

$string = "CSI_Miami_7x04.avi"
$new_string = StringRegExpReplace($string,"(\d)(?=\.avi)",StringRegExpReplace($string,".*(\d)\.avi","\1","")+1)
MsgBox(0,"",$new_string)
Edited by Mison

Hi ;)

Share this post


Link to post
Share on other sites

That does it.

Local $string = "CSI_Miami_7x09.avi"
Local $ar = StringRegExp($string, "(?U)(.*[^0-9])(\d+)(\..*\z)", 1)
$ar[1] += 1
Local $new_string = _ArrayToString($ar, '')
MsgBox(0, "", $new_string)

Some previous suggestions failed to increment the _number_ as a whole.

The successor of "abc999.mpg" should be "abc1000.mpg" and not "abc9910.mpg", at least as I see it.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

And another attempt.

$string = "CSI_Miami_7x09.avi"
$new_string = Execute('"' & StringRegExpReplace($string, _
        "x(\d+)\.", 'x" & StringRight("0" & (\1 + 1),2) & ".') & '"')
MsgBox(0, "", $new_string)

Share this post


Link to post
Share on other sites

And another attempt.

Nice try, but still lacks generality: the successor of "abc99.mpg" becomes "abc00.mpg" which may not be what's needed.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

OMG ! :D

It's so nice to see so much people helping me :huggles:

Thanks a lot guys !

I'll try all the code you posted and choose the one that's the best for me...

I'll maybe need to change it a little bit 'cause there are many cases...

Here are some exemples :

"myfile_7x05.avi" => "myfile_7x06.avi"

"myfile_7x09.avi" => "myfile_7x10.avi"

"myfile_7x099.avi" => "myfile_7x100.avi"

With extention : "myfile_7x04.mp4" => "myfile_7x05.mp4" (and not myfile_7x04.mp5)

Generally : "someText837462someText2737someText.AnyExtention" => "someText837462someText2738someText.AnyExtention"

It's not needed that "myfile_7x9.avi" => "myfile_7x10.avi" 'cause if i have more than 9 file i'll use the 09 notation

It's not needed that "myfile_7x99.avi" => "myfile_7x100.avi" 'cause if i have more than 99 file i'll use the 099 notation

Anyway, thanks a lot for your help guys !

If you still have a little time for me, it would be great.

But it's already so nice to helped me so much.

Thank you

PS : Again, sorry for my english guys ! :

Edited by pollop

Share this post


Link to post
Share on other sites

Local $in[6] = ["myfile_7x05.avi", _
"myfile_7x09.avi", _
"myfile_7x9.avi", _
"myfile_7x099.avi", _
"myfile_7x99.avi", _
"myfile_7x04.mp4"]

Local $want[6] = ["myfile_7x06.avi", _
"myfile_7x10.avi", _
"myfile_7x10.avi", _
"myfile_7x100.avi", _
"myfile_7x100.avi", _
"myfile_7x05.mp4"]

$error = False

For $i = 0 to UBound($want)-1
    $haveNow = _incrementEpisode($in[$i]) ; <== In a single fucking line.

    ConsoleWrite("Test start with: " & $in[$i] & @CRLF)
    ConsoleWrite("Test expected: " & $want[$i] & @CRLF)
    ConsoleWrite("Test got: " & $haveNow & @CRLF)

    If Not ($want[$i] == $haveNow) Then
        $error = True
        ConsoleWrite("! Test failed" & @CRLF)
    Else
        ConsoleWrite("> Test success" & @CRLF)
    EndIf
Next

If $error Then
    ConsoleWrite("! One or more errors have occured." & @CRLF)
    Exit 1
EndIf

Exit 0

Func _incrementEpisode($in)
    $episodeRegEx = StringRegExp($in, "[0-9]*x([0-9]*)", 3)
    $episode = String(Number($episodeRegEx[0]) + 1)
    If StringLen($episode) == 1 Then $episode = "0" & $episode
    Return StringReplace($in, $episodeRegEx[0], $episode)
EndFunc

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

Yeah ! RegExp are so powerful :huggles:

Your script is amazing for almost every cases ! : Thanks a lot !

There are just some cases not working but i'll try to fix that :D

Test start with: myfile_7x055.mp4
Test expected: myfile_7x056.mp4
Test got: myfile_7x56.mp4
! Test failed

Test start with: someText456someText123SomeText.mp4
Test expected: someText456someText124SomeText.mp4
Test got: 
! Test failed
Edited by pollop

Share this post


Link to post
Share on other sites

#include <File.au3>

Local $in[7] = ["myfile_7x05.avi", _
        "myfile_7x09.avi", _
        "myfile_7x9.avi", _
        "myfile_7x099.avi", _
        "myfile_7x99.avi", _
        "someText456someText123SomeText.mp4", _
        "myfile_7x04.mp4"]

Local $want[7] = ["myfile_7x06.avi", _
        "myfile_7x10.avi", _
        "myfile_7x10.avi", _
        "myfile_7x100.avi", _
        "myfile_7x100.avi", _
        "someText456someText124SomeText.mp4", _
        "myfile_7x05.mp4"]

$error = False

For $i = 0 To UBound($want) - 1
    $haveNow = _incrementEpisode($in[$i]) ; <== In a single fucking line.

    ConsoleWrite("Test start with: " & $in[$i] & @CRLF)
    ConsoleWrite("Test expected: " & $want[$i] & @CRLF)
    ConsoleWrite("Test got: " & $haveNow & @CRLF)

    If Not ($want[$i] == $haveNow) Then
        $error = True
        ConsoleWrite("! Test failed" & @CRLF)
    Else
        ConsoleWrite("> Test success" & @CRLF)
    EndIf
Next

If $error Then
    ConsoleWrite("! One or more errors have occured." & @CRLF)
    Exit 1
EndIf

Exit 0

Func _incrementEpisode($in)
    Dim $szDrive, $szDir, $szFName, $szExt
    $TestPath = _PathSplit($in, $szDrive, $szDir, $szFName, $szExt)
    $episodeRegEx = StringRegExp($szFName, "(\d+)", 3)
    $episode = String(Number($episodeRegEx[UBound($episodeRegEx) - 1]) + 1)
    Return StringReplace($szFName, $episodeRegEx[UBound($episodeRegEx) - 1], StringFormat('%0' & StringLen($episodeRegEx[UBound($episodeRegEx) - 1]) & 'i', $episode)) & $szExt
EndFunc ;==>_incrementEpisode

Mega


Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites

#include <File.au3>

Local $in[7] = ["myfile_7x05.avi", _
        "myfile_7x09.avi", _
        "myfile_7x9.avi", _
        "myfile_7x099.avi", _
        "myfile_7x99.avi", _
        "someText456someText123SomeText.mp4", _
        "myfile_7x04.mp4"]

Local $want[7] = ["myfile_7x06.avi", _
        "myfile_7x10.avi", _
        "myfile_7x10.avi", _
        "myfile_7x100.avi", _
        "myfile_7x100.avi", _
        "someText456someText124SomeText.mp4", _
        "myfile_7x05.mp4"]

$error = False

For $i = 0 To UBound($want) - 1
    $haveNow = _incrementEpisode($in[$i]) ; <== In a single fucking line.

    ConsoleWrite("Test start with: " & $in[$i] & @CRLF)
    ConsoleWrite("Test expected: " & $want[$i] & @CRLF)
    ConsoleWrite("Test got: " & $haveNow & @CRLF)

    If Not ($want[$i] == $haveNow) Then
        $error = True
        ConsoleWrite("! Test failed" & @CRLF)
    Else
        ConsoleWrite("> Test success" & @CRLF)
    EndIf
Next

If $error Then
    ConsoleWrite("! One or more errors have occured." & @CRLF)
    Exit 1
EndIf

Exit 0

Func _incrementEpisode($in)
    Dim $szDrive, $szDir, $szFName, $szExt
    $TestPath = _PathSplit($in, $szDrive, $szDir, $szFName, $szExt)
    $episodeRegEx = StringRegExp($szFName, "(\d+)", 3)
    $episode = String(Number($episodeRegEx[UBound($episodeRegEx) - 1]) + 1)
    Return StringReplace($szFName, $episodeRegEx[UBound($episodeRegEx) - 1], StringFormat('%0' & StringLen($episodeRegEx[UBound($episodeRegEx) - 1]) & 'i', $episode)) & $szExt
EndFunc ;==>_incrementEpisode

Mega

Excellent code, Mega. What did you think of the test setup?

Share this post


Link to post
Share on other sites

This is a bit more clean.

#include <File.au3>

Local $in[7] = ["myfile_7x05.avi", _
        "myfile_7x09.avi", _
        "myfile_7x9.avi", _
        "myfile_7x099.avi", _
        "myfile_7x99.avi", _
        "someText456someText123SomeText.mp4", _
        "myfile_7x04.mp4"]

Local $want[7] = ["myfile_7x06.avi", _
        "myfile_7x10.avi", _
        "myfile_7x10.avi", _
        "myfile_7x100.avi", _
        "myfile_7x100.avi", _
        "someText456someText124SomeText.mp4", _
        "myfile_7x05.mp4"]

$error = False

For $i = 0 To UBound($want) - 1
    $haveNow = _incrementEpisode($in[$i]) ; <== In a single fucking line.

    ConsoleWrite("Test start with: " & $in[$i] & @CRLF)
    ConsoleWrite("Test expected: " & $want[$i] & @CRLF)
    ConsoleWrite("Test got: " & $haveNow & @CRLF)

    If Not ($want[$i] == $haveNow) Then
        $error = True
        ConsoleWrite("! Test failed" & @CRLF)
    Else
        ConsoleWrite("> Test success" & @CRLF)
    EndIf
Next

If $error Then
    ConsoleWrite("! One or more errors have occured." & @CRLF)
    Exit 1
EndIf
Exit 0

Func _incrementEpisode($in)
    Local $szDrive, $szDir, $szFName, $szExt
    _PathSplit($in, $szDrive, $szDir, $szFName, $szExt)
    Local $episodeRegEx = StringRegExp($szFName, "(\d+)", 3)
    If @error Then Return 0
    Local $lastDigit = $episodeRegEx[UBound($episodeRegEx) - 1]
    Return StringReplace($szFName, $lastDigit, StringFormat('%0' & StringLen($lastDigit) & 'i', Number($episodeRegEx[UBound($episodeRegEx) - 1]) + 1)) & $szExt
EndFunc ;==>_incrementEpisode

Your test script works great. It made it very easy to find a solution.

Mega


Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites

#16 ·  Posted (edited)

And another one.

#include <Math.au3>

Local $in[8] = ["myfile_7x05.avi", _
    "myfile_7x09.avi", _
    "myfile_7x9.avi", _
    "myfile_7x999.avi", _
    "myfile_7x99.avi", _
    "myfile_7x2.avi", _
    "myfile_7x04.mp4", _
    "someText837462someText2737someText.AnyExtention"]

Local $want[8] = ["myfile_7x06.avi", _
    "myfile_7x10.avi", _
    "myfile_7x10.avi", _
    "myfile_7x1000.avi", _
    "myfile_7x100.avi", _
    "myfile_7x3.avi", _
    "myfile_7x05.mp4", _
    "someText837462someText2738someText.AnyExtention"]

$error = False

For $i = 0 To UBound($want) - 1
    $haveNow = _IncrementEpisode($in[$i]) ; <== In a single fucking line.

    ConsoleWrite("Test start with: " & $in[$i] & @CRLF)
    ConsoleWrite("Test expected: " & @TAB & $want[$i] & @CRLF)
    ConsoleWrite("Test got: " & @TAB & $haveNow & @CRLF)

    If Not ($want[$i] == $haveNow) Then
    $error = True
    ConsoleWrite("! Test failed" & @CRLF)
    Else
    ConsoleWrite("> Test success" & @CRLF)
    EndIf
Next

If $error Then
    ConsoleWrite("! One or more errors have occured." & @CRLF)
    Exit 1
EndIf

Exit 0

Func _IncrementEpisode($sFileName)
    Local $sSecNumInStr = StringRegExpReplace($sFileName, "^[^\d]*\d+[^\d]+(\d{1,6})[^\d]*\..*$", "\1")
    Local $iLen = _Max(StringLen(String($sSecNumInStr)), StringLen($sSecNumInStr + 1))
    Return Execute('"' & StringRegExpReplace($sFileName, "(.*[^\d]+)(\d{1,6})(.*?\..*)", _
    '\1" & StringRight("0" & (\2 + 1),' & $iLen & ') & "\3') & '"')
EndFunc ;==>_IncrementEpisode
Edited by Malkey

Share this post


Link to post
Share on other sites

#17 ·  Posted (edited)

OMG ! Thank you so much ! Your code is so clean ^.^ It works perfectly :

Thanks to you i can now just be in my bed and the episodes of my favorite tv show are launched automaticly ! :D

Here's my final Function :

Func guessNextEpisodeFullPath($sFileFullPath = False)
    LogIt("          Trying to gess the next episode full path.")
    If Not $sFileFullPath Then
        $sFileFullPath = getFileRunningFullPath()
    EndIf
    If $sFileFullPath Then
        Local $szDrive, $szDir, $szFName, $szExt
        _PathSplit($sFileFullPath, $szDrive, $szDir, $szFName, $szExt)
        Local $episodeRegEx = StringRegExp($szFName, "(\d+)", 3)
        If @error Then Return False
        Local $lastDigit = $episodeRegEx[UBound($episodeRegEx) - 1]
        Local $sNextEpisodeFullPath = $szDrive & $szDir & StringReplace($szFName, $lastDigit, StringFormat('%0' & StringLen($lastDigit) & 'i', Number($episodeRegEx[UBound($episodeRegEx) - 1]) + 1)) & $szExt
        If FileExists($sNextEpisodeFullPath) Then
            Return $sNextEpisodeFullPath
        EndIf
    EndIf
    Return False
EndFunc ;==> guessNextEpisodeFullPath

Here is the log File of my script :D

= It's seems that there is a new file playing.
== File Name : CSI_Miami_7x01.avi
== Full Path : C:\NAS\Series\CSI Miami\Saison 7\CSI_Miami_7x01.avi
== Total Runtime : 2497
== Credits appears at : 2341
== Next Episode Full Path (From INI) : C:\NAS\Series\CSI Miami\Saison 7\CSI_Miami_7x02.avi
= We are at the end of the Video. Closing the player.
= Automatic launching of next episode : C:\NAS\Series\CSI Miami\Saison 7\CSI_Miami_7x02.avi

= It's seems that there is a new file playing.
== File Name : CSI_Miami_7x02.avi
== Full Path : C:\NAS\Series\CSI Miami\Saison 7\CSI_Miami_7x02.avi
== Total Runtime : 2491
== Credits appears at : 2341
== Next Episode Full Path (From INI) : False
== [W] Can't find next episode path (in INI File). Will try to guess it...
== Guessing of next episode path : C:\NAS\Series\CSI Miami\Saison 7\CSI_Miami_7x03.avi
= We are at the end of the Video. Closing the player.
= Automatic launching of next episode : C:\NAS\Series\CSI Miami\Saison 7\CSI_Miami_7x03.avi

= It's seems that there is a new file playing.
== File Name : CSI_Miami_7x03.avi
== Full Path : C:\NAS\Series\CSI Miami\Saison 7\CSI_Miami_7x03.avi
== Total Runtime : 2462
== [W] Can't find when the credits appears (in INI File). Will use total runtime instead.
== Credits appears at : 2462
== Next Episode Full Path (From INI) : False
== [W] Can't find next episode path (in INI File). Will try to guess it...
== Guessing of next episode path : C:\NAS\Series\CSI Miami\Saison 7\CSI_Miami_7x04.avi
= We are at the end of the Video. Closing the player.
= Automatic launching of next episode : C:\NAS\Series\CSI Miami\Saison 7\CSI_Miami_7x04.avi

Again, thanks a lot guys :huggles:

Edited by pollop

Share this post


Link to post
Share on other sites

If you wanted to guess which the next file is from a sequence of files, then we could have written a perfect algorithm to do that. The current algorithm will fail if it's not an exact match. But never mind that.

Share this post


Link to post
Share on other sites

#19 ·  Posted (edited)

If I knew that before, then I wouldn't have helped you :D:huggles::

You lazy guy! :D

Edited by Xenobiologist

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites

If you wanted to guess which the next file is from a sequence of files, then we could have written a perfect algorithm to do that. The current algorithm will fail if it's not an exact match. But never mind that.

Can you tell me a little bit more about that ? :D

Thanks.

If I knew that before, then I wouldn't have helped you :D:huggles::

You lazy guy! :D

Haha ^.^

Thanks again anyway :)

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