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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error: File already exists

Status
Not open for further replies.

jrjacobs

IS-IT--Management
Dec 5, 2002
11
0
0
Code 800A003A

I have a script that randomly renames the first three charachters of files to allow a random play order. It worked before the latest updates but does not now.

Can anybody help?

--------------script below----------------

const FilesSubFolders="samples\Music"
Napis = "Run InFlight Music Randomizer for IL2 ?" & chr(10) & "(use parameter /s to suppress messages)"
Titulek = "Windows Scripting Host"

Dim intDoIt
Dim fso, fold, ffil, ffilact, num
Dim ShowMessages

'Parameter /s will run it silently

Messages=True
FileCounter=0

'Get commandline arguments.
set objArgs = WScript.Arguments
'WScript.Echo "Total number of arguments: " & WScript.Arguments.Count
for each strArg in objArgs
'WScript.Echo strArg
If Ucase(StrArg) = "/S" or Ucase(StrArg) = "/SILENT" then
Messages = False
End if
next


'Create a loop in which we will check all subfolders,
'masked "samples\Music\InFlight*"

Randomize
If Messages then Call Welcome()

'List of Subfolders
Dim sFso
Dim sFolder

Set sFso = CreateObject("Scripting.FileSystemObject")
'Collection of subfolders in the subdir Music
Set sFolder = sfso.GetFolder(FilesSubFolders)

'Loop all folders
For Each item in sFolder.Subfolders
'msgbox item.Name
'If the folder name starts with 'InFlight', then renames files in it
If ucase(left(item.name,Len("Inflight")))=Ucase("Inflight") then
'msgbox FilessubFolders & "\" & Item.Name
RenameFilesThisFolder( FilessubFolders & "\" & Item.Name)
End if
Next
If Messages then msgbox "Run IL2 and Enjoy"
Wscript.Quit

Msgbox "Should not see me!"

Sub RenameFilesThisFolder(FilesSubdir)
Set fso = CreateObject("Scripting.FileSystemObject")
Set fold = fso.GetFolder(Fso.getAbsolutePathName("")& "\" & Filessubdir)
'Collection of files in this Folder
Set ffil = fold.Files

'Loop all files
For Each ffilact in ffil
'Randomize a number
num=RandoChars()
ffilact.move(fold.path & "\" & string(3-len(cstr(num)),"0") & cstr(num) & right(ffilact.name,len(ffilact.name)-3))
Next
End sub

Sub Welcome()
intDoIt = MsgBox(Napis, vbOKCancel + vbinformation, Titulek )
If intDoIt = vbCancel Then WScript.Quit
End Sub

Function RandoChars()
RandoChars=int(rnd*999)
End Function
 
It worked before the latest updates but does not now
Before which updates ?
BTW, please, define "does not work"

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
since i updated to XP SP3 and the updates since then.

it runs, returns the error, then does not continue

line 63
character 1
error File already exists
code 800A003A

I am just trying to have a script that randomly renames the first three charachters of files to allow a random play order.
 
Which line is line 63 ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
ffilact.move(fold.path & "\" & string(3-len(cstr(num)),"0") & cstr(num) & right(ffilact.name,len(ffilact.name)-3))
 
error File already exists
So, use either the fileExists method or copy and delete the file.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
i was already pushing my knowledge of VBScripting and had to get help.

what is the "the fileExists method
 
copy and delete would take to long and reading the "FileExists Method" only seemed to verify if file exists which it does.

i'm looking for the code that allows this to just rename the first three characters in the name as the "game" always plays in alpha-numeric order and it gets repetitious.

This is way above me (i'm not much of a programmer) and I thank you for trying to "teach me to fish" but this is a "give me a fish" type problem.

thanks anyways
 
Ah, but you have found a "teach me to fish" forum!!!!


At any rate, if you try to use the .Move method on a file, and the destination filename already exists, you get an error. This is by design. It's how the Move method works.

But if you use the FileExists property beforehand, you can "plan ahead", and not exexcute the "Move"... instead, perhaps, get a new random number. Here's a starting point, untested tho.

Code:
Dim bRenamedFile, sNewFileName
For Each ffilact in ffil 
   bRenamedFile = False
	
   Do While Not bRenamedFile
      num = RandoChars()
      sNewFileName = fold.path & "\" & string(3-len(cstr(num)),"0") & cstr(num) & right(ffilact.name,len(ffilact.name)-3)
      If Not fso.FileExists(sNewFileName)
         ffilact.move(sNewFileName)
         bRenamedFile = True
      End If
   Loop
Next
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top