Re: rename files in directory
- From: Tom Lavedas <tglbatch@xxxxxxx>
- Date: Thu, 7 Feb 2008 10:51:52 -0800 (PST)
On Feb 7, 1:10 pm, John <J...@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
Thank you Pegasus,{snip}
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'"
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/
.
- Follow-Ups:
- Re: rename files in directory
- From: John
- Re: rename files in directory
- References:
- Re: rename files in directory
- From: Pegasus \(MVP\)
- Re: rename files in directory
- From: John
- Re: rename files in directory
- Prev by Date: Re: rename files in directory
- Next by Date: Re: Quotes using WMI SQL Statement
- Previous by thread: Re: rename files in directory
- Next by thread: Re: rename files in directory
- Index(es):
Relevant Pages
|