0

I have a script that changes particular string within files names (the file stores in "my_folder"):

Set objFso = CreateObject("Scripting.FileSystemObject")
Set Folder = objFSO.GetFolder("g:\my folder")
For Each File In Folder.Files
sNewFile = File.Name
sNewFile = Replace(sNewFile,"._epf","_v0_1._epf")
if (sNewFile<>File.Name) then
    File.Move(File.ParentFolder+"\"+sNewFile)
end if
Next

the scrpit works fine if there are no folders under "g:\my folder", otherewise, if there are folders in "my folder" and the name of one (or more) of those folders are similiar to some file name, the scrip cause unwanted results like multiplying the replace string.

for example if "my folder" contain:

hello (folder)

hello_.epf (file)

then the script will eventually change the file name to:

hello_v0_1_v0_1._epf (unwanted result)

and i want the result to be:

hello_v0_1._epf

I'll appreciate quick help in this manner. thanks.

Community
  • 1
  • 1

1 Answers1

0

I haven't bothered to try to figure out where your VBScript is going wrong. But you tagged your question with batch-file, batch, and batch-rename.

Here is a simple one-liner that can be run from the command prompt that will do what you want. It doesn't even need a batch script.

for %F in ("g:\my folder\*._epf") do @ren "%F" "%~nF_v0_1%~xF"

If you want to run the command within a batch script, then you need to double all percents.

@echo off
for %%F in ("g:\my folder\*._epf") do ren "%%F" "%%~nF_v0_1%%~xF"

EDIT

The above will append a new version suffix to each file name, before the extension.

If you want to replace an existing version number, then the solution is even easier. I'm assuming that your version always starts with _v, and v will never occur in the file extension.

ren "g:\my folder\*_v0_1._epf" "*v0_2.*"

The above command renames all files that end with _v0_1._epf. It preserves all characters up through the last occurance of v in the name, then adds the new version number, and finally appends the original extension.

See How does the Windows RENAME command interpret wildcards? for rules on how REN uses wildcards.

Community
  • 1
  • 1
dbenham
  • 127,446
  • 28
  • 251
  • 390
  • this solution is not enough because i need to make occasionaly rename "v0_1" to "v0_2" "v0_3".... "v1_0", "v1_1"... and so on. (the ending mentioning the version of the file) . – terminetorx Dec 05 '12 at 05:13
  • I tagged "batch-file" from the reason that a solution in batch will also be appropriate (if there is no solution in vbscript). – terminetorx Dec 05 '12 at 05:20
  • @terminetorx - I can't read your mind ;) I've extended my answer to handle your newly stated requirement. – dbenham Dec 05 '12 at 05:56
  • I used your solution from the post of renaming directories [link](http://stackoverflow.com/questions/13699785/batch-search-and-replace-folders-names) - i wrote: @ren "g:\my folder\%~1.vhd" "%~1_v0_1.vhd" and it worked on the file too (when i added the extensions). Thanks man. – terminetorx Dec 06 '12 at 11:44