Re: rename files in directory



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: Testing If Active Directory Replication Has Occurred
    ... solution is to use a global catalog query instead of an LDAP query and ... > the root domain on a specific user account that has just been created ... > means that when we create a new user, the script needs to also create ... > folder redirection works fine. ...
    (microsoft.public.scripting.vbscript)
  • Re: recursive file editing
    ... > 2) each file examined should be edited; IF a string of this type is found ... > the edited output file should also be renamed according to the same rule ... script with the directory you want to process. ... for folder, folders, files in os.walk: ...
    (comp.lang.python)
  • 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. ...
    (microsoft.public.scripting.vbscript)
  • Re: OT:macros or software?
    ... when a .PDF file is stored there, ... words and depending on weather those words are found in that document, move it to a predetermined folder. ... Easy enough if it were a text document, or even Word, as you could write a DOS script, or a VBS script if you had to load Word to read Word documents, although writing such a script can be harder to a beginner than some might think. ... If the string was in the name of the file it would be less work - you can set up the rather marvellous "robocopy" but that won't read inside a file, and certainly not a pdf! ...
    (uk.comp.homebuilt)
  • Re: Search data with vbscript
    ... folder, the string to search for, and if sub folders are to be searched. ... You could modify the script to instead load the files into ... Notepad before opening the next file. ...
    (microsoft.public.scripting.vbscript)