GP SoftwareTwitter
Opus FAQsManualCommandsObjects

From File rename v3.1

vbscript
scripting

#1

If you are using Directory Opus 12 and above use From clipboard or file rename script 4.0 instead.

This rename script takes lines from a user created text file or from clipboard and use them as part of the new file names. Read the instructions below for more information.

Script Information
From file rename script version 3.1 (tested with Directory Opus 10.0.5.0)
by Henrik Ripa (gieron@yahoo.com) 2012-05-13

Changelog for version 3.1

  • Added option to skip empty lines

Installation
Download and unzip the .orp file. Open the Directory Opus rename dialog and set it to advanced mode. Select import from the file menu and open the .orp file. You can then add it to the presets with the add button. I suggest using the name From File but you can call it anything you want.

Instructions
This script replaces the string ‘FROMFILE’ in new file names with lines taken from a user created text file or from the clipboard. If the lines include any characters that are illegal in filenames they will be removed.

By default the text file the script looks for must have the name ‘filenames.txt’ or ‘Clipboard Text.txt’. The latter is the default name DOpus uses when you paste text content into a lister. The text file must be located in the same directory as the files you want to rename.

When the text file is in place, or you have copied some text to the clipboard, select the files you want to rename. The order of the selected files in the lister must be the same as the order in the text file. Then click the rename button and select this script under presets.

Set up the old name and new name fields so that the the string ‘FROMFILE’ is present in the new file names. You can use standard rename, regular expressions or find and replace. Inspect the results in the preview pane and click OK.

If you have problems you can start by examining the script tab in the Directory Opus output window. The script outputs a lot of information there. If you find a bug or have a feature request you can either post it in this thread or send me an email.
FromFile-3_1.zip (4.2 KB)


Batch renaming files from a text list
Script to rename from a file or clipboard content
From Clipboard or File v4.0
Move folders based on txt file
Using a pre Opus 12 beta script?
#2

works great, thanks for adding clipboard functionality. VERY handy.


#3

thank you :grin:


#4

i tried this script but i cant get it to work.
i tried serveral things.

is it possible to post screenshots of how to use it. or explain exactly what to do
i added the FROMFILE to the old and new name but cant get it to work.

any help would be wonderful


#5

Just put the list of filenames in Clipboard Text.txt (in the same dir as the files).

Select the files you want to rename.

Click the Rename button.

Import the preset (or select it if you’ve already saved it into the list on the right), as explained in the root post.

That’s it. The preset sets up the old & new names so you should not have to edit those fields unless you want something different.



List renaming
#6

Thank you.

i had the pld name wrong i didnt had the
^.+(…)$|^.$

like you have in the screen shot :slight_smile:

Thank you very much this makes renaming like 50-100 files at once easyer :slight_smile:


#7

This is just AUESOME!!!
You make me buy this program, DO. :slight_smile:

And I studied this rename script thing for last two days, but still it’s difficult to me.

So, can I ask you a favor??

If there is any BLANK line in the text file, skip the BLANK line.
And read the next NOT BLANK line.

Is is possible?

It’s so sad that I have many ideas, but I don’t have that skill. :blush:

Thank you!!! :thumbsup:


#8

I tested many many times ruining your great work, finally I got just what I wanted.
But, as you see, it’s very stupid way. :blush:
I never studied programming things before and even English isn’t my first language.

Could you Fix my stupid work? :slight_smile:

@script vbscript
option explicit

' ************************** Script Information **************************
' From file rename script version 3.0
' (tested with Directory Opus 9.5.6.0)
' by Henrik Ripa (gieron@yahoo.com) 2010-12-17
' ************************************************************************

' ****************************** Changelog *******************************
' Version 3.0
' + Will now get text from the clipboard if there are no text files to use
' + Now supports text files in UTF-8 and UTF-16 (LE and BE) if they have
'   a correct BOM (Byte Order Mark)
' + Added an option to read lines in reverse order
' + The default regexp works better for file names with dots in them
' Version 2.1
' + Better regexp that works for renaming of directories and files
'   without extension.
' + Will now remove characters that are illegal in filenames.
' + Now looks for a 'Clipboard Text.txt' file if the 'filenames.txt'
'   file is missing.
' Version 2.0
' + Rewritten to use magic markers in new filenames.
' Version 1.0
' + Initial version. Worked entirely differently.
' ************************************************************************

' ***************************** Installation *****************************
' Download the .orp file. Open the Directory Opus rename dialog and set
' it to advanced mode. Select import from the file menu and open the .orp
' file. You can then add it to the presets with the add button. I suggest
' using the name From File but you can call it anything you want.
' ************************************************************************

' **************************** Instructions ******************************
' This script replaces the string 'FROMFILE' in new file names with lines
' taken from a user created text file or from the clipboard. If the lines
' include any characters that are illegal in filenames they will be
' removed.
'
' By default the text file the script looks for must have the name
' 'filenames.txt' or 'Clipboard Text.txt'. The latter is the default name
' DOpus uses when you paste text content into a lister. The text file
' must be located in the same directory as the files you want to rename.
'
' When the text file is in place, or you have copied some text to the
' clipboard, select the files you want to rename. The order of the
' selected files in the lister must be the same as the order in the text
' file. Then click the rename button and select this script under presets.
' 
' Set up the old name and new name fields so that the the string
' 'FROMFILE' is present in the new file names. You can use standard
' rename, regular expressions or find and replace. Inspect the results
' in the preview pane and click OK.
'
' If you have problems you can start by examining the script tab in the
' Directory Opus output window. The script outputs a lot of information
' there. If you find a bug or have a feature request you can either go to
' the Dopus Resource Centre forum thread where you got this script from
' or send me an email.
' ************************************************************************

' ******************************* Options ********************************
dim optReverseOrder, optFileNames, optMagicMarker, optIllegalReplacement
dim optIllegalCharacters, optUseEmptyLines

' If this is true the file names from the file, or from clipboard,
' will be read in reverse order
optReverseOrder = False

' List of files to read file names from, can be empty if you only want
' to use text from the clipboard
optFileNames = Array("filenames.txt", "Clipboard Text.txt")

' Magic marker to replace with names from file
optMagicMarker = "FROMFILE"

' What to replace illegal file name characters with
optIllegalReplacement = ""

' Pattern of illegal file name characters
optIllegalCharacters = "[/\\:*?<>\|""]"

' Whether to use empty lines in the source or not
optUseEmptyLines = False
' ************************************************************************

' ************************ DOpus rename function *************************
' This is the function that is called by DOpus to get a new file name,
' it is called once for each file that will be renamed
Function Rename_GetNewName _
  ( strFileName, strFilePath, fIsFolder, strOldName, ByRef strNewName )

  dorInitialize strFilePath
  strNewName = dorGetNewName(strFileName, strNewName)
End Function
' ************************************************************************

' ********************* Directory Opus rename module *********************
dim dorInitialized
dorInitialized = False

' Initalizes the module by initializing all the modules it depends on
Function dorInitialize( strPath )
  ' Only run this initialization once
  if (dorInitialized) then exit function
  dorInitialized = True

  ' Print script identification
  eioPrint "From file rename script by Henrik Ripa"

  ' Initialize the file names source and remove illegal character modules
  fnsInitialize strPath, optFileNames, optReverseOrder
  ricInitialize optIllegalCharacters, optIllegalReplacement
End Function

' Returns the new name for the given file, the input is the files old
' name and the name it gets after the preprocessing that is done in DOpus
Function dorGetNewName( strOldName, strMidName )
  dim line

  ' Set new name to the original name to undo the preprocessing
  ' in case we do not come up with a new name
  dorGetNewName = strOldName

  ' Exit if there are nothing to rename with
  if (fnsNoMoreNames()) then exit function

  ' Ignore anything that does not have the magic marker
  if (instr(strMidName, optMagicMarker) = 0) then exit function

  ' Ignore the filenames file if the user has selected it
  if (strOldName = focGetUsedFile()) then exit function

  ' Get next file name from source
  line = fnsGetNextName()

  ' Clean file name from unwanted characters
  line = ricReplaceCharacters(line)

  ' Assemble new file name, if it isn't empty
  if (optUseEmptyLines or line <> "") then
    dorGetNewName = Replace(strMidName, optMagicMarker, line)
   if (fnsGetNextName = "") then exit function
      if (fnsGetNextName = "") then exit function
        if (fnsGetNextName = "") then exit function
	  if (fnsGetNextName = "") then exit function
	    if (fnsGetNextName = "") then exit function
	      if (fnsGetNextName = "") then exit function
	        if (fnsGetNextName = "") then exit function
		  if (fnsGetNextName = "") then exit function
		    if (fnsGetNextName = "") then exit function
		      if (fnsGetNextName = "") then exit function
		        if (fnsGetNextName = "") then exit function
			  if (fnsGetNextName = "") then exit function
			  
  end if
  
End Function
' ************************************************************************

' ****************** Replace illegal characters module *******************
dim ricRegexp, ricReplacement

' Takes arguments for the module and creates the regexp object
Function ricInitialize( strPattern, strReplacement )
  set ricRegexp = new Regexp
  ricRegexp.Global = True
  ricRegexp.IgnoreCase = False
  ricRegexp.Pattern = strPattern
  ricReplacement = strReplacement
End Function

' Replaces illegal characters in the text with the replacement string
Function ricReplaceCharacters( strText )
  dim text
  text = ricRegexp.Replace(strText, ricReplacement)
  if (strText <> text) then
    eioPrint "Replaced illegal characters in """ & strText & """"
  end if
  ricReplaceCharacters = text
End Function
' ************************************************************************

' *********************** File names source module ***********************
dim fnsSourceLines, fnsCurrentLine

' Initializes the file names source by getting the source text
' from file or clipboard
Function fnsInitialize( strPath, arrFileNames, bolReverseOrder )
  dim lines, text
  fnsCurrentLine = 0
  text = focGetText(strPath, arrFileNames)
  lines = arhSplitString(text)
  if (bolReverseOrder) then
    lines = arhRemoveLastIfEmpty(lines)
    lines = arhReverse(lines)
  end if
  fnsSourceLines = lines
End Function

' Returns true if there are no lines left, i.e. if the source text
' is empty or non existant or if all lines have been returned,
' false otherwise
Function fnsNoMoreNames( )
  dim nomore
  nomore = (fnsCurrentLine > Ubound(fnsSourceLines))
  if (nomore) then
    eioPrint "No more names in source text"
  end if
  fnsNoMoreNames = nomore
End Function

' Returns the next line from the source text
Function fnsGetNextName( )
  if (fnsCurrentLine > Ubound(fnsSourceLines)) then
    fnsGetNextName = ""
  else
    fnsGetNextName = fnsSourceLines(fnsCurrentLine)
    fnsCurrentLine = fnsCurrentLine + 1
  end if
End Function
' ************************************************************************

' *********************** File or clipboard module ***********************
dim focFileNameUsed

' Reads text from files in the given string array or from clipboard,
' it will return the first one that has content
Function focGetText( strPath, arrFileNames )
  dim file, text
  focFileNameUsed = ""
  for each file in arrFileNames
    text = focGetTextFromFile(strPath & "\" & file)
    if (text <> "") then
      focFileNameUsed = file
      eioPrint "Using the file '" & file & "' as source text"
      exit for
    end if
  next
  if (text = "") then
    text = focGetTextFromClipboard()
    if (text <> "") then
      eioPrint "Using the clipboard as source text"
    else
      eioPrint "No files and nothing in clipboard to use as source text"
    end if
  end if
  focGetText = text
End Function

' Returns the file name used for getting the text, if any
Function focGetUsedFile( )
  focGetUsedFile = focFileNameUsed
End Function

' Reads text from clipboard
Function focGetTextFromClipboard( )
  dim hto, text
  set hto = CreateObject("htmlfile")
  text = hto.parentwindow.clipboardData.GetData("text")
  if (VarType(text) <> vbString) then text = ""
  focGetTextFromClipboard = text
End Function

' Reads text from file, supports UTF-8, UTF-16 and UTF-16 (BE) files
' with proper BOM (Byte Order Mark)
Function focGetTextFromFile( strTextFile )
  dim ado, fso, tmp, bom(3), encoding, file, i
  const adTypeBynary = 1
  const adTypeText = 2
  const adReadAll = -1
  on error resume next
  set ado = CreateObject("ADODB.Stream")
  ado.Open
  ado.Type = adTypeBynary
  ado.LoadFromFile strTextFile
  tmp = ado.Read(3)
  for i = 0 To 2
    bom(i) = AscB(MidB(tmp, i+1, 1))
  next
  ado.Close
  if err.number > 0 then
    focGetTextFromFile = ""
  else
    if (bom(0) = &hEF and bom(1) = &hBB and bom(2) = &hBF) then
      encoding = "utf-8" ' UTF-8
    elseif (bom(0) = &hFE and bom(1) = &hFF) then
      encoding = "unicode" ' UTF-16 big endian
    elseif (bom(0) = &hFF and bom(1) = &hFE) then
      encoding = "unicodeFFFE" ' UTF-16 little endian
    else
      encoding = "default"
    end if
    eioPrint "Loading file using " & encoding & " encoding"
    if (encoding = "default") then
      set fso = CreateObject("Scripting.FileSystemObject")
      set file = fso.OpenTextFile(strTextFile, 1, False)
      if err.number > 0 then
        focGetTextFromFile = ""
      else
        focGetTextFromFile = file.ReadAll()
      end if
    else
      set ado = CreateObject("ADODB.Stream")
      ado.Open
      ado.Type = adTypeText
      ado.CharSet = encoding
      ado.LoadFromFile strTextFile
      if err.number > 0 then
        focGetTextFromFile = ""
      else
        focGetTextFromFile = ado.ReadText(adReadAll)
      end if
    end if
  end if
End Function
' ************************************************************************

' ************************* Array helper module **************************
' Splits a string into an array of strings on line breaks (\r\n, \r or \n)
Function arhSplitString( strText )
  dim text
  text = strText
  text = Replace(text, vbCrLf, vbLf)
  text = Replace(text, vbCr, vbLf)
  arhSplitString = Split(text, vbLf)
End Function

' Returns an array that is in reverse order to the input array
Function arhReverse( ByVal arrIn )
  dim index, maxIndex, out
  maxIndex = Ubound(arrIn)
  if (maxIndex < 1) then
    arhReverse = arrIn
    exit function
  end if
  redim out(maxIndex)
  for index = 0 to maxIndex
    out(maxIndex - index) = arrIn(index)
  next
  arhReverse = out
End Function

' Returns an array where the last element of the input array has
' been removed if it was an empty string
Function arhRemoveLastIfEmpty( ByVal arrIn )
  dim index, maxIndex, out
  maxIndex = Ubound(arrIn)
  if (maxIndex > -1) then
    if (arrIn(maxIndex) = "") then
      maxIndex = maxIndex - 1
      redim out(maxIndex)
      for index = 0 to maxIndex
        out(index) = arrIn(index)
      next
      arhRemoveLastIfEmpty = out
    else
      arhRemoveLastIfEmpty = arrIn
    end if
  else
    arhRemoveLastIfEmpty = arrIn
  end if
End Function
' ************************************************************************

' ****************** Error & information output module *******************
' Prints message to the appropriate output channel
Function eioPrint( strMessage )
  Dopus.OutputString strMessage
  'WScript.Echo strMessage
End Function
' ************************************************************************

#9

[quote=“Joejo”]If there is any BLANK line in the text file, skip the BLANK line.
And read the next NOT BLANK line.[/quote]
I have added this as an option now. Just set optSkipEmptyLines to True.

It’s not a perfect solution because the check is done before replacement of illegal characters. The lines also has to be completely empty, a line consisting of only whitespace would not be ignored. I may rewrite it to include these features in the future.

And I don’t blame you for not figuring it out. I barely understand the code myself :laughing:


#10

Thank you for quick reply!! :thumbsup:

I did it!! :smiley: :smiley:

Many Thanks!!!


#11

Great script, exactly what I was looking for! :smiley:
Thanks a lot,
MartO


#12

Hi,

Bug report [v3.1], I guess.

Let’s say I have those lines as source:

test 01.avi
test 02.srt
test 03.txt

It works great if they are in Clipboard Text.txt but completely fails if the exact same lines are in the clipboard:

From file rename script by Henrik Ripa
No files and nothing in clipboard to use as source text
No more names in source text
No more names in source text
No more names in source text

Can someone confirm this bug.

My use will only be from clipboard so I can’t use it as it is now :confused:


#13

Hi fred,
no, I can’t confirm this; here everything works fine.
I have these 3 files:

file1.avi
file2.srt
file3.txt

I copy the following lines to the clipboard:

test 01.avi
test 02.srt
test 03.txt

After renaming the result is:

test 01.avi.avi
test 02.srt.srt
test 03.txt.txt

This looks strange but since the script leaves the file extension alone and the source lines contain additional file extensions, it works exactly as described.
MartO


#14

Hmm, I can’t reproduce this either. It works for me on Windows 7 x64 with DOpus 10.1.0. Unfortunately, I don’t have a Windows XP machine to test with.

It looks like the script doesn’t recognize what is in the clipboard as text. But I have no idea why.


#15

Thanks for report.

The problem comes from the focGetTextFromClipboard function. I’m investigating why the method fails on my XP SP3 (IE8) :confused:

If someone could test it on similar OS…


#16

Ok, it works now. The problem came from my security settings (xp-antispy or comodo). I’ll post as soon as I find out the guilty.


#17

Ok, I found out the guilty.

In xp-antispy, if the javascript for IE is disabled focGetTextFromClipboard fails to access clipboard content.

So I have to enable Script in IE now. Although I use firefox, I would like to turned JS in IE off for security purpose, external programs can access it. It would be great that opus has a clipboard object that could be used in VBscript. :smiley:


#18

Interesting. I tried to turn off scripting in explorer but it didn’t seem to affect the clipboard in VBscript for me. I guess it works differently in Windows 7.


#19

I just checked that on IE8.

It seems that the JS deactivation option in xp-antispy disables all objects that are in IE option > security tab > Internet > custom level > script included the clipboard access setting in there. This is the guilty.

Opus must be restart once you have made the changes in IE.


#20

Just a quick post to say THANK YOU. I wish I had seen this years ago - it’s obscenely useful :slight_smile: