?
Solved

Script to change file name

Posted on 2007-10-19
5
Medium Priority
?
1,350 Views
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(oFile.name, Prefix)
 if(i < 1)then
   oFile.name = Prefix + oFile.name
 end if
Next

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?
0
Comment
Question by:Kimberly1467
4 Comments
 
LVL 13

Accepted Solution

by:
rettiseert earned 1000 total points
ID: 20109098
Try changing...
   
oFile.name = Prefix + oFile.name

To:

On error resume next
oFile.name = Prefix + oFile.name
On error goto 0
0
 
LVL 30

Assisted Solution

by:SteveGTR
SteveGTR earned 1000 total points
ID: 20109100
For Each oFile in fileList
 Dim i
 i = instr(oFile.name, Prefix)
 if(i < 1)then
   if not fso.FileExists(Prefix + oFile.name) then
     oFile.name = Prefix + oFile.name
   end if
 end if
Next

0
 
LVL 3

Expert Comment

by:elissaqp
ID: 20113674
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
Next
0
 
LVL 1

Expert Comment

by:Computer101
ID: 21046413
Forced accept.

Computer101
EE Admin
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses
Course of the Month16 days, 19 hours left to enroll

864 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