topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Monday March 18, 2024, 9:14 pm
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: IDEA - a simple way to batch rearrange blocks of text within filenames  (Read 25649 times)

jovejupiter

  • Charter Member
  • Joined in 2005
  • ***
  • default avatar
  • Posts: 27
    • View Profile
    • Donate to Member
Hi

I'm looking for a simple way to rearrange blocks of text within filenames as a batch process.

The reason is that I enjoy listening to old US radio adventure and scifi series from the 1940s and 50s, such as Suspense, Escape and DimensionX. A lot of shows are in circulation but the file naming style varies - for example showname-date-episodenumber-episodetitle.mp3 or showname-episodenumber-date-episodetitle and so on. I'd like to be able to get a consistent set of names to help identify duplicates etc.

The program would need to take a filename of the form AxBxCxDxE.*, where A,B,C,D and E are blocks of text of at least one character, and x represents a spacer character which could be any valid character for XP filenames. It would then rearrange the name to, for example BxExDxCxA.*, or whatever sequence was required, and repeat the process for all of the files in a specified folder.

I imagine the interface would be a window with three boxes - one box to specify the folder containing the files to me renamed (ideally with the usual browse button), one box to specify the spacer character and one box to list the required sequence of text block in the new file names.

An alternative would be a command line program where the command would look something like:
c:\path\program c:\folder x 53214
where the files are located in c:\folder, the spacer character is x and the rename process would be AxBxCxDxE.* changed to ExCxBxAxD.*

Ideally the program should handle filenames with up to 9 blocks of text

I've searched the net for a suitable program but only found one which claimed to be able to do it. Unfortunately it had vast numbers of options (total overkill for this application), had a confusing interface and was very poorly documented. As a result I never managed to get it to work properly

A simple solution would be very much appreciated.

Thanks in advance

Tony

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,896
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
this sounds like a nice feature to have in a renamer -
but before someone like skrommel tries to write this, might i recommend instead we look for a renamer which already supports something like this using regular expression rename, which would work well in this case.

f0dder

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 9,153
  • [Well, THAT escalated quickly!]
    • View Profile
    • f0dder's place
    • Read more about this member.
    • Donate to Member
Yes, a RegEx file renamer would be very nifty... I know some irc guy who wrote one a while ago (main purpose was renaming MP3s I think), but I haven't seen him in a while :/

- carpe noctem

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,896
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
surely most of the renamers support regex rename

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,896
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
though i have to admit that the basic procedure being asked for is a nice idea -
might i add to this the ability to CHANGE the separator character.

ie change from
A,B,C,D
to
D - B - A

i still think is kind of crazy to write this separately from a renamer as a new project.

skrommel

  • Fastest code in the west
  • Developer
  • Joined in 2005
  • ***
  • Posts: 933
    • View Profile
    • 1 Hour Software by skrommel
    • Donate to Member
 :) Here's a quick script I think works...

Just edit the three first lines.

Edited 2005.27: Changed the script to work properly with multiple files.

Skrommel


path       =C:\Temp             ;Path to the files to rename
inseparator=-                   ;Char(s) separating the parts of the filename
outfile    =<4><2>test<2>-<1>   ;Pattern of the output filename
;infile    =<1>-<2>-<3>-<4>     ;Pattern of the input filename, not used

Loop,%path%\*.*,,1
{
  outname=%outfile%
  Splitpath,A_LoopFileFullPath,name,dir,ext,name_no_ext,drive
  StringSplit,name_,name_no_ext,%inseparator%
  Loop,%name_0%
    %A_Index%:=name_%A_Index%
  Loop,%name_0%
  {
    part:=%A_Index%
    StringReplace,outname,outname,<%A_Index%>,%part%,ReplaceAll
  }
  FileMove,%A_LoopFileFullPath%,%dir%\%outname%.%ext%
}
« Last Edit: November 26, 2005, 07:26 PM by skrommel »

jovejupiter

  • Charter Member
  • Joined in 2005
  • ***
  • default avatar
  • Posts: 27
    • View Profile
    • Donate to Member
Hi Skrommel,

Thanks for the script.
This is probably going to seem an odd question for a forum like this, but how do I run it?
I've been using computers since the late 70s but somehow I've never had time to get into coding. (apart from HTML and assorted in-programme macros)

As an aside, can anybody recommend a good introduction to scripting, which takes you from the real basics up to a reasonably competent level? Things like this make me think that I really should put some time aside to learn more about the subject.

Cheers

Tony

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,896
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
it's not an add question -
i think skrommel is so used to using "AutoHotkey" that he forgets that most of the world has never heard of it.

all of skrommel's utilities are written in a very cool free scripting language system called AutoHotkey:
http://www.autohotkey.com/

it's definitely worth installing since you'll probably be seeing more scripts from skrommel you like :)

most of the time skrommel adds tools to his page but sometimes if its short like this one he just pastes it in a reply.

so you would copy and paste his code and save it as a file with a .ahk extension, and then after you install AutoHotkey you can run it (or compile it into an exe using autohotkey tool).


while i'm here i have a question for skrommel:

1) can we drag and drop files onto this autohotkey script from explorer to have them renamed?
2) if we compile the script can we do this?
3) in your line setting the separator, would there be a way to specify a SPACE character as a separator?

jovejupiter

  • Charter Member
  • Joined in 2005
  • ***
  • default avatar
  • Posts: 27
    • View Profile
    • Donate to Member
Thanks Mouser,

I'll have a look at AutoHotkey

Cheers

Tony

brotherS

  • Master of Good Ideas
  • Honorary Member
  • Joined in 2005
  • **
  • Posts: 2,260
    • View Profile
    • Donate to Member
Hi jovejupiter, welcome to the forum! Make sure to read https://www.donation...dex.php?topic=1167.0 too :)

skrommel

  • Fastest code in the west
  • Developer
  • Joined in 2005
  • ***
  • Posts: 933
    • View Profile
    • 1 Hour Software by skrommel
    • Donate to Member
Re: IDEA - a simple way to batch rearrange blocks of text within filenames
« Reply #10 on: November 26, 2005, 06:21 PM »
 :tellme: Sorry about that! I just threw it together (and edited it about 10 times after posting it!) so I forgot to add the usual AutoHotkey info.

@mouser:
1) can we drag and drop files onto this autohotkey script from explorer to have them renamed?
2) if we compile the script can we do this?

This can easily be added if you compile or say yes to allow dropping on AHK-file when installing AHK.

3) in your line setting the separator, would there be a way to specify a SPACE character as a separator?

Check out the PatternRenamer script below.

It supports drag and drop or a command line, you can use <space> as a separator, and you can have a multichar separator or several singlechar ones. It also shows a preview of the first new filename before starting the renaming.

Skrommel


;PatternRenamer.ahk
; Rename files
;Skrommel @2005    www.donationcoder.com/Software/Skrommel

#SingleInstance,Force
SetBatchLines,-1
;AutoTrim,Off

separator   =-<space>            ;Char(s) separating the parts of the input filename, use <space> for space
oneseparator=1                   ;0=No 1=Yes  Treat the separator as one unit?
outpattern  =<4><2>test<2>-<1>   ;Pattern of the output filename
inpattern   =<1>- <2>- <3>- <4>  ;Pattern of the input filename, not in use, just to explain!

Loop,%0%
{
  StringReplace,inseparator,separator,<space>,%A_Space%,ReplaceAll
  outfile=%outpattern%
  shortpath:=%A_Index%
  Loop,%shortpath%
    longpath=%A_LoopFileLongPath%
  SplitPath,longpath,name,dir,ext,name_no_ext,drive
  If oneseparator=1
  {
    StringReplace,name_no_ext,name_no_ext,%inseparator%,``,ReplaceAll
    StringSplit,name_,name_no_ext,``
  }
  Else
    StringSplit,name_,name_no_ext,%inseparator%
  Loop,%name_0%
    %A_Index%:=name_%A_Index%
  Loop,%name_0%
  {
    part:=%A_Index%
    StringReplace,outfile,outfile,<%A_Index%>,%part%,ReplaceAll
  }
  Loop,10
    StringReplace,outfile,outfile,<%A_Index%>,,ReplaceAll
  If A_Index=1
  {
    msg=InFile:`t`t%longpath%
    msg=%msg%`nOutFile:`t`t%dir%\%outfile%.%ext%
    msg=%msg%`nSeparator:`t%separator%
    msg=%msg%`nOneSeparator:`t%oneseparator%
    msg=%msg%`nInPattern:`t%inpattern%
    msg=%msg%`nOutPattern:`t%outpattern%
    MsgBox,4,Is this correct?,%msg%
    IfMsgBox,No
      Break
  }
  FileMove,%longpath%,%dir%\%outfile%.%ext%,1
}
« Last Edit: November 26, 2005, 07:29 PM by skrommel »

jovejupiter

  • Charter Member
  • Joined in 2005
  • ***
  • default avatar
  • Posts: 27
    • View Profile
    • Donate to Member
Re: IDEA - a simple way to batch rearrange blocks of text within filenames
« Reply #11 on: November 26, 2005, 06:34 PM »
Hi again Skrommel,

I downloaded AutoHotkey which looks extremely useful - The only problem is that your script only rearranges the name of the first file in the folder and doesn't repeat the operation for the other files.

The documentation on the website looks pretty extensive - Does anybody have the whole set of documentation as a single pdf (or similar) rather than as a set of web pages?

Cheers

Tony

jovejupiter

  • Charter Member
  • Joined in 2005
  • ***
  • default avatar
  • Posts: 27
    • View Profile
    • Donate to Member
Re: IDEA - a simple way to batch rearrange blocks of text within filenames
« Reply #12 on: November 26, 2005, 06:43 PM »
Hi again Skrommel,

I converted your pattern renamer script to an exe on my desktop and it seems to work fine for drag and drop.

Cheers

Tony

jovejupiter

  • Charter Member
  • Joined in 2005
  • ***
  • default avatar
  • Posts: 27
    • View Profile
    • Donate to Member
Re: IDEA - a simple way to batch rearrange blocks of text within filenames
« Reply #13 on: November 26, 2005, 07:08 PM »
Hi again,

The program is converting multiple files but seems to miss a few files and leaves them unchanged.

Cheers

Tony

skrommel

  • Fastest code in the west
  • Developer
  • Joined in 2005
  • ***
  • Posts: 933
    • View Profile
    • 1 Hour Software by skrommel
    • Donate to Member
Re: IDEA - a simple way to batch rearrange blocks of text within filenames
« Reply #14 on: November 26, 2005, 07:30 PM »
 :tellme: Still got problems? Care to elaborate?

Skrommel

jovejupiter

  • Charter Member
  • Joined in 2005
  • ***
  • default avatar
  • Posts: 27
    • View Profile
    • Donate to Member
Re: IDEA - a simple way to batch rearrange blocks of text within filenames
« Reply #15 on: November 27, 2005, 12:21 PM »
Hi Skrommel,

I created 15 text files, all identical except for their names which ran from series-date-001-episode.txt to series-date-015-episode.txt

As a test I wanted to reverse the order of the parts, so I set the relevant part of the input ahk file to

separator   =-            ;Char(s) separating the parts of the input filename, use <space> for space
oneseparator=1                   ;0=No 1=Yes  Treat the separator as one unit?
outpattern  =<4>-<3>-<2>-<1>   ;Pattern of the output filename
inpattern   =<1>-<2>-<3>-<4>  ;Pattern of the input filename, not in use, just to explain!

If I understand the code correctly, that should take files of the form A-B-C-D.* and convert them to D-C-B-A.*

As I'm not completely familiar with AutoHotkey yet, I converted the script to an exe file using Ahk2Exe.exe and placed it on my desktop.

Then I dragged the 15 files to the desktop icon.

Files number 004 to 115 were converted correctly but files number 001 to 003 were unchanged.

I then dragged the 3 remaining files to the desktop icon and file 003 was converted.

Dragging files 001 and 002 to the icon converted file 002

Finally dragging file 001 to the icon converted it.

Is there something I've misunderstood about how to set up the input parameters or is there a problem with the code?

Cheers

Tony

skrommel

  • Fastest code in the west
  • Developer
  • Joined in 2005
  • ***
  • Posts: 933
    • View Profile
    • 1 Hour Software by skrommel
    • Donate to Member
Re: IDEA - a simple way to batch rearrange blocks of text within filenames
« Reply #16 on: November 27, 2005, 06:17 PM »
 :tellme: Hmmm.... Is this a problem with the topmost script, too?

Skrommel

jovejupiter

  • Charter Member
  • Joined in 2005
  • ***
  • default avatar
  • Posts: 27
    • View Profile
    • Donate to Member
Re: IDEA - a simple way to batch rearrange blocks of text within filenames
« Reply #17 on: November 28, 2005, 03:20 PM »
Hi again Skrommel,

I converted the original script to an exe without any modifications and dragged the 15 files to the desktop icon.

All 15 file names were changed, with the format:

series-date-001-episode.txt    to    test-series-date-001-episode.txt

The input part of the script reads:

separator   =-<space>            ;Char(s) separating the parts of the input filename, use <space> for space
oneseparator=1                   ;0=No 1=Yes  Treat the separator as one unit?
outpattern  =<4><2>test<2>-<1>   ;Pattern of the output filename
inpattern   =<1>- <2>- <3>- <4>  ;Pattern of the input filename, not in use, just to explain!

If I'm reading the code correctly I would expect this to do nothing to the files in this case as the separator is - and not -<space>

If it accepted the spacer it should end up with file names of the form:

episode-datetestdate-series.

Regards

Tony



skrommel

  • Fastest code in the west
  • Developer
  • Joined in 2005
  • ***
  • Posts: 933
    • View Profile
    • 1 Hour Software by skrommel
    • Donate to Member
Re: IDEA - a simple way to batch rearrange blocks of text within filenames
« Reply #18 on: November 28, 2005, 03:56 PM »
 :) The topmost script does not handle <space>, and it also handles the separator chars individually. I just need to know if there's bug in the renaming or the dragging and dropping.

Skrommel

jovejupiter

  • Charter Member
  • Joined in 2005
  • ***
  • default avatar
  • Posts: 27
    • View Profile
    • Donate to Member
Re: IDEA - a simple way to batch rearrange blocks of text within filenames
« Reply #19 on: November 28, 2005, 04:07 PM »
Hi Skrommel,

The original script renames all of the files in the same way (none are missed out) but the sequence of the parts in the new file names is incorrect.

Changing the input parameters results in the rename working correctly for some files but some are left unchanged.

Cheers

Tony