compdigit44
asked on
VBSCript and %userprofile% - File NOt found
I am try to write a script that will drop a log file at the root of the user's profile using %userprofile%
I searched online on how to do this but still no luck.
What is the proper syntax I need to reference %userprofile% and drop a file with a predefined name in this location.
I searched online on how to do this but still no luck.
What is the proper syntax I need to reference %userprofile% and drop a file with a predefined name in this location.
ASKER
I mean the log file created has to be placed in the %userprofile% directory.
When I call the file in the script using strprofile\file.txt I get file not found
When I call the file in the script using strprofile\file.txt I get file not found
Do you want to copy the log file or move it? What is the source path?
ASKER
I need to create a new log file in this location
Try this:
Dim oWsh, strUserProfile , oFSO, oFile
Set oWsh = WScript.CreateObject("WScript.Shell")
strUserProfile = ws.ExpandEnvironmentStrings("%userprofile%")
Set oFSO= CreateObject("Scripting.FileSystemObject")
Set oFile= oFSO.CreateTextFile("strUserProfile\file.txt")
ASKER
I get the error in line one: "object required: 'ws'
Use the new code instead of the old one.
ASKER
I am it errors on line 4
strUserProfile = ws.ExpandEnvironmentString s("%userpr ofile%")
Object required 'WS'
strUserProfile = ws.ExpandEnvironmentString
Object required 'WS'
I'm so sorry, should be:
strUserProfile = oWsh.ExpandEnvironmentStrings("%userprofile%")
ASKER
Thanks!!!
I we are makin progess.. I keep getting path not found though.
Here is my line to insert the log file...
Set objLogFile = objFSO.OpenTextFile("oFile ", 8, True, 0)
I we are makin progess.. I keep getting path not found though.
Here is my line to insert the log file...
Set objLogFile = objFSO.OpenTextFile("oFile
Now I'm confused, the code I posted creates a text file (file.txt) in the user profile. Isn't that what you want?
ASKER
No I think I am confused since I not not a scripting person. Below is sample from my scirpt so far...
Dim oWsh, strUserProfile , oFSO, oFile
Set oWsh = WScript.CreateObject("WScr ipt.Shell" )
strUserProfile = oWsh.ExpandEnvironmentStri ngs("%user profile%")
Set oFSO= CreateObject("Scripting.Fi leSystemOb ject")
Set oFile= oFSO.CreateTextFile("strUs erProfile\ file.txt")
Function LogEntry (LogEntryText)
objLogFile.WriteLine "<![LOG[" & LogEntryText & "]LOG]!><time=""" & Hour(Now) & ":" & Minute(Now) & ":" & Second(Now) & ".001+240"" date=""" & Replace(Date(), "/", "-") & """ component=""PrintMapDelete "" context="""" type=""1"" thread=""0"" file=""" & WScript.Application & " " & WScript.Version & """>"
End Function
Set objFSO = CreateObject("Scripting.Fi leSystemOb ject")
Set objLogFile = objFSO.OpenTextFile(oFile, 8, True, 0)
Why is my Set objLogFile = objFSO.OpenTextFile(oFile, 8, True, 0) line not working
Dim oWsh, strUserProfile , oFSO, oFile
Set oWsh = WScript.CreateObject("WScr
strUserProfile = oWsh.ExpandEnvironmentStri
Set oFSO= CreateObject("Scripting.Fi
Set oFile= oFSO.CreateTextFile("strUs
Function LogEntry (LogEntryText)
objLogFile.WriteLine "<![LOG[" & LogEntryText & "]LOG]!><time=""" & Hour(Now) & ":" & Minute(Now) & ":" & Second(Now) & ".001+240"" date=""" & Replace(Date(), "/", "-") & """ component=""PrintMapDelete
End Function
Set objFSO = CreateObject("Scripting.Fi
Set objLogFile = objFSO.OpenTextFile(oFile,
Why is my Set objLogFile = objFSO.OpenTextFile(oFile,
ASKER
What is going wrong ??????
Try this:
Dim oWsh, strUserProfile, oFSO, oFile
Call LogEntry("TEST")
Function LogEntry(LogEntryText)
Set oWsh = WScript.CreateObject("WScript.Shell")
strUserProfile = oWsh.ExpandEnvironmentStrings("%userprofile%")
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFile = oFSO.OpenTextFile(strUserProfile & "\file.txt", 8, True, 0)
oFile.WriteLine "<![LOG[" & LogEntryText & "]LOG]!><time=""" & Hour(Now) & ":" & Minute(Now) & ":" & Second(Now) & ".001+240"" date=""" & Replace(Date, "/", "-") & """ component=""PrintMapDelete"" context="""" type=""1"" thread=""0"" file=""" & WScript.Application & " " & WScript.Version & """>"
End Function
ASKER
Still having problems ...
Below is a sample of my script.
Dim oWsh, strUserProfile, oFSO, oFile
Call LogEntry("LogEntryText")
Function LogEntry(LogEntryText)
Set oWsh = WScript.CreateObject("WScr ipt.Shell" )
strUserProfile = oWsh.ExpandEnvironmentStri ngs("%user profile%")
Set oFSO = CreateObject("Scripting.Fi leSystemOb ject")
Set oFile = oFSO.CreateTextFile(strUse rProfile & "\file.txt")
Set oFile = oFSO.OpenTextFile(strUserP rofile & "\file.txt", 8, 0)
oFile.WriteLine "<![LOG[" & LogEntryText & "]LOG]!><time=""" & Hour(Now) & ":" & Minute(Now) & ":" & Second(Now) & ".001+240"" date=""" & Replace(Date, "/", "-") & """ component=""PrintMapDelete "" context="""" type=""1"" thread=""0"" file=""" '& WScript.Application & " " & WScript.Version & """>"
End Function
Set WshNetwork = WScript.CreateObject("WScr ipt.Networ k")
Set colPrinters = WshNetwork.EnumPrinterConn ections
For intPrinter = 0 To colPrinters.Count - 1 Step 2
strPrinterPort = colPrinters(intPrinter)
strPrinterName = colPrinters(intPrinter + 1)
LogEntry strPrinterName & " - " & strPrinterPort
If left(colPrinters.Item(intP rinter + 1),len(strSrvName1)) = strSrvName1 Then
LogEntry " Printer matches target name. Printer will be deleted"
On Error Resume Next
WshNetwork.RemovePrinterCo nnection strPrinterName
Below is a sample of my script.
Dim oWsh, strUserProfile, oFSO, oFile
Call LogEntry("LogEntryText")
Function LogEntry(LogEntryText)
Set oWsh = WScript.CreateObject("WScr
strUserProfile = oWsh.ExpandEnvironmentStri
Set oFSO = CreateObject("Scripting.Fi
Set oFile = oFSO.CreateTextFile(strUse
Set oFile = oFSO.OpenTextFile(strUserP
oFile.WriteLine "<![LOG[" & LogEntryText & "]LOG]!><time=""" & Hour(Now) & ":" & Minute(Now) & ":" & Second(Now) & ".001+240"" date=""" & Replace(Date, "/", "-") & """ component=""PrintMapDelete
End Function
Set WshNetwork = WScript.CreateObject("WScr
Set colPrinters = WshNetwork.EnumPrinterConn
For intPrinter = 0 To colPrinters.Count - 1 Step 2
strPrinterPort = colPrinters(intPrinter)
strPrinterName = colPrinters(intPrinter + 1)
LogEntry strPrinterName & " - " & strPrinterPort
If left(colPrinters.Item(intP
LogEntry " Printer matches target name. Printer will be deleted"
On Error Resume Next
WshNetwork.RemovePrinterCo
What is the problem and where does it occur?
ASKER
line 10 char 5 file not found...
ASKER
I am sorry one of my co-workers was helping me write then left for vacation
ASKER
Disregard my post about my error it was a typo. New error is permision denied for %userprofile%
Try this:
Dim oWsh, strUserProfile , oFSO, oFile
Call LogEntry("TEST")
Function LogEntry (LogEntryText)
Set oWsh = WScript.CreateObject("WScript.Shell")
strUserProfile = oWsh.ExpandEnvironmentStrings("%userprofile%")
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFile = oFSO.CreateTextFile(strUserProfile & "\file.txt")
oFile.Close
Set oFile = oFSO.OpenTextFile(strUserProfile & "\file.txt", 8, True, 0)
oFile.WriteLine "<![LOG[" & LogEntryText & "]LOG]!><time=""" & Hour(Now) & ":" & Minute(Now) & ":" & Second(Now) & ".001+240"" date=""" & Replace(Date(), "/", "-") & """ component=""PrintMapDelete"" context="""" type=""1"" thread=""0"" file=""" & WScript.Application & " " & WScript.Version & """>"
End Function
ASKER
Thanks this seems to be working, although I do not understand something.
In lines 9 & 11 if I rename the file.txt I get a Permission Denied message. Yet it works fine with file.txt
From what I am finding online it seems to be caused by the fact the file could already be open or present. Yet, I have set the file write to append and we are already closing the connection before reading it.
In lines 9 & 11 if I rename the file.txt I get a Permission Denied message. Yet it works fine with file.txt
From what I am finding online it seems to be caused by the fact the file could already be open or present. Yet, I have set the file write to append and we are already closing the connection before reading it.
ASKER
I have confirm the script is creating the file in the correct location but nothing is getting written to it. The script is close the file after creating it so I am at a loss as to the problem
The top portion of your script should look as follows:
The problem is you're assigning oFile when you're creating the file and then you're trying to assign it again when you're opening it for appending.
Dim oWsh, strUserProfile, oFSO, oFile, strFile
Call LogEntry("LogEntryText")
Function LogEntry(LogEntryText)
Set oWsh = WScript.CreateObject("WScript.Shell")
strUserProfile = oWsh.ExpandEnvironmentStrings("%userprofile%")
strFile = strUserProfile & "\file.txt"
Set oFSO = CreateObject("Scripting.FileSystemObject")
If Not oFSO.FileExists(strFile) Then
oFSO.CreateTextFile strFile
End If
Set oFile = oFSO.OpenTextFile(strFile, 8, 0)
oFile.WriteLine "<![LOG[" & LogEntryText & "]LOG]!><time=""" & Hour(Now) & ":" & Minute(Now) & ":" & Second(Now) & ".001+240"" date=""" & Replace(Date, "/", "-") & """ component=""PrintMapDelete"" context="""" type=""1"" thread=""0"" file=""" '& WScript.Application & " " & WScript.Version & """>"
End Function
The problem is you're assigning oFile when you're creating the file and then you're trying to assign it again when you're opening it for appending.
ASKER
I got the same error again using the code you suggested... :o(
How are you executing the script? Are you double-clicking it, running it from a command prompt using 'cscript scriptname.vbs' or are you assigning this as a startup script that runs when the computer boots?
ASKER
For testing purposes I am double clicking the script from my desktop but plan on deploying it via a group policy login script
ASKER
I just tried to run it using cscript from the command line and got the same permission denined error
ASKER
the script is creating the file but not able to write to it after it is created
ASKER
I have read online this has nothing to do with a permission problem persay but have to do with the fact the CreateTextFile method is beging used and is failing on the opentextfile method becusae the file is already open.
The suggestion has been to write to the reference instead.. Does anyone know what they are referring to?
http://stackoverflow.com/questions/369242/vbscript-fso-opentextfile-permission-denied
The suggestion has been to write to the reference instead.. Does anyone know what they are referring to?
http://stackoverflow.com/questions/369242/vbscript-fso-opentextfile-permission-denied
The reference is correct. If you create the text file and leave it open, such as:
Then objFile has the file locked. However, in the code I'm proposing, we're creating the text file with:
And this does not leave the file open. If you're using the block of text below, exactly as I have it, it should run with no issues. I've written to the log file several times with no issues. I can run it over and over and it appends properly.
Try just that code snippet in a vbs file all by itself and see if it works - it should. Perhaps you're experiencing another issue that we're not seeing the code for.
Set objFile = objFSO.CreateTextFile(strFile)
Then objFile has the file locked. However, in the code I'm proposing, we're creating the text file with:
oFSO.CreateTextFile strFile
And this does not leave the file open. If you're using the block of text below, exactly as I have it, it should run with no issues. I've written to the log file several times with no issues. I can run it over and over and it appends properly.
Dim oWsh, strUserProfile, oFSO, oFile, strFile
Call LogEntry("LogEntryText")
Function LogEntry(LogEntryText)
Set oWsh = WScript.CreateObject("WScript.Shell")
strUserProfile = oWsh.ExpandEnvironmentStrings("%userprofile%")
strFile = strUserProfile & "\file.txt"
Set oFSO = CreateObject("Scripting.FileSystemObject")
If Not oFSO.FileExists(strFile) Then
oFSO.CreateTextFile strFile
End If
Set oFile = oFSO.OpenTextFile(strFile, 8, 0)
oFile.WriteLine "<![LOG[" & LogEntryText & "]LOG]!><time=""" & Hour(Now) & ":" & Minute(Now) & ":" & Second(Now) & ".001+240"" date=""" & Replace(Date, "/", "-") & """ component=""PrintMapDelete"" context="""" type=""1"" thread=""0"" file=""" '& WScript.Application & " " & WScript.Version & """>"
End Function
Try just that code snippet in a vbs file all by itself and see if it works - it should. Perhaps you're experiencing another issue that we're not seeing the code for.
ASKER
Thanks for the reply I am using the exact code of posted earlier and getting the same permission denied at line 13 which is:
Set oFile = oFSO.OpenTextFile(strFile, 8, 0)
Set oFile = oFSO.OpenTextFile(strFile,
ASKER
Here is my code....
Dim oWsh, strUserProfile, oFSO, oFile, strFile
Call LogEntry("LogEntryText")
Function LogEntry(LogEntryText)
Set oWsh = WScript.CreateObject("WScript.Shell")
strUserProfile = oWsh.ExpandEnvironmentStrings("%userprofile%")
strFile = strUserProfile & "\file.txt"
Set oFSO = CreateObject("Scripting.FileSystemObject")
If Not oFSO.FileExists(strFile) Then
oFSO.CreateTextFile strFile
End If
Set oFile = oFSO.OpenTextFile(strFile, 8, 0)
oFile.WriteLine "<![LOG[" & LogEntryText & "]LOG]!><time=""" & Hour(Now) & ":" & Minute(Now) & ":" & Second(Now) & ".001+240"" date=""" & Replace(Date, "/", "-") & """ component=""PrintMapDelete"" context="""" type=""1"" thread=""0"" file=""" '& WScript.Application & " " & WScript.Version & """>"
End Function
strSrvName1 = "\\server1\HP"
strSrvName2 = "\\server2\Dell"
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set colPrinters = WshNetwork.EnumPrinterConnections
For intPrinter = 0 To colPrinters.Count - 1 Step 2
strPrinterPort = colPrinters(intPrinter)
strPrinterName = colPrinters(intPrinter + 1)
LogEntry strPrinterName & " - " & strPrinterPort
If left(colPrinters.Item(intPrinter + 1),len(strSrvName1)) = strSrvName1 Then
LogEntry " Printer matches target name. Printer will be deleted"
On Error Resume Next
WshNetwork.RemovePrinterConnection strPrinterName
If Err.Number = 0 Then
LogEntry " Printer was succesfully deleted"
Else
LogEntry " An error occured while deleting the printer. Error Number: " & Err.Number & " Error Description: " & Err.Description
End If
Else
LogEntry " Printer does not match target name. No action required"
End If
If left(colPrinters.Item(intPrinter + 1),len(strSrvName2)) = strSrvName2 Then
LogEntry " Printer matches target name. Printer will be deleted"
On Error Resume Next
WshNetwork.RemovePrinterConnection strPrinterName
If Err.Number = 0 Then
LogEntry " Printer was succesfully deleted"
Else
LogEntry " An error occured while deleting the printer. Error Number: " & Err.Number & " Error Description: " & Err.Description
End If
Else
LogEntry " Printer does not match target name. No action required"
End If
Next
I'm wondering if perhaps another script or program is still running and has the file locked.
Can you please open Task Manager (CTRL + SHIFT + ESCAPE), change to the processes view (or Details view on Windows 8), and look for any processes called 'wscript' or 'wscript.exe'. If you find any, end task on them. Then try the script again.
Can you please open Task Manager (CTRL + SHIFT + ESCAPE), change to the processes view (or Details view on Windows 8), and look for any processes called 'wscript' or 'wscript.exe'. If you find any, end task on them. Then try the script again.
ASKER
Nothing is using the script an I only see the one instance of wscript and it goes away after I close the error dialog box
I have tried three different computers all with the same result
I am not a scripting person but trying hard to research this error and do not get what I am doing wrong..
I have tried three different computers all with the same result
I am not a scripting person but trying hard to research this error and do not get what I am doing wrong..
ASKER
Any ideas!!!!
I have even tried to change the file location to C:\ on my workstation which I have local admin rights on and still get permission denied.. I am at a total lose an need to find a solution for this is week .. :o(
I have even tried to change the file location to C:\ on my workstation which I have local admin rights on and still get permission denied.. I am at a total lose an need to find a solution for this is week .. :o(
I missed most of the discussion, did the code I wrote work? It worked fine for me.
ASKER
I got the same error using your code.
Tried three different workstations 2 Windows 7 and 1 Windows 8 all gave permission denied error.
Why is this failing I am trying so hard to research this and stumped
Tried three different workstations 2 Windows 7 and 1 Windows 8 all gave permission denied error.
Why is this failing I am trying so hard to research this and stumped
This is the file my code created, is this what it should look like?
file.txt
file.txt
ASKER
Yes it does contain that text but a dialg box comes up with permission denied...
when do you get the permission denied?
ASKER
As soon as I double click the script to run an it is always on the OpenTextFile line.
I did find something interesting. When I copy the script to my %username%b directory and run it from there I get Path Not Found.... This make me would if for some reason the script is not finding the %username% location...
I did find something interesting. When I copy the script to my %username%b directory and run it from there I get Path Not Found.... This make me would if for some reason the script is not finding the %username% location...
That's easy enough to find out. The following will display a message box with the user profile path:
Dim oWsh, strUserProfile , oFSO, oFile
Call LogEntry("TEST")
Function LogEntry (LogEntryText)
Set oWsh = WScript.CreateObject("WScript.Shell")
strUserProfile = oWsh.ExpandEnvironmentStrings("%userprofile%")
MsgBox "The current users profile is located at: " & strUserProfile
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFile = oFSO.CreateTextFile(strUserProfile & "\file.txt")
oFile.Close
Set oFile = oFSO.OpenTextFile(strUserProfile & "\file.txt", 8, True, 0)
oFile.WriteLine "<![LOG[" & LogEntryText & "]LOG]!><time=""" & Hour(Now) & ":" & Minute(Now) & ":" & Second(Now) & ".001+240"" date=""" & Replace(Date(), "/", "-") & """ component=""PrintMapDelete"" context="""" type=""1"" thread=""0"" file=""" & WScript.Application & " " & WScript.Version & """>"
End Function
I just noticed, your not using %username% right? you need %userprofile%.
ASKER
Sorry ... my script is using %userprofile% I was testing different combinations when I was trying to figure this out.
I ran you test script from my desktop an it ran perfectly and showed by path as C:\users\MyName
I ran you test script from my desktop an it ran perfectly and showed by path as C:\users\MyName
Does the script write the text to the text file?
ASKER
Yes the script did write to the file.txt
In which case I fail to see what the problem is.
ASKER
This is why I am confused when I run the whole script just like I ran your test script I get permisson denied. I am using the same script I posted earlier.
Does this script work:
strSrvName1 = "\\server1\HP"
strSrvName2 = "\\server2\Dell"
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set colPrinters = WshNetwork.EnumPrinterConnections
For intPrinter = 0 To colPrinters.Count - 1 Step 2
strPrinterPort = colPrinters(intPrinter)
strPrinterName = colPrinters(intPrinter + 1)
LogEntry strPrinterName & " - " & strPrinterPort
If Left(colPrinters.Item(intPrinter + 1), Len(strSrvName1)) = strSrvName1 Then
LogEntry " Printer matches target name. Printer will be deleted"
On Error Resume Next
WshNetwork.RemovePrinterConnection strPrinterName
If Err.Number = 0 Then
LogEntry " Printer was succesfully deleted"
Else
LogEntry " An error occured while deleting the printer. Error Number: " & Err.Number & " Error Description: " & Err.Description
End If
Else
LogEntry " Printer does not match target name. No action required"
End If
If Left(colPrinters.Item(intPrinter + 1), Len(strSrvName2)) = strSrvName2 Then
LogEntry " Printer matches target name. Printer will be deleted"
On Error Resume Next
WshNetwork.RemovePrinterConnection strPrinterName
If Err.Number = 0 Then
LogEntry " Printer was succesfully deleted"
Else
LogEntry " An error occured while deleting the printer. Error Number: " & Err.Number & " Error Description: " & Err.Description
End If
Else
LogEntry " Printer does not match target name. No action required"
End If
Next
Function LogEntry(LogEntryText)
Set oWsh = WScript.CreateObject("WScript.Shell")
strUserProfile = oWsh.ExpandEnvironmentStrings("%userprofile%")
MsgBox "The current users profile is located at: " & strUserProfile
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFile = oFSO.CreateTextFile(strUserProfile & "\file.txt")
oFile.Close
Set oFile = oFSO.OpenTextFile(strUserProfile & "\file.txt", 8, True, 0)
oFile.WriteLine "<![LOG[" & LogEntryText & "]LOG]!><time=""" & Hour(Now) & ":" & Minute(Now) & ":" & Second(Now) & ".001+240"" date=""" & Replace(Date, "/", "-") & """ component=""PrintMapDelete"" context="""" type=""1"" thread=""0"" file=""" & WScript.Application & " " & WScript.Version & """>"
oFile.Close
End Function
ASKER
Well I do not get a permissoin denied error but I get a dialog box that pops up showing by location as C:\users\MyUser name but will not go away when we click on it.
I think we " You" are getting close .. You are scripting master!!!!
I think we " You" are getting close .. You are scripting master!!!!
Delete line 41.
ASKER
It working with no errors.. The only thing I noticed in the log file it that it states the printer was deleted but does listed which on..
Thank you so much for your help
Thank you so much for your help
I'm sorry I don't understand. What information is missing?
ASKER
Sorry for the poor explanation. There are no errors and the script runs perfectly.. I noticed in the file.txt file it states a printer was deleted or not deleted but doesn't the the printer's name
Try this script, does it give you the names?
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set oDrives = WshNetwork.EnumNetworkDrives
Set oPrinters = WshNetwork.EnumPrinterConnections
For i = 0 to oDrives.Count - 1 Step 2
WScript.Echo "Drive " & oDrives.Item(i) & " = " & oDrives.Item(i+1)
Next
ASKER
Should I run this script as it or add it to the order script you wrote earlier today?
Run it separately.
ASKER
I ran the script but it brought up a dialog box with my home folder path and nothing about printers.
Sorry my bad, try this script:
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set oDrives = WshNetwork.EnumNetworkDrives
Set oPrinters = WshNetwork.EnumPrinterConnections
WScript.Echo "Network drive mappings:"
For i = 0 to oDrives.Count - 1 Step 2
WScript.Echo "Drive " & oDrives.Item(i) & " = " & oDrives.Item(i+1)
Next
WScript.Echo
WScript.Echo "Network printer mappings:"
For i = 0 to oPrinters.Count - 1 Step 2
WScript.Echo "Port " & oPrinters.Item(i) & " = " & oPrinters.Item(i+1)
Next
ASKER
After multiple dialog boxes if does end up listing my printers..
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
It think it is working!!!
I have to test it some more but so far it is looking good..........
You are a master... If I could award 50,000 points for this would ..
I will post back shortly.
I have to test it some more but so far it is looking good..........
You are a master... If I could award 50,000 points for this would ..
I will post back shortly.
ASKER
Everything worked perfectly. Thank you so much for your help. Script is one of those things I have wanted to learn and even though this was stressfull I did learn something as well!!!!!
Glad it worked. Thanks for the points.
Open in new window