This question already has answers here:
can you get the pc user name in vbs?
(2 answers)
Closed 4 years ago.
I need to find a way to get the current user of the system and put it in the address.
Right now I am using this script:
Set WshShell = CreateObject("WScript.Shell")
Dim User
User = "User"
WshShell.Run Chr(34) & "C:\Users\" & User & "\Documents\Test\M2.bat" & Chr(34), 0
Set WshShell = Nothing
I am using it to run a batch file without showing. Right now to change the user I have to change the variable myself. Is there a way to make it automatic?
Msgbox CreateObject("WScript.Shell").SpecialFolders("MyDocuments")
The correct way is to ask the system where is My Documents. Users can move them.
There are several ways of obtaining the username of the currently logged-in user, for example:
expanding the environment variable %USERNAME%:
Set sh = CreateObject("WScript.Shell")
User = sh.ExpandEnvironmentStrings("%USERNAME%")
via the UserName property of a WshNetwork object:
Set net = CreateObject("WScript.Network")
User = net.UserName
from the Win32_ComputerSystem WMI class:
Set wmi = GetObject("winmgmts://./root/cimv2")
For o In wmi.ExecQuery("SELECT * FROM Win32_ComputerSystem")
User = o.UserName
Next
via ADSI:
Set sys = CreateObject("ADSystemInfo")
User = sys.UserName
For a more complete list see this overview compiled by Rob van der Woude.
Related
This question already has an answer here:
How to get a path with the variable user in VBscript
(1 answer)
Closed 3 years ago.
Basically, the code im looking for is how to find username with a variable
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CreateTextFile "C:\Users\%username%\Desktop\test.txt"
I tried with other lines of code to find out the username. But nothing worked
when i typed out the code i wrote with other lines of code to figure out the %username% variable, it doesn't work.
Please respond with new lines of code attached to my previous code here. (I'm new to this so it would make it confusing if you responded with unattached lines of code.)
You should write it like that :
strUser = CreateObject("WScript.Network").UserName
wscript.echo strUser
And to continue with your code like that :
Dim fso,strUser
Set fso = CreateObject("Scripting.FileSystemObject")
strUser = CreateObject("WScript.Network").UserName
'wscript.echo strUser
fso.CreateTextFile "C:\Users\"& strUser &"\Desktop\test.txt"
Also,in VBScript you can get the path to the current user's desktop folder via the SpecialFolders collection:
WScript.Echo CreateObject("WScript.Shell").SpecialFolders("Desktop")
And your code can be written like that too :
Option Explicit
Dim fso,DesktopFolder
Set fso = CreateObject("Scripting.FileSystemObject")
DesktopFolder = CreateObject("WScript.Shell").SpecialFolders("Desktop")
fso.CreateTextFile DesktopFolder &"\test.txt"
I've created a VBScript to run a macro from excel without opening the file.
Set objExcel = CreateObject("Excel.Application")
objExcel.Application.Run "'C:\Users\MyUser\Desktop\RM.xlsm'!Module4.Email"
objExcel.DisplayAlerts = False
objExcel.Application.Quit
Set objExcel = Nothing
I want to use this VBScript & XLSM file on different computers, so how can i change this script to work without editing the path every time?
(Maybe a code to run from current folder or a code to run from any user desktop)
If the file will always be on the desktop of every user then you can use environment variables to determine the location.
Set wshShell = CreateObject( "WScript.Shell" )
userName = wshShell.ExpandEnvironmentStrings( "%UserName%" )
path = "'C:\Users\" + userName + "\Desktop\RM.xlsm'!Module4.Email"
Set objExcel = CreateObject("Excel.Application")
objExcel.Application.Run path
objExcel.DisplayAlerts = False
objExcel.Application.Quit
Set objExcel = Nothing
(Untested code)
If the file is not on each user's desktop then you'll need to store it in a common location on the network and have each user access it from there, e.g. in .
\\YourFileSever\SharedFiles\RM.xlsm
In practical terms the latter is preferable as it means the workbook is in only one place and when it comes to releasing a new version you only have to update one copy
Im really new to VB script and have run into a problem. I keep getting
Error: Path not found
Code: 800A004C
when running the script;
Set objMachineList = objFSO.OpenTextFile(txtMachineList, ForReading)
If I change it to a local folder I no longer get the error but it needs to access the network drive for the script to work properly. Now time for the background story. There is a host computer that has the program (called TOMART, which are technical docs that are PDF with a GUI interface) installed to the network drive, with multiple computers accessing the drive (either online or always available offline) for sync purposes, which works fine. But the updates for the said program need to be installed into each of the networked computers hard drive for it to integrate and apply the updates for use with the program. The problem is, the updates need to be transferred to each computers hard drive for the program to recognise it and without the script working properly the updates arent getting transfered to each computers hard drive, so when accessing the updated PDFs in the program, the program just throws up error codes. I have attached the entire script in the hope someone here can help me out.
Sorry for the long winded description and apologies in advance if I haven't been clear with anything.
Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.fileSystemObject")
Set objNet = CreateObject("WScript.Network")
Const ForReading = 1
Const ForWriting = 2
ServerName = "\\Mynetworkdrivename\Files"
ETool = objNet.computerName
updateServer = "\\" & ServerName & "\TOMART\Update\"
txtMachineList = "\\" & ServerName & "\TOMART\Machines.txt"
txtLogFile = "\\" & ServerName & "\TOMART\Logs\" & ETool & ".txt"
localTOMARTUpdate = "C:\TOMART\update\"
Set objMachineList = objFSO.OpenTextFile(txtMachineList, ForReading)
Do While objMachineList.AtEndOfStream = False
strLine = objMachineList.ReadLine
If ETool = strLine THEN
Set updServerList = objFSO.GetFolder(updateServer)
Set colSubFlds = updServerList.SubFolders
For Each fl in colSubFlds
subFldr = fl.name
If Not objFSO.folderExists _
(localTOMARTUpdate & subFldr) THEN
objFSO.copyFolder updateServer & subFldr, localTOMARTUpdate
End If
Next
Set textFile = objFSO.createTextFile(txtLogFile)
End If
Loop
I have a network share already setup. I am trying to create a VBScript that will create a folder in the share called computername, which is the PC's name. The script will run locally on the PC, access the share and create the folder in the share.
My error is "bad path". I'm guessing I can't just state the network share path?
My script is below:
Dim objShell
Set oWS = WScript.CreateObject("WScript.Shell")
Set objShell = Wscript.CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
computername = oWS.ExpandEnvironmentStrings( "%COMPUTERNAME%" )
If NOT (objFSO.FolderExists("\\ServerPath\Share$" + computername)) Then
objFSO.CreateFolder("\\ServerPath\Share$" + computername)
End If
You're lacking a backslash between the name of the share and the name of the folder you want to create. Also, I'd recommend using a variable for the path, so you don't have to construct it multiple times.
Change this:
If NOT (objFSO.FolderExists("\\ServerPath\Share$" + computername)) Then
objFSO.CreateFolder("\\ServerPath\Share$" + computername)
End If
into this:
path = "\\ServerPath\Share$\" & computername
If NOT objFSO.FolderExists(path) Then
objFSO.CreateFolder(path)
End If
I have created a VBScript which pulls the service tag, username, and computer name from a computer. What I need to do now is compile this information in a text document.
How it's set up is as follows:
We have an Active Directory Server, with a folder for login scripts. I have created a batch file to run this .vbs script, and the script works well so far. What I now need is for a file on the AD server, called "logging.txt", to be populated with the information that is created with the .vbs script.
This is the script I have so far:
'Get Dell Service Tag Info
set ProSet = GetObject("winmgmts:").InstancesOf("Win32_BIOS")
Set ProSet1 = GetObject("winmgmts:").InstancesOf("Win32_SystemEnclosure")
For each Pro in ProSet
For each Pro1 in ProSet1
ServiceTag=Pro.SerialNumber
wscript.echo ServiceTag
exit for
Next
exit for
Next
'get username and computername, could also be asked in a batch
Set oShell = WScript.CreateObject("WScript.Shell")
Set oShellEnv = oShell.Environment("Process")
sComputerName = oShellEnv("ComputerName")
sUsername = oShellEnv("username")
wscript.echo sComputerName & " " & sUsername
Thank you very much in advance!
This is what I've tried so far:
sub log (user, computer)
dim fs,f
set fs=Server.CreateObject("Scripting.FileSystemObject")
set f=fs.OpenTextFile(Server.MapPath(".\logging.csv"),8,true)
f.WriteLine now & "," & user & "," & computer
f.Close:set f=Nothing
set fs=Nothing
end sub
I feel as though you're leaving something out.. this is how to write a text file in VBS.
dim filesys, filetxt, getname, path
Set filesys = CreateObject("Scripting.FileSystemObject")
Set filetxt = filesys.CreateTextFile("c:\somefile.txt", True)
path = filesys.GetAbsolutePathName("c:\somefile.txt")
getname = filesys.GetFileName(path)
filetxt.WriteLine("Your text goes here.")
filetxt.Close
If filesys.FileExists(path) Then
Response.Write ("Your file, '" & getname & "', has been created.")
End If
It would probably be easiest to save the information to a local text file and upload it with the command line ftp utility from the batch script that calls your script rather than trying to invent another wheel and do it in vbscript.
You can list the command line options of ftp with:
ftp /?
I recommend setting up something like this:
ftp -s:control.txt
control.txt would contain something like:
open [hostname or address]
[username]
[password]
cd [remote directory name]
put logging.txt
disconnect
bye