AutoHotkey Community

It is currently May 26th, 2012, 4:47 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 12 posts ] 
Author Message
 Post subject: [Smart Renamer]
PostPosted: June 7th, 2006, 7:24 pm 
Offline

Joined: May 9th, 2006, 11:00 pm
Posts: 94
Hello once more.
After coding my [Anime Renamer] i felt like to do something better and ... not self based :?
Therefore....
I started the [Smart Renamer]
Basically you set up a template for the stuff you want to rename, for example

Code:
[%Anime%] %Team% #/*


This will create 2 edit box with a text (the one enclosed within %%) upon them.
And then another edit box for the counter ( represented by #)
After the slash comes the extention... to keep the old one just put *

It almost prepares coffee :p

Then you just have to drag drop files upon the window to start renaming them 8)

Please post your comments. (Any of them !!! Flaming is allowed too >_< , well at least if the person has solid arguments)

THE CODE (Commented ^_^)
Code:
#SingleInstance Off
SetBatchLines, -1

;-----------------------File Management------------------------------
IfNotExist, Template
   FileAppend, [Templates]`nTemp1=`%Example`% #/*, Template
Loop
{
   IniRead, Temp%A_Index%, Template, Templates, Temp%A_Index%
   IfEqual, Temp%A_Index%, ERROR, Break
}
;--------------------------------------------------------------------

Str := Temp1                  ;Take the first template as default
Err2 = No start number


Menu, FileMenu, Add, &Templates, Templates
Menu, FileMenu, Add, E&xit, Exit
Menu, Menu, Add, &File, :FileMenu

Gui_Create:
Gui, 1:Default
Name =
Gui_Num = 0
Pos = 1
Loop, Parse, Str, /            
   If (A_Index & 1)            ;Separate the string in string/extension
   {
      Loop, Parse, A_LoopField, `%
      If (A_Index & 1)
         Name := Name "|" A_LoopField   ;Replace the percent enclosed stuff by pipes
      Else
      {
         Gui_Num++         ;How many variables
         Pos%A_LoopField% := A_Index   ;Remember the variable position
         Title%Gui_Num% := A_LoopField   ;Remember the variable name
      }
   }
   Else
      Ext := A_LoopField         ;Store the extension
StringTrimLeft, Name, Name, 1            ;Remove the heading pipe
Width := Gui_Num*100-8               ;Set the control positioning equation
Gui, 1:-SysMenu +AlwaysOnTop
Loop %Gui_Num%                  ;Create controls
{
   Gui, Add, Text, x%Pos% y0 w50 h11, % Title%A_Index%   
   Gui, Add, Edit, x%Pos% y12 w90 h18 vText%A_Index%
   Pos += 100
}
Gui, Menu, Menu
Gui, Add, Text, x1 y31 w50 h11 , Count
Gui, Add, Edit, x1 y50 w90 h18 Number v#
Gui, Show, h90 w%Width%, Smart Renamer
Return

GuiDropFiles:
Gui, Submit, Nohide                  ;Save the current vars
IfEqual, #,
{                     ;The counter variable is empty
   Loop, Parse, A_GuiControlEvent, `n            ;Parse the file list
   {
      Line++
      If (Line != 1)
      {
      Error(Err2)
      Line = 0
      Exit
      }
   }
Line = 0
}
List := A_GuiControlEvent               ;Duplicate the list
Sort List, \                     ;Sort the list
NName := Name                     ;Duplicate the string
Loop %Gui_Num%
{
   StringReplace, NName, NName, |, % Text%A_Index%      ;Replaces each pipe with the edit vlabel

}
NumLen := StrLen(#)
Zeroes = 0
Loop, Parse, #                     ;Counter is set with heading zeroes?
{
   If !(A_LoopField = "0")
      Break
   Zeroes++
}
Loop, Parse, List, `n
{
   If (Zeroes != 0)
   {
      Loop %Zeroes%
            # = 0%#%
         StringRight #, #, NumLen          ;Keep as much chars as input (avoid 01 => 2)
   }
   StringReplace, FileName, NName, #, %#%         ;Replace # with the counter
   If # =
      StringTrimRight, FileName, FileName, 1      ;Delete the tailing space if there is no counter
   SplitPath, A_LoopField,, Dir, OExt
   IfEqual, Ext, *                  ;Extension is not specified ?
   {          
      Nxt := OExt            
      FileMoveDir, %A_LoopField%, %Dir%\%FileName%   ;Rename Dir
   }
   Else
      Nxt := Ext
   FileMove, %A_LoopField%, %Dir%\%FileName%.%Nxt%      ;Rename File
   If ErrorLevel <> 0
      Msgbox,, ERROR, Could not process file(s).
   #++
}
Return

Error(ByRef Error_Text)
{
   Gui, Submit
   MsgBox,,ERROR, %Error_Text%
   Gui, Show
}

Templates:                     ;Choose among templates
Gui, Destroy
Gui, 2:-SysMenu +AlwaysOnTop
Gui, 2:Default
Gui, 2:Add, ListView, NoSortHdr gChoose, Number|Content
Loop
{
   IfEqual, Temp%A_Index%, ERROR, Break
   LV_Add("", A_Index, Temp%A_Index%)
}
Gui, 2:Show,, Templates
Return

Choose:
WinGetPos, Cx, Cy,,, A
If A_GuiEvent = DoubleClick               ;Double click = choose template
{
   LV_GetText(RowNum, A_EventInfo, 1)
   LV_GetText(RowContent, A_EventInfo, 2)
   Str := Temp%RowNum%               ;Set the string to be the choosen one
Gui, 2:Submit
Gosub, Gui_Create                  ;Recreate the gui with the newly got input
}
return


Exit:
ExitApp


Future Improvements :
-A non-Ridiculous Gui (Almost Done :p)
-Load templates from files ? (Done)
-Maybe a menu to create and choose amongst the templates (Done)
I don't know.... anyway if it isn't that much usefull for other people than me i will not tweak it that much 8)


Edit : Now reading from file and possibility to choose the wanted template.
Edit : 11/06/06 Now everything is working as expected (filels AND folders ) and code is commented
Edit : 03/07/06 Added error recognition / shortened the code ^_^[/code]

_________________
Image


Last edited by Eleria on August 2nd, 2006, 4:19 pm, edited 14 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 8th, 2006, 9:47 am 
Offline

Joined: December 27th, 2005, 1:46 pm
Posts: 6837
Location: France (near Paris)
Uuh, I have no time to read it attentively, just a remark or two:

- What if I want Ext as extension? May seems silly, but I don't like restrictions... Perhaps use a forbidden character like * instead.

- Do you know the Transform, v, Deref, String command? It may save you some sweat... ^_^

_________________
Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 8th, 2006, 10:00 am 
Offline

Joined: May 9th, 2006, 11:00 pm
Posts: 94
Hi PhiLho
Quote:
Perhaps use a forbidden character like * instead

Yeah good idea 8)
Code:
Do you know the Transform, v, Deref, String command? It may save you some sweat

Yeah i do know the transform command.
And as i can see in the manual the deref command is a special transform operation....
Is it the same as dereferencing in c#/c++ ?
If so then i can do
Code:
Transform, List, deref, %A_GuiControlEvent%

Instead of
Code:
List = %A_GuiControlEvent%

So i use less variables, is that what you think of ?

As i do not need to handle non printable characters .. why would i need to transform the strings ? please explain further :wink:

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 8th, 2006, 11:19 am 
Offline

Joined: December 27th, 2005, 1:46 pm
Posts: 6837
Location: France (near Paris)
Example of use:
Code:
Str =  `%WTF`%`%BBQ`%`%Sauce`%  #/AE
WTF = What the Furnace
BBQ = Boring bold quote
Sauce = Yes, thank you
num = 42

pos := InStr(Str, "/")
StringLeft template, Str, pos - 1
StringTrimLeft ext, Str, pos
StringReplace result, template, #, %num%
Transform result, Deref, %result%
MsgBox %template% => %result%.%ext%
Of course, it needs some more tests (pos and ext values, etc.) but that's the spirit.

[EDIT] I don't know if it is usable, you still have to parse the string to know how many fields to display, their names... Sorry, it may not be usable.

_________________
Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 8th, 2006, 12:39 pm 
Offline

Joined: May 9th, 2006, 11:00 pm
Posts: 94
I just looked at your post... and ... it was a :shock:
Its so funny how i ALWAYS complicate simple things .....
Maybe i followed this too closely : "Why having hard time doing it the easy way when its simple to do it in a complicated way"
Anyway thank you again for pointing this out to me.
I will try to update the code with this damn simple alternative.... when i get some time....

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 8th, 2006, 1:14 pm 
Offline

Joined: December 27th, 2005, 1:46 pm
Posts: 6837
Location: France (near Paris)
Eleria wrote:
Its so funny how i ALWAYS complicate simple things .....
Maybe i followed this too closely : "Why having hard time doing it the easy way when its simple to do it in a complicated way"
Actually, I tend to work this way: I do complicate things, and when dust falls down, I take a view from a higher point and then think of ways to simplify my code, factorize stuff, etc.
Experience allows to reduce this tendency, but not that much.
And knowing well your programming language and system to find shortcuts helps too... ^_^

_________________
Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 8th, 2006, 4:02 pm 
Offline

Joined: May 9th, 2006, 11:00 pm
Posts: 94
A question on the flight :
When i do that :
Code:
Transform, List, deref, %A_GuiControlEvent%

List now points to the address of %A_GuiControlEvent%
And it doesnt create another emplacement in the memory ... riiiiiight ?

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 8th, 2006, 4:38 pm 
Offline

Joined: December 27th, 2005, 1:46 pm
Posts: 6837
Location: France (near Paris)
Wrong... -_-
List will contain a copy of the string in A_GuiControlEvent with all occurences of %xxx% replaced by the content of the variable xxx.
To have the address of a variable, you can do:
list := &var
but you can't do much with this address, except accessing the numerical value of the character pointed at the address:
Code:
len := StrLen(str)
addr := &str
Loop %len%
{
   If (*addr = Asc("%"))
      ; Do something...
   addr++
}
That's the same thing as Loop Parse, with the bonus that you can jump forward or backward, stop and continue, etc.

_________________
Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 8th, 2006, 5:06 pm 
Offline

Joined: May 9th, 2006, 11:00 pm
Posts: 94
Okay .... i got confused alot while i was learning C#, when i reached the pointer and struct chapters
And still have some problems with them :lol:
That is why sometime i have problems in ahk :oops:

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 11th, 2006, 11:01 am 
Offline

Joined: May 9th, 2006, 11:00 pm
Posts: 94
New code, works properly and new functionalities ^_^

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 2nd, 2006, 4:12 pm 
Offline

Joined: May 9th, 2006, 11:00 pm
Posts: 94
Code shortened and error check added.

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
PostPosted: April 3rd, 2009, 9:13 am 
Offline

Joined: June 12th, 2008, 10:29 am
Posts: 52
What about dropdown boxes?
Can we do those? With defined options
and where do i save the template? I can't find the file...

also you need to put a comment/marker on the GUI saying, Drag & drop file here to rename according to your selected options... because it isn't intuitive...

Anyway your idea is great and it's the same I had just now for generating perfect filenames for specific uses.

I think another alternative to dragging and dropping files would also be good, like selecting the file/folder.

Also a preview of the final name would be great! For error checking sake...


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 12 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Rajat and 7 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group