Re: rename files in directory



Thank you Pegasus,
When I run your script I get an error: " Microsoft VBScript runtime error:
Invalid procedure call or argument: 'left' " Any Idea's why? All I changed
was the first line. I changed it to the path that contained to files I wanted
to rename.

v/r
john

"Pegasus (MVP)" wrote:


"John" <John@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:8B0C55BB-52D6-42BF-96E5-2DB7B4E8EE93@xxxxxxxxxxxxxxxx
Hello, Someone on the windows xp newsgroup said I should post this on the
vb
script group.
I have a directory that list hundreds of files with a name in this format:
"Name of file - 0001.pdf" I would like to create a command that would
switch
the number of the file to the beginning of the file name. ie. "0001 - name
of
file.pdf" This command should do this for each file in the directory. I
figure I should be able to do it with a for command but can not figure out
how to start. Could anyone get me started?

v/r
john


You could use this script:

const strSourceDir = "'D:\Tue'"
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")

Set colFiles = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name=" & strSourceDir _
& "} Where ResultClass = CIM_DataFile")

For Each objFile In colFiles
p = InStr(objFile.FileName, " - ")
strName = Left(objFile.FileName, p-1) & "."
strNumber = Right(objFile.FileName, 4) & " - "
strNewName = objFile.Drive & objFile.Path _
& strNumber & strName & objFile.extension
errResult = objFile.Rename(strNewName)
Next

Watch for the single+double quotes around strSourceDir
and try the script on a sample folder first!

The script should work well for small folders. For larger
folders a different approach would be more appropriate.



.



Relevant Pages

  • Folder size script
    ... I'm running a vbs script that gets the size of folders on another server. ... Set objFSO = CreateObject ... Microsoft VBScript runtime error: ...
    (microsoft.public.scripting.vbscript)
  • Re: Object not a collection?
    ... Source: Microsoft VBScript runtime error ... Const REG_SZ = 1 ... Using your script, I get, for .txt, three ...
    (microsoft.public.scripting.vbscript)
  • Re: How to display a picture by using an Access database in FrontPage 2003
    ... Microsoft VBScript runtime error '800a000d' ... Microsoft MVP - FrontPage ... yes I did change the actual field name in the script to the ...
    (microsoft.public.frontpage.programming)
  • Re: Failed to initialize all required WMI classes
    ... > I have the following messages in WMI Control Properties: ... > I tried to google for some hints to resolve this problem, ... I attach this tiny script I wanted to ... > Microsoft VBScript runtime error ...
    (microsoft.public.windowsxp.wmi)
  • Re: List group membership
    ... but I get an error when trying to run the script. ... It gives me: Microsoft VBScript runtime error: Object not a collection. ... membership. ...
    (microsoft.public.scripting.vbscript)