VBScript - Renaming files using replace function and getbasename method
<p>I don't have any problem when prepending characters or strings to a filename using the <code>Replace</code> function e.g.</p>

<pre><code>j = prependstring
for each i in folder.files    
k = len(i.name)
i.name = replace(i.name, i.name, j &amp; left(i.name, k))
</code></pre>

<p>Nor for that matter appending e.g.</p>

<pre><code>j = appendstring
for each i in folder.files   
ext = fso.getextensionname(i)
k = instrrev(i.name,".")-1
i.name = replace(i.name, i.name, left(i.name, k) &amp; " " &amp; j &amp; "." &amp; ext)
</code></pre>

<p>However, using the following code,  if I try replace the entire <code>base name</code>, with a version where the leading letter of each word is capitalized I get a "File already exists" error.</p>

<pre><code>option explicit
dim fso, folder, wshell, strpath, strname, i, abspath, base, ext, capstr

set fso = createobject("scripting.filesystemobject")
set folder = fso.getfolder(".")

set wshell = createobject("wscript.shell")
strpath = wscript.scriptfullname
strname = fso.getfilename(strpath)

function wordcaps(strinput)
dim x 
for x = asc("a") to asc("z") 
strinput = replace(strinput, " " &amp; chr(x), " " &amp; ucase(chr(x))) 
next 
wordcaps = ucase(left(strinput, 1)) &amp; mid(strinput, 2) 
end function 

for each i in folder.files
if i.name &lt;&gt; strname then
base=fso.getbasename(i)
capstr = wordcaps(base)
'wscript.echo capstr
i.name = replace(i.name, base, capstr)
end if
next
wscript.echo "Done!"
</code></pre>

<p>I can achieve the same objective using <code>MoveFile</code> method as shown below:</p>

<pre><code>for each i in folder.files
if i.name &lt;&gt; strname then
abspath=fso.getabsolutepathname(i)
base=fso.getbasename(i)
ext=fso.getextensionname(i)
capstr = wordcaps(base)
fso.movefile abspath, folder &amp; "\" &amp; capstr &amp; "." &amp; ext
end if
next
wscript.echo "Done!"
</code></pre>

<p>I can't understand why does the <code>replace</code> version does not work?</p>