Script to change file name

Posted on 2007-10-19
Last Modified: 2008-03-04
I have the following script, which changes the file name.
Const FullPath = "C:\test"
Const Prefix = "Han-Solo"
Dim fso, directory, fileList, oFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set directory = fso.GetFolder(FullPath)
Set fileList = directory.files

For Each oFile in fileList
 Dim i
 i = instr(, Prefix)
 if(i < 1)then = Prefix +
 end if

This script has been add to a server as a scheduled task to run daily. The script does not work when there is a file has the same name. For example IIS Log files has a file two files name ex071023, so when the script runs it says the file already exist. I need to alter the script so that it skips files with the same name and renames the others. How can I do this?
Question by:Kimberly1467
    LVL 13

    Accepted Solution

    Try changing...
   = Prefix +


    On error resume next = Prefix +
    On error goto 0
    LVL 30

    Assisted Solution

    For Each oFile in fileList
     Dim i
     i = instr(, Prefix)
     if(i < 1)then
       if not fso.FileExists(Prefix + then = Prefix +
       end if
     end if

    LVL 3

    Expert Comment

    This is a simple example you can check:

    strComputer = "."
    Set objWMIService = GetObject _
        ("winmgmts:" & "!\\" & strComputer & "\root\cimv2")
    Set colFiles = objWMIService.ExecQuery _
        ("Select * from Cim_Datafile where Name = " _
            & "'c:\\scripts\\toggle_service.vbs'")
    For Each objFile in colFiles
        errResult = objFile.Rename("c:\scripts\toggle_service.old")
        Wscript.Echo errResult
    LVL 1

    Expert Comment

    Forced accept.

    EE Admin

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
    This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now