Jump to content

regExp problem - detailed


Recommended Posts

Hi,

im trying to rename all files in a specific folder.

Their names look like this:

[AB]_Movie_123_[34F4D3].avi

[AB]_Movie_124_[1E25A4].avi

etc

Whereas the numbers in the last brackets are some random hexadecimals.

I'd like to rename them so that the "useless" information is being deleted and only the movie name and number remains:

Movie_123.avi

Movie_124.avi

I was going to change the Movie-names by using the function: StringRegExpReplace.

I made it in 2 steps. The first step would delete the junk at the beginning of the String: [AB]_

This worked fine.

The second step should remove the rest: _[34F4D3]

Here i'm getting problems with the second step as it just doesn't work.

Here is my code so far:

$path = "C:\Anime\Movies"
$FileList = _FileListToArray($path, "*.avi")

For $i=1 To UBound($FileList)-1
    $oldName = StringStripWS($FileList[$i], 8)
    $newName = StringRegExpReplace ( $oldName, "\[DB\]_", "")
    $newName2 = StringRegExpReplace ( $newName, "_\[([:xdigit:])+\]", "")
    Tooltip($oldName & " -- " & $newName & " - " & $newName2)
    sleep (5000)
Next

The tooltip will then display something like this:

[AB]_Movie_123_[34F4D3].avi -- Movie_123_[34F4D3].avi - Movie_123_[34F4D3].avi

As you can see, nothing has been changed in step 2, since the junk after the movie-number ( _[34F4D3] ) is still there.

I just dont know what im doing wrong :)

Hope you guys can give me a hint

PS: i havent included the actual rename-process (FileMove) yet, since i wanted to test it first. Hence the tooltip - sleep

Edited by crest
Link to comment
Share on other sites

A non-regex alternative:

Dim $fileList[2]
$fileList[0] = "[AB]_Movie_123_[34F4D3].avi"
$fileList[1] = "[AB]_Movie_124_[1E25A4].avi"

For $X = 0 to Ubound($fileList) - 1
    $tempArray = StringSplit(StringStripWS($fileList[$X], 8), "_")
    $newFile = StringFormat("%s_%s", $tempArray[2], $tempArray[3])
    ConsoleWrite("Original: " & $fileList[$X] & " New: " & $newFile & @CRLF)
Next
Edited by weaponx
Link to comment
Share on other sites

Here you go:

#include <array.au3>

Dim $fileList[2]
$fileList[0] = "[AB]_Movie_123_[34F4D3].avi"
$fileList[1] = "[AB]_Movie_124_[1E25A4].avi"

For $X = 0 to Ubound($fileList) - 1
    $array = StringRegExp($fileList[$X],".+_(.+_.+)_.+", 2)
    _ArrayDisplay($array)
Next

This will return just the match:

$array = StringRegExp($fileList[$X],".+_(.+_.+)_.+", 3)

Edited by weaponx
Link to comment
Share on other sites

this works fine, thanks weaponx :)

i'd appreciate, if someone could post a solution to the regexp-method, since i'd really like to know what im doing wrong there

Based upon the original post code, here's the syntax correction:

$newName2 = StringRegExpReplace ( $newName, "_\[[[:xdigit:]]+\]", "")

- MoChr(77)& Chr(97)& Chr(100)& Chr(101)& Chr(32)& Chr(121)& Chr(97)& Chr(32)& Chr(108)& Chr(111)& Chr(111)& Chr(107)-------I've told you 100,000 times not to exaggerate!-------Don't make me hit you with my cigarette hand...-------My scripts:Random Episode Selector, Keyboard MouseMover, CopyPath v2.1, SmartRename for XP,Window Tracer[sup]New![/sup]

Link to comment
Share on other sites

Based upon the original post code, here's the syntax correction:

$newName2 = StringRegExpReplace ( $newName, "_\[[[:xdigit:]]+\]", "")
You can also use..

#include <array.au3>

Dim $fileList[2]
$fileList[0] = "[AB]_Movie_123_[34F4D3].avi"
$fileList[1] = "[AB]_Movie_124_[1E25A4].avi"

For $i=1 To UBound($FileList)-1
    $oldName = StringStripWS($FileList[$i], 8)
    $newName = StringRegExpReplace ( $oldName, "\[DB\]_", "")
    $newName2 = StringRegExpReplace ( $newName, "_\[\w*.\]", "")
    MsgBox(0,"info",$oldName & " -- " & $newName & " - " & $newName2)
    sleep (5000)
Next

Although the way Weapon did it is the way I would have done it. It's clean and efficient.

Edited by Ealric

My Projects: [topic="89413"]GoogleHack Search[/topic], [topic="67095"]Swiss File Knife GUI[/topic], [topic="69072"]Mouse Location Pointer[/topic], [topic="86040"]Standard Deviation Calculator[/topic]

Link to comment
Share on other sites

thanks everyone for your great help :)

@ Ealric

i wonder whether that would work: "_\[\w*.\]"

the \w* would match any "word" character: a-z, A-Z or underscore (_). (taken from the autoit helpfile)

So numbers as in _[34F4D3] wouldnt be matched. Or am i wrong?

Edited by crest
Link to comment
Share on other sites

thanks everyone for your great help :)

@ Earric

i wonder whether that would work: "_\[\w*.\]"

the \w* would match any "word" character: a-z, A-Z or underscore (_). (taken from the autoit helpfile)

So numbers as in _[34F4D3] wouldnt be matched. Or am i wrong?

Try it out - it will work. ;)

Although that's not what I would use - Weapon gave the best example.

My Projects: [topic="89413"]GoogleHack Search[/topic], [topic="67095"]Swiss File Knife GUI[/topic], [topic="69072"]Mouse Location Pointer[/topic], [topic="86040"]Standard Deviation Calculator[/topic]

Link to comment
Share on other sites

ive got another question:

how can i specify a regexp that will find a string consistent of any character except for a dot?

Basically the . - operator, just without the "." character itself

given the following string:

ash2[34)khsf_ASd.ashd2o341o4h_324.pdf

it should only return:

ash2[34)khsf_ASd

thanks in advance :)

Link to comment
Share on other sites

Is this what you want?

$a = StringSplit('ash2[34)khsf_ASd\ashd2o341o4h_324\pdf',"\",0)
MsgBox(0,"",$a[$a[0]])

Explanation:

Example:

$a = StringSplit("Sun\Mon\Tue", "\",0)

msgBox(0,"",$a[1])

msgBox(0,"",$a[$a[0]])

StringSplit splits the text & "\" is where the split happens

$a[1] is the number of the string ;in this example--> Sun

$a[0] == index of last string in array ;in this example--> Tue

so to display the last number of the srting-->$a[last_number] we must put it like this:

$a[$a[0]] == $'display'_number_of_the_string[$Index_of_last_string_in_array]

Edited by goldenix
My Projects:[list][*]Guide - ytube step by step tut for reading memory with autoitscript + samples[*]WinHide - tool to show hide windows, Skinned With GDI+[*]Virtualdub batch job list maker - Batch Process all files with same settings[*]Exp calc - Exp calculator for online games[*]Automated Microsoft SQL Server 2000 installer[*]Image sorter helper for IrfanView - 1 click opens img & move ur mouse to close opened img[/list]
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...