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

Delete File in User Profile 1

Status
Not open for further replies.

pshelfo

IS-IT--Management
Apr 27, 2004
16
US
I am trying to delete a file (Outlook name completion cache fiel) located within a users profile via a vbscript. For example:

c:\Documents and Settings\username\Application Data\Microsoft\Outlook\outlook.n2k

I can write a script to delete a file within a specified directory but I can't get a script to work when username is part of the directory path. Here is what I have started working on if anyone can lend a hand. I have taken the script that deletes a specified file location and have tried to add the network.username variable. When I run this script I get the following error:

Line 4 Char 1
Object required: "[string: "jsmith"]'

Note: jsmith is just an exampel of who is logged in at the time.

dim filesys, demofile
set filesys = CreateObject ("Scripting.FileSystemObject")
set network=createObject("wscript.network")
set username=network.username
set demofile = filesys.GetFile("c:\Documents and Settings\username\Application Dta\Microsoft\Outlook\test.txt")
demofile.Delete
 
Have you tried the following "delete" line instead?

set demofile = filesys.GetFile("c:\Documents and Settings\" & username & "\Application Dta\Microsoft\Outlook\test.txt")
 
I just tried the change and unfortunately it did not work. It stills comes back with the same error. If I remove the last two lines and pipe the output to MsgBox I get the username back. I can't find a way to use the variable in the directory path for some reason.
 
Try this

Set fso = CreateObject("Scripting.FileSystemObject")
Set WshNetwork = WScript.CreateObject("WScript.Network")
username=WshNetwork.UserName
file = ("C:\Documents and Settings\"& username & "\Application Data\Microsoft\Outlook\test.txt")
if fso.FileExists(file) Then
fso.DeleteFile file
msgbox file & " deleted"
Else
msgbox file & " not exist"
End If
 
pshelfo,

Try the below and change your info to the required and see if it works:

set oNetwork = CreateObject("WScript.Network")
Username = oNetwork.Username
Const DeleteReadOnly = True
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.DeleteFile("C:\Documents and Settings\" & username & "\application data\microsoft\outlook\test.txt"), DeleteReadOnly
 
lamber,

I guess I'm a little bit slower than you are!
 
Yes. It worked!!! Thanks for all your help!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top