Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stange file renaming problem.

Status
Not open for further replies.

freavis

MIS
Feb 5, 2002
19
0
0
US
I have a script that goes through a folder and finds all files with a .kof extension. I then add a timestamp on the filename. ex.(test.kof to test2005082411.kof)
However, I am getting mixed results on some systems this runs on. On some systems it works fine and others it is renaming way to many times. This is what it looks like on those systems. ex.(test.kof to test2005082411200508241120050824112005082411.kof) It acts like it keeps going through the same directory and finding the file over and over again and keeps renaming it. Sometimes twice sometimes 15 times. Does anybody have any suggestions? Thanks

Test Code below.
_________
Dim oFSO, stagefolder, stagefilelist, koffile, FileExt, FileBase, NewFileName

Set oFSO = CreateObject("Scripting.FileSystemObject")

Set stagefolder = oFSO.GetFolder("C:\test")


Set stagefilelist = stagefolder.Files
timestamp = Year(Now) & Month(Now) & Day(Now) & Hour(Now) & Minute(Now) & Second(Now)



For Each koffile in stagefilelist
FileExt = oFSO.GetExtensionName(koffile)
If (FileExt = "kof") Then
FileBase = oFSO.GetBaseName(koffile)
'WScript.Echo FileBase
NewFileName = FileBase & timestamp & "." & FileExt
'WScript.Echo NewFileName
oFSO.MoveFile koffile, stagefolder & "\" & NewFilename
End If
Next

WScript.Quit
___________
 
When you change the name (or delete) use an counterdown indice instead of the For Each instruction.


Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
PHV - Thanks for the quick reply. I'm not sure I understand what you are saying to do. Could you maybe give a simple example of what you mean?

Thanks,
Frank
 
>...renaming way to many times
The reason that happens is that the file collection is a dynamic thing. When you rename or move or delete some object, the collection can be adjusted realtime perturbing the original serialization/numbering. For file system, do something like this.
[tt]
Dim oFSO, stagefolder, stagefilelist, koffile, FileExt, FileBase, NewFileName[blue], afile(), i[/blue]

Set oFSO = CreateObject("Scripting.FileSystemObject")

Set stagefolder = oFSO.GetFolder("C:\test")

Set stagefilelist = stagefolder.Files
[blue]
redim afile(Stagefilelist.count-1)
i=0[/blue]
For Each koffile in stagefilelist
[blue] i=i+1
afile(i)=koffile.path
Next[/blue]

timestamp = Year(Now) & Month(Now) & Day(Now) & Hour(Now) & Minute(Now) & Second(Now)

For i=0 to ubound(afile)
[blue]set koffile=oFSO.getfile(afile(i))[/blue]
[blue]'[/blue]FileExt = oFSO.GetExtensionName(koffile) [blue]'you are using default property, can be misleading[/blue]
FileExt = oFSO.GetExtensionName(koffile[red].path[/red]) [blue]'or .name if you like[/blue]
If (FileExt = "kof") Then
FileBase = oFSO.GetBaseName(koffile[red].path[/red]) [blue]'or .name if you like[/blue]
'WScript.Echo FileBase
NewFileName = FileBase & timestamp & "." & FileExt
'WScript.Echo NewFileName
[blue]'[/blue]oFSO.MoveFile koffile, stagefolder & "\" & NewFilename
[blue]'instead of movefile, you can simple reassign name
koffile.name=NewFileName[/blue]
End If
Next

WScript.Quit
[/tt]
 
Amendment: The corresponding line should be read
[tt]i=[red]-1[/red][/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top