Re: rename files in directory



Thanks Tom, Actually the script had a space in between " - " but my file
names did not have that space thanks for your help!

v/r
john

"Tom Lavedas" wrote:

On Feb 7, 1:10 pm, John <J...@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
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" <J...@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'"
{snip}

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.

You will get that error when the name of the file returned does NOT
contain a " - " string. I would also suspect this may be due to an
error in the replacement of the string in the first line:

const strSourceDir = "'D:\Tue'"

NOTE that Pegasus (as he told you) is including the single quote marks
that are needed in the later query as part of this string rather than
as part of the query (what I would do). If you missed that point and
did not surround your actual path with them, then the query returned
an empty result.

Finally, if the target folder has files with names in a format other
than the one you specified, then the variable "p" will return zero and
the subsequent statement will throw an error.

Put a test for "p" and fix the construction of the strSourceDir
string, say like this ...

const strSourceDir = "D:\Tue" ' note I moved the single quotes to the
query
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, " - ")
if p > 2 Then
strName = Left(objFile.FileName, p-1) & "."
strNumber = Right(objFile.FileName, 4) & " - "
strNewName = objFile.Drive & objFile.Path _
& strNumber & strName & objFile.extension
errResult = objFile.Rename(strNewName)
end if
Next

Tom Lavedas
===========
http://members.cox.net/tglbatch/wsh/

.



Relevant Pages

  • Re: comma in a list box
    ... Thanks John, but I want to avoid using another table or a query if I can. ... The text goes into the list box from string variables. ... Static qNum As Long ...
    (microsoft.public.access.formscoding)
  • Re: rename files in directory
    ... When I run your script I get an error: " Microsoft VBScript runtime error: ... contain a " - " string. ... as part of the query. ... if the target folder has files with names in a format other ...
    (microsoft.public.scripting.vbscript)
  • Re: Cant figure out how to use [ Querystring(arg) ] in data view
    ... string seems like the answer at first glance. ... > This is easiest to accomplish by using Web Part connections instead of the ... > John Jansen ... >> query string. ...
    (microsoft.public.sharepoint.windowsservices)
  • Re: Decision Tables
    ... <sniped code to store an execute a match query> ... field_match: string = string ... not_expr: ... "John Smith England\n" ...
    (comp.programming)
  • Multi field search
    ... Is there a way to construct one query that will search across more than one ... I want to find all instances of the word string "John" which can ... John Johnson ...
    (microsoft.public.access.forms)