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

rename a file or delete a file 1

Status
Not open for further replies.
Nov 18, 2005
43
US
Hi new to VB I am trying something simple. To rename a exe if it exists. Eventually I would want to try and delete it. How my if else is not working.
I am getting a Line 2 end of statement expected error at char 57. Can anyone help me out.
Thanks.
Code:
If fso.FolderExists ("C:\Program Files\Winamp\")Then
Set objFSO = CreateObject("Scripting.FileSystemObject") objFSO.MoveFile "C:\Program Files\Winamp\winamp.exe" , "C:\Program Files\Winamp\Winamp_is_prohibited.txt";
        Else
'Do nothing



End If
 
There are a couple things going on here.

You set to create the FSO object before using it.
You don't use semi-colons to end a line.
You don't need an else unless you want to use it (although it really doesn't cause a problem).

Here's how the code should look.
Code:
Set objFSO = CreateObject("Scripting.FileSystemObject")

If fso.FolderExists("C:\Program Files\Winamp\") Then
    objFSO.MoveFile "C:\Program Files\Winamp\winamp.exe", "C:\Program Files\Winamp\Winamp_is_prohibited.txt"
End If

There are still some other problems that you may run in to. To be safe, you should make sure the file exists in the source location, and that it doesn't exist in the destination.

This is hw I woudl write it.
Code:
    Dim FSO As Scripting.FileSystemObject
    
    Set FSO = CreateObject("Scripting.FileSystemObject")
    If FSO.fileexists("C:\Program Files\Winamp\winamp.exe") Then
        If FSO.fileexists("C:\Program Files\Winamp\Winamp_is_prohibited.txt") Then
            Call FSO.DeleteFile("C:\Program Files\Winamp\Winamp_is_prohibited.txt")
        End If
        
        FSO.MoveFile "C:\Program Files\Winamp\winamp.exe", "C:\Program Files\Winamp\Winamp_is_prohibited.txt"
    End If

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
I forgot to mention that you will need to add a reference to the object.

Click Project -> References
Scroll to 'Microsoft Scripting Runtime'
Select it.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Not sure what you mean.I am just messing around using notepad.
 
In that case, you will have a problem compiling this.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
I suspect you might want to wander over to forum329, which is for VbScript
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top