title: Getting "Bad Filename or Number" when renaming file with a variable for the destination filename
body: <p>I am trying to loop a folder full of .html files and add some code at the beginning of the files (although I am getting some unwanted line breaks before the code I am inserting) and also to grab the contents of the <code>&lt;title&gt;</code> tag and use this for the renaming each file.</p>
  
  <p>I am replacing the spaces and unwanted characters with -'s</p>
  
  <p>All of this works but I am also trying to rename the existing file (<code>Default0010.html</code> is one example) to the text from the <code>&lt;title&gt;</code>.</p>
  
  <p>This works too but when I am trying to move the existing file to the new file I get a <code>Bad File name or Number</code> but when I explicilty set the destination file name to a simple string it works.</p>
  
  <p>It makes me thing my string is not clean or you cannot use a variable for the destination.</p>
  
  <p>Also please ignore the lines <code>Dim i</code>, <code>i = i + 1</code> and <code>If i=1 Then Exit For</code>.</p>
  
  <p>This was added whilst I test the script then when  I was happy it does what I wanted I would run it on all the HTML files.</p>
  
  <pre><code>Set objFso = CreateObject("Scripting.FileSystemObject")
  Set Folder = objFSO.GetFolder("C:\My Web Sites\test\www.test.org.uk\html")
  
  Dim i
  
  Dim ObjFsoFile
  Dim ObjFile
  Dim StrData
  Dim StrTitleTag
  Dim OldFilename
  Dim NewFilename
  Set ObjFsoFile = CreateObject("Scripting.FileSystemObject")
  
  'Loop all of the files
  For Each File In Folder.Files
    'Get contents of the file and store in a string
    'Opening the file in READ mode
    Set ObjFile = ObjFsoFile.OpenTextFile(File.Name)
  
    'Reading from the file
    StrData = ObjFile.ReadAll
    'Add the Perch include to the beginning
    StrData = replace(StrData,"&lt;?php include('cms/runtime.php');?&gt;","") 'Remove the Perch include in-case we are re-running this
    StrData = replace(StrData,"&lt;!DOCTYPE HTML PUBLIC " &amp; Chr(34) &amp; "-//W3C//DTD HTML 4.0 Transitional//EN" &amp; Chr(34) &amp; "&gt;","&lt;?php include('cms/runtime.php');?&gt;" &amp; vbcrlf &amp; "&lt;!DOCTYPE HTML PUBLIC " &amp; Chr(34) &amp; "-//W3C//DTD HTML 4.0 Transitional//EN" &amp; Chr(34) &amp; "&gt;")     
    'Msgbox StrData
  
    'Closing the file
    ObjFile.Close
  
    'Write the changes to the current file
    Set objFile = objFSO.CreateTextFile(File.Name,True)
    objFile.Write StrData
    objFile.Close
  
    'Re-write the contents of the current file and replace with the StrData Above
  
    'Grab the contents between &lt;title&gt; and &lt;/title&gt;
  
    parse_string1 = StrData 'see above post 
    parse_string1 = replace(parse_string1,"&lt;title&gt;","¦") 
    parse_string = split(parse_string1,"¦") 
    parse = parse_string(1) 
    parse_string1 = replace(parse,"&lt;/title&gt;","¦") 
    parse_string = split(parse_string1,"¦") 
    parsed_string = parse_string(0)
  
    StrTitleTag = parsed_string 'gives final result
  
    'Save old filename of current file to a string
    OldFilename = File.Name
    'Msgbox OldFilename
  
    'Rename current file to the above contents of between &lt;title&gt; and &lt;/title&gt;
    'Replace spaces with - characters in the filename.
  
    Dim divider
    divider = "-"
  
    'Replace &amp; with and
    NewFilename = Replace((StrTitleTag &amp; ".php"),"&amp;","and")
    'Replace triple space with single space     
    NewFilename = Replace(NewFilename,"   "," ")
    'Replace double space with single space
    NewFilename = Replace(NewFilename,"  "," ")
    'Replace - with space
    NewFilename = Replace(NewFilename," ",divider)
    'Replace ---- with -
    NewFilename = Replace(NewFilename,divider &amp; "-" &amp; divider,divider)      
    'Replace ---- with -
    NewFilename = Replace(NewFilename,divider &amp; divider &amp; divider,divider)          
    'Replace ,- with -
    NewFilename = Replace(NewFilename,"," &amp; divider,divider)
    'Replace LineBreaks with nothing (remove line breaks)
    NewFilename = Replace(NewFilename,vbCrLf,"")    
    NewFilename = Replace(NewFilename,vbLf,"")  
    NewFilename = Replace(NewFilename,vbCr,"")  
    NewFilename = LCase(NewFilename)
    'Msgbox NewFilename
  
    'Loop through all files
    For Each File2 In Folder.Files
      'Opening the file in READ mode
      Set ObjFile = ObjFsoFile.OpenTextFile(File2.Name)
  
      'Get contents of the file and store in a string         
      'Reading from the file
      StrData = ObjFile.ReadAll
  
      'Closing the file
      ObjFile.Close
  
      'Replace all occurences of the old filename with the new filename
      StrData = Replace(StrData, OldFilename, NewFilename)
  
      'How to write file
      Set objFile = objFSO.CreateTextFile(File2.Name,True)
      objFile.Write StrData
      objFile.Close
    Next
  
    'Rename Old file with the new filename  
    If objFso.FileExists("C:\My Web Sites\test\www.test.org.uk\html\" &amp; OldFilename) Then
      'NewFileName = "test.php"
      'NewFileName = "test-test-test-test-test-test-test-test-test.php"
      Msgbox "Renaming the file " &amp; OldFilename &amp; " (Length: " &amp; Len(OldFilename)     &amp; ") with the following name: " &amp; NewFilename &amp; " (Length: " &amp; Len(NewFilename) &amp; ")"
      Msgbox "Compare: test-test-test-test-test-test-test-test-test.php " &amp; NewFilename
      objFso.MoveFile "C:\My Web   Sites\test\www.test.org.uk\html\" &amp; OldFilename, "C:\My Web     Sites\test\www.test.org.uk\html\" &amp; NewFileName
    End If
  
    i = i + 1
    If i=1 Then Exit For
  Next
  </code></pre>