<?xml version="1.0" encoding="UTF-8"?>
<rename_preset case="none" group="My" preset="40 从剪贴板导入文件名" script="yes" type="regexp" version="13">
	<from>^.+(\..*)$|^.*$</from>
	<to>FROMFILE\1</to>
	<script>@script vbscript
option explicit

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

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

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

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

&apos; ******************************* Options ********************************
dim optReverseOrder, optFileNames, optMagicMarker, optIllegalReplacement
dim optIllegalCharacters, optUseEmptyLines, optSkipEmptyLines

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

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

&apos; Magic marker to replace with names from file
optMagicMarker = &quot;FROMFILE&quot;

&apos; What to replace illegal file name characters with
optIllegalReplacement = &quot;&quot;

&apos; Pattern of illegal file name characters
optIllegalCharacters = &quot;[/\\:*?&lt;&gt;\|&quot;&quot;]&quot;

&apos; Whether to use empty lines in the source or not
optUseEmptyLines = False

&apos; Whether to skip empty lines in the source or not
optSkipEmptyLines = False
&apos; ************************************************************************

&apos; ************************ DOpus rename function *************************
&apos; This is the function that is called by DOpus to get a new file name,
&apos; 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
&apos; ************************************************************************

&apos; ********************* Directory Opus rename module *********************
dim dorInitialized
dorInitialized = False

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

  &apos; Print script identification
  eioPrint &quot;From file rename script by Henrik Ripa&quot;

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

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

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

  &apos; Exit if there are nothing to rename with
  if (fnsNoMoreNames()) then exit function

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

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

  &apos; Get next file name from source
  line = fnsGetNextName()

  &apos; Clean file name from unwanted characters
  line = ricReplaceCharacters(line)

  &apos; Assemble new file name, if it isn&apos;t empty
  if (optUseEmptyLines or line &lt;&gt; &quot;&quot;) then
    dorGetNewName = Replace(strMidName, optMagicMarker, line)
  end if
End Function
&apos; ************************************************************************

&apos; ****************** Replace illegal characters module *******************
dim ricRegexp, ricReplacement

&apos; 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

&apos; Replaces illegal characters in the text with the replacement string
Function ricReplaceCharacters( strText )
  dim text
  text = ricRegexp.Replace(strText, ricReplacement)
  if (strText &lt;&gt; text) then
    eioPrint &quot;Replaced illegal characters in &quot;&quot;&quot; &amp; strText &amp; &quot;&quot;&quot;&quot;
  end if
  ricReplaceCharacters = text
End Function
&apos; ************************************************************************

&apos; *********************** File names source module ***********************
dim fnsSourceLines, fnsCurrentLine

&apos; Initializes the file names source by getting the source text
&apos; 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

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

&apos; Returns the next line from the source text
Function fnsGetNextName( )
  if (fnsCurrentLine &gt; Ubound(fnsSourceLines)) then
    fnsGetNextName = &quot;&quot;
  else
    fnsGetNextName = fnsSourceLines(fnsCurrentLine)
    fnsCurrentLine = fnsCurrentLine + 1
    if (optSkipEmptyLines and fnsGetNextName = &quot;&quot;) then
      fnsGetNextName = fnsGetNextName( )
    end if
  end if
End Function
&apos; ************************************************************************

&apos; *********************** File or clipboard module ***********************
dim focFileNameUsed

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

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

&apos; Reads text from clipboard
Function focGetTextFromClipboard( )
  dim hto, text
  set hto = CreateObject(&quot;htmlfile&quot;)
  text = hto.parentwindow.clipboardData.GetData(&quot;text&quot;)
  if (VarType(text) &lt;&gt; vbString) then text = &quot;&quot;
  focGetTextFromClipboard = text
End Function

&apos; Reads text from file, supports UTF-8, UTF-16 and UTF-16 (BE) files
&apos; 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(&quot;ADODB.Stream&quot;)
  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 &gt; 0 then
    focGetTextFromFile = &quot;&quot;
  else
    if (bom(0) = &amp;hEF and bom(1) = &amp;hBB and bom(2) = &amp;hBF) then
      encoding = &quot;utf-8&quot; &apos; UTF-8
    elseif (bom(0) = &amp;hFE and bom(1) = &amp;hFF) then
      encoding = &quot;unicode&quot; &apos; UTF-16 big endian
    elseif (bom(0) = &amp;hFF and bom(1) = &amp;hFE) then
      encoding = &quot;unicodeFFFE&quot; &apos; UTF-16 little endian
    else
      encoding = &quot;default&quot;
    end if
    eioPrint &quot;Loading file using &quot; &amp; encoding &amp; &quot; encoding&quot;
    if (encoding = &quot;default&quot;) then
      set fso = CreateObject(&quot;Scripting.FileSystemObject&quot;)
      set file = fso.OpenTextFile(strTextFile, 1, False)
      if err.number &gt; 0 then
        focGetTextFromFile = &quot;&quot;
      else
        focGetTextFromFile = file.ReadAll()
      end if
    else
      set ado = CreateObject(&quot;ADODB.Stream&quot;)
      ado.Open
      ado.Type = adTypeText
      ado.CharSet = encoding
      ado.LoadFromFile strTextFile
      if err.number &gt; 0 then
        focGetTextFromFile = &quot;&quot;
      else
        focGetTextFromFile = ado.ReadText(adReadAll)
      end if
    end if
  end if
End Function
&apos; ************************************************************************

&apos; ************************* Array helper module **************************
&apos; 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

&apos; 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 &lt; 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

&apos; Returns an array where the last element of the input array has
&apos; been removed if it was an empty string
Function arhRemoveLastIfEmpty( ByVal arrIn )
  dim index, maxIndex, out
  maxIndex = Ubound(arrIn)
  if (maxIndex &gt; -1) then
    if (arrIn(maxIndex) = &quot;&quot;) 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
&apos; ************************************************************************

&apos; ****************** Error &amp; information output module *******************
&apos; Prints message to the appropriate output channel
Function eioPrint( strMessage )
  Dopus.OutputString strMessage
  &apos;WScript.Echo strMessage
End Function
&apos; ************************************************************************
</script>
</rename_preset>
