Jump to content

newbie needs help with rename loop


Recommended Posts

I want to rename the files in a certain folder. The files all start with a number, followed by a space or symbol or so

ex 1 - song1 11.song2

I want to creat a loop that removes all before the first alphabetic character.

As the amount of characters changes a simple stringleft won't do

Any ideas or directions you could send me to?

Thanks for your intrest

Link to comment
Share on other sites

Stringregex is going to be your best bet.

I'm not the best at this sort of thing, but the line that I would use assuming that you have created an array with something like _filelisttoarray and that all the files are mp3s

#include <file.au3>
#include <array.au3>
local $junk = _arraycreate('Song1.mp3','11-song2.mp3','some3.mp3','111 %234 song4.mp3')

For $icc=1 To UBound($junk)-1
    $found = StringRegExp($junk[$icc],'(\w+\.mp3)',1)
    _ArrayDisplay($found)
    Next

Kerros===============================================================How to learn scripting: Figure out enough to be dangerous, then ask for assistance.

Link to comment
Share on other sites

@ Kerros

said I would keep you informed. Thanks for pointing me in the right direction, but I'm working on the script wright now.

It works but leaves only the last word (ex 101 on fire gives just fire) but I'm way further now than a day ago

Link to comment
Share on other sites

could you drop in an example of the code you are using, along with a couple file names. I'm not the best with regular expressions, but I'll give what help I can.

Kerros===============================================================How to learn scripting: Figure out enough to be dangerous, then ask for assistance.

Link to comment
Share on other sites

started from scratch after your hint, and now I'm checking out the stringreg... functions

all I'v got at the moment is (just testing and reading helpfiles for the rest now)

#include <file.au3>
#include <array.au3>
$folder = FileSelectFolder("Choose a folder.", "")
local $junk = _FileListToArray($folder,"*.mp3")

For $icc=1 To UBound($junk)-1
?
?
?
    Next

it all are names like 101 - Queen & David Bowie - Under Pressure

that I would like to be Queen & David Bowie - Under Pressure (so 101 - should be deleted)

22_Thin Lizzy - The Boys Are Back In Town to Thin Lizzy - The Boys Are Back In Town

and so on

Edited by hoppy
Link to comment
Share on other sites

Almost there

this seems to do the trick (but it could erase also a blank in the name if there is not much stuff before the first letter as it goes 6 characters deep)

wasn't able yet to create a loop that I wanted to make (still trying)

#include <file.au3>
#include <array.au3>
$folder = FileSelectFolder("Choose a folder.", "")
local $junk = _FileListToArray($folder,"*.mp3")

For $icc=1 To UBound($junk)-1
    $good =StringRegExpReplace($junk[$icc],'[^[:alpha:]]','',6)
    msgbox (0,"test",$good)
    Next
Link to comment
Share on other sites

This should do the trick

#include <file.au3>
#include <array.au3>

local $junk = _arraycreate('101 - Queen & David Bowie - Under Pressure.mp3','22_Thin Lizzy - The Boys Are Back In Town.mp3' , '23-whatever popsong.mp3')

For $icc=0 To UBound($junk)-1
     $good =StringRegExp($junk[$icc] , '([\d\s-_]+)((.*).mp3)' , 1  , 1)
     msgbox (0,"test",$good[1])
Next
Edited by Prophet
+==================================================================+| The Definition of Madness: Creating a GUI, with GUI automation scripts |+==================================================================+
Link to comment
Share on other sites

your scrips seems to do the trick indeed

i was tryin (unsuccessfull)

#include <file.au3>
#include <array.au3>
AutoItSetOption("TrayIconHide", 1)

$folder = FileSelectFolder("Kies de folder.", "")
$junk = _FileListToArray($folder,"*.mp3")

For $icc=1 To UBound($junk)-1
    $nOffset = 1
    while 1
        $test =StringRegExp (stringleft($junk[$icc],1),'[^[:alpha:]]', 1, $nOffset)
    If @error then ExitLoop
        $test =Stringtrimleft($junk[$icc],1)
        msgbox(0,"",$test)
    WEnd
Next

loop inside a loop never seems to work for me

but in your script when I replace

the handmade array to fileselectfolder

so

local $junk = _arraycreate('101 - Queen & David Bowie - Under Pressure.mp3','22_Thin Lizzy - The Boys Are Back In Town.mp3' , '23-whatever popsong.mp3')
to

$folder = FileSelectFolder("Kies de folder.", "")
$junk = _FileListToArray($folder,"*.mp3")

like I used before it doesn't work anymore

But no worries I'll find my mistake

Thanks for the support

Link to comment
Share on other sites

msgbox (0,"test",$good[1])

msgbox (0,"test",$good^ ERROR

strange with [1] your stuff works just fine without not , with[1] I get an error in the modified one and not whithout, but then the script misses its purpose

Edited by hoppy
Link to comment
Share on other sites

its in the .mp3

may array was already filtered en needed to remove your .mp3 in the expression

thanks for the help (but I still don't get how it stops from filtering the blanks in the middle, need to take a good look again at the helpfiles)

Thanks phrophet and kerros

Edited by hoppy
Link to comment
Share on other sites

Ill give a short explanation of the regular expression

([\d\s-_]+) means that it will group all combination of numbers (\d) Spaces (\s) and the 2 characters "_" and "-"

((.*).mp3) means that it wil group everything up to and including the .mp3

So the first part looks for digits spaces and - _ , as soon as it detects a letter it moves to the second group, wich will include everything else.

Edited by Prophet
+==================================================================+| The Definition of Madness: Creating a GUI, with GUI automation scripts |+==================================================================+
Link to comment
Share on other sites

@ prophet (if your still there)

Just out of curiosity

how would you make a loop that keeps deleting the left character till the first alpha is found

your scripts did the job at my wife's external drive but am eager to learn more about loops and functions

Link to comment
Share on other sites

Well there is probably more then one way to do this, but here is a way

$SomeString = "4354#$%^&this is somestring"
$array = StringSplit($SomeString , "") ;Split the string to individual characters
$start = 0

For $i=1 to UBound($array) - 1 ; look at each character
    If StringRegExp($array[$i] , '[[:alpha:]]' , 0)  Then ;if the character is a letter
        $start = $i ;save the start position
        ExitLoop    ;and exit the loop
    EndIf
Next

$output = StringTrimLeft($SomeString , $start -1) ; cut the non letters from string
MsgBox(0 , "test" , $output)
+==================================================================+| The Definition of Madness: Creating a GUI, with GUI automation scripts |+==================================================================+
Link to comment
Share on other sites

But ofcourse a more advance RegExp like this would give pretty much the same results :)

$SomeString = "4354#$%^&this_is-somestring too"
$output = StringRegExp($SomeString , '([[:alpha:]\s_-]+)' , 3)
MsgBox(0 , "test" , $output[0])
+==================================================================+| The Definition of Madness: Creating a GUI, with GUI automation scripts |+==================================================================+
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...