title: Using Replace function in VBScript to rename a group of folders
body: <p>I'm trying to create a script which will insert 2 characters into the names of a group of subfolders in a folder. Here are the details of these folders:</p>
  
  <ul>
  <li><p>Each folder begins with a 14-digit number and an underscore (ex. 60818400200000_)</p></li>
  <li><p>The folder names are of various lengths</p></li>
  <li><p>Some folder names have additional underscores in their names</p></li>
  </ul>
  
  <p>The script needs to insert a dash and an underscore (-_) after the first underscore, or replace the first underscore with an underscore-dash-underscore (_-_).</p>
  
  <p>Each folder afterwards should look like [14-digit number]_-_[rest of file name].</p>
  
  <p>Here is what I've come up with so far:</p>
  
  <pre><code>Dim objFso, folder, fc, rname as string
  
  Set objFso = CreateObject("Scripting.FileSystemObject")
  Set folder = objFso.GetFolder("F:\Eagle_Ford_20130220\TO_LOAD")
  Set fc = folder.SubFolders
  
  Set rname = Mid(folder.Name,15,1)
  
  For each folder in fc
  
      If rname = "_" Then
          rname = rname.replace("_","_-_")
  
      End If
  
  
  Next
  
  Wscript.Echo "Finished"
  </code></pre>
  
  <p>However, whenever I run this script I end up with some kind of error message. If I don't define the rname as a string, I end up with an "Object Required" error message. If I define the rname as a string (as is shown in the script above), I end up with an "Expected end of statement" error.</p>
  
  <p>I've looked everywhere on the web to figure out what I'm doing wrong. Would someone here be willing to point me in the right direction? I would be ever so grateful! :)</p>
  
  <p>EDIT:</p>
  
  <p>After getting some direction from Rory, I modified my script slightly to this:</p>
  
  <pre><code>Dim objFso, folder, sfolder, rname
  
  Set objFso = CreateObject("Scripting.FileSystemObject")
  Set folder = objFso.GetFolder("F:\Eagle_Ford_20130220\TO_LOAD")
  Set sfolder = folder.SubFolders
  
  
  For each n in sfolder
  
  rname = Mid(folder.Name,15,1)
  
      If rname = "_" Then
          folder.name = rname.replace("_","_-_")
  
      End If
  
  
  Next
  
  Wscript.Echo "Finished"
  </code></pre>
  
  <p>The main changes were to rename a couple of my variables and remove the Set and string definition for the rename variable. I also moved my rname variable within the For Each statement, which eliminated the Object error I kept getting.</p>
  
  <p>The script runs, but no changes are made to the subfolders in the folder listed in the script. I've tested the For Each statement using an Echo command to make sure it was picking up each of the folders (it is). The only thing I can conclude is that there is something wrong with my If statement.</p>
  
  <p>I've ran my script through a couple of debuggers, but get no error messages. I would really appreciate any additional assistance, especially since coding is not my strong suit (obviously).</p>