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!

Delete file based on Username 1

Status
Not open for further replies.

PU36

MIS
Mar 16, 2005
202
US
I'm creating an HTA for all users that needs to delete a file based on their username.

For example, when a user opens the HTA, selects the Radio button and they clicks the "Run" button, it needs to delete \\server\share\%username%.txt

The Sub that I'm working on is (I just put the MsgBox there to verify everything works);

Sub TestSub
If RadioOption(0).Checked Then
MsgBox "You Checked the The First One"
ElseIf RadioOption(1).Checked Then
MsgBox "You Checked the The Second One"
ElseIf RadioOption(2).Checked Then
MsgBox "You Checked the The Third One"
End If
End Sub
 
Sounds like a fun project.

If [blue]you have problems[/blue], I want [green]source code[/green] AND [green]error messages[/green], none of this [red]"there was an error crap"[/red]
 
:) Totally contradictory (in concept) to Qik3Coder's signature, I'll make the assumption that the question you didn't ask but meant to is, "How do I do this?". Also, why even give the user an option to delete the file - just delete it! I'll also assume that this HTA is being run locally BY THE CURRENT USER)

Code:
set objShell = WScript.CreateObject("WScript.Shell")
set objFSO = WScript.CreateObject("Scripting.FileSystemObject")

strCurrentUser = objShell.ExpandEnvironmentStrings("%UserName%")
objFSO.DeleteFile "\\server\share\" & strCurrentUser & ".txt"

-Geates

"I hope I can feel and see the change - stop the bleed inside a feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Thank you GEATES!!!!!! Qik3Coder....not so much.

Here is what I came up with which works well.

Code:
Dim wshShell 
Dim objUserVariables
Dim objSystemVariables
Dim filesys
Dim demofile

Set wshShell  =  CreateObject("WScript.Shell")
Set filesys = CreateObject ("Scripting.FileSystemObject")
strUserName = wshShell.ExpandEnvironmentStrings( "%USERNAME%" )

Sub TestSub
    If RadioOption(0).Checked Then
        If filesys.FileExists("\\server\share\" & strUserName & ".txt") Then
            Set demofile = filesys.GetFile("\\server\share\" & strUserName & ".txt")
            demofile.Delete
            MsgBox "Your file has been fixed.", vbInformation, "File Delete Success"
        Else
            MsgBox "Error Code: 404" & vbNewLine & vbNewLine & "Your file was not found.", vbCritical, "File Delete Error"            
        End If
    ElseIf RadioOption(1).Checked Then
        If filesys.FileExists("\\server1\share1\" & strUserName & ".txt") Then\\server\share\" & strUserName & ".txt")
            demofile.Delete
            MsgBox "Your file has been fixed.", vbInformation, "File Delete Success"
        Else
            MsgBox "Error Code: 404" & vbNewLine & vbNewLine & "Your file was not found.", vbCritical, "File Delete Error"            
        End If   
    ElseIf RadioOption(2).Checked Then
        If filesys.FileExists("\\server2\share2\" & strUserName & ".txt") Then
            Set demofile = filesys.GetFile("\\server2\share2\" & strUserName & ".txt")
            demofile.Delete
            MsgBox "Your file has been fixed.", vbInformation, "File Delete Success"
        Else
            MsgBox "Error Code: 404" & vbNewLine & vbNewLine & "Your file was not found.", vbCritical, "File Delete Error"            
        End If
    End If
End Sub
 
Aside from being quite redundant and missing code in RadioOption1 (both I assume incidental), I'm glad you were able to implement it for you needs.

-Geates

NOTE: Qik3Coder probably said what he did because your original post was not a question but rather a statement. :)

"I hope I can feel and see the change - stop the bleed inside a feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
You didn't ask a question. You didn't even hint at an error.
This isn't a helpdesk.

I optimized your function
Code:
Dim wshShell 
Dim objUserVariables
Dim objSystemVariables
Dim filesys
Dim demofile
Set wshShell  =  CreateObject("WScript.Shell")
Set filesys = CreateObject ("Scripting.FileSystemObject")
strUserName = wshShell.ExpandEnvironmentStrings( "%USERNAME%" )

Sub TestSub
	Dim serverPath
	If RadioOption(0).Checked Then
		set serverPath = "\\server\share\"
	ElseIf RadioOption(1).Checked Then
		serverPath = "\\server1\share1\"
	ElseIf RadioOption(2).Checked Then
		serverPath = "\\server2\share2\"
	End If

	set serverPath = serverPath & strUserName & ".txt"
	If filesys.FileExists(serverPath) Then
		Set demofile = filesys.GetFile(serverPath)
		demofile.Delete
		MsgBox "Your file has been fixed.", vbInformation, "File Delete Success"
	Else
		MsgBox "Error Code: 404" & vbNewLine & vbNewLine & "Your file was not found.", vbCritical, "File Delete Error" 
	End If    	
End Sub

If [blue]you have problems[/blue], I want [green]source code[/green] AND [green]error messages[/green], none of this [red]"there was an error crap"[/red]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top