CMD script press button - windows

I'm running this script:
#echo off
C:\Users\it.suporte\Desktop\edesta.vbs > siga%computername%.txt
but when the vbs file opens generate a pop up window that asks me if I wanna save the information on a .txt file. As you can see I used > siga%computername%.txt so it can save the report in a .txt file but since there's a pop up windows it creats a .txt blank.
How can I make it to accept the pop up windows and save it?
my pop up are these lines:
If vbYes = MsgBox(ProductData & vblf & vblf & "Save to a file?", vbYesNo + vbQuestion, "BackUp Windows Key Information") then
Save ProductData
End If
and I got this function:
'Save data to a file
Function Save(Data)
Dim fso, fName, txt,objshell,UserName
Set objshell = CreateObject("wscript.shell")
'Get current user name
UserName = objshell.ExpandEnvironmentStrings("%UserName%")
'Create a text file on desktop
fName = "C:\Users\" & UserName & "\Desktop\%computername%.txt"
Set fso = CreateObject("Scripting.FileSystemObject")
Set txt = fso.CreateTextFile(fName)
txt.Writeline Data
txt.Close
End Function
Thanks in advance

Related

Use a VBS to edit ini for users %appdata% folder

I have script that edits the a line in the ini file, which sits on users %Appdata% folder i.e C:\Users\<>\AppData\Roaming.
The current script which I have only edits a file pointing to proper file location, but I would like to have script which can edit the file on every logged on users folder
I have a vbs below which look like this , but I am not able to use a variable %appdata% to edit the file under folder when the user is logged on
Const ForReading = 1
Const ForWriting = 2
Dim strUserName, CurrDir
Set objFSO = CreateObject("Scripting.FileSystemObject")
strUserName = InputBox("Please enter your email address below in the following format:" & Vbnewline & "firstname_lastname#test.com" & Vbnewline & Vbnewline & "HINT - If you are unsure, you can look up your name", "Add internet email address")
If strUserName = "" Then
Wscript.Quit
End If
Set objTextFile = objFSO.OpenTextFile("H:\appdata\Linkpoint360\LinkPointConfig.ini", ForReading)
Do Until objTextFile.AtEndOfStream
strNextLine = objTextFile.Readline
intLineFinder = InStr(strNextLine, "UserEMailAddress")
If intLineFinder <> 0 Then
strNextLine = "UserEMailAddress=" & strUserName
End If
strNewFile = strNewFile & strNextLine & VbCrLf
Loop
objTextFile.Close
Set objTextFile = objFSO.OpenTextFile("H:\appdata\Linkpoint360\LinkPointConfig.ini", ForWriting)
objTextFile.WriteLine strNewFile
objTextFile.Close
I am not scripting expert, but I have tried best to find a suitable solution over the internet and I have no luck
If someone can please edit this vbs and give a proper script, that will be really appreciated
# Ansgar Wiechers, can't post the image as i don't have 10 repuataion, but here is what I get in pop box:
Script: << Location of file >>
Line: 13
Char: 1
Error: Path not found
Code: 800A004C
Scource: Microsoft VBScript runtime error
the error I get when is use %appdata% in my script.
from the above code I have just edited file location "H:\appdata...." to "%appdata%....."
FileSystemObject methods don't expand environment variables. You need to do it yourself, e.g. like this:
...
Set sh = CreateObject("WScript.Shell")
config = sh.ExpandEnvironmentStrings("%APPDATA%\Linkpoint360\LinkPointConfig.ini")
Set objTextFile = objFSO.OpenTextFile(config, ForReading)
...
You can't reliabily do this in vbscript.
However you can make a safe assumption (disregarding network and profile updating issues that I don't think will matter) that profiles are under Users folder and each user will have the same relative path to AppFolder.
The normal way of handling this problem type is to use logon scripts.

VB Script to Delete Desktop shortcut

I need help with this script. This script creates a new desktop shortcut and then deletes another one. However I want it to delete one desktop shortcut if it exists or another if it exists. Not sure how to do this. I've put the 2 different shortcuts in after the "fso.deletefile" but I'm not sure what syntax to use (how to word it.) I'm new to vbs. Thanks in advance for the help.
L_Welcome_MsgBox_Message_Text = "A shortcut to the PM Master" & vbcrlf & "will be created on your desktop."
L_Welcome_MsgBox_Title_Text ="Windows Scripting Host Sample"
Call Welcome()
Dim WSHShell
Set WSHShell =CreateObject("WScript.Shell")
Dim MyShortcut, MyDesktop, DesktopPath
' Read desktop path using WshSpecialFolders object
DesktopPath =WSHShell.SpecialFolders("Desktop")
' Create a shortcut object on the desktop
Set MyShortcut =WSHShell.CreateShortcut(DesktopPath & "\PM-Master-ALL.lnk")
' Set shortcut object properties and save it
MyShortcut.TargetPath =WSHShell.ExpandEnvironmentStrings( "c:\Local Cloud\Shared\Sites\Bailey Lane\PM-Master-ALL")
MyShortcut.Save
Set Shell = CreateObject("WScript.Shell")
Set FSO = CreateObject("Scripting.FileSystemObject")
DesktopPath = Shell.SpecialFolders("Desktop")
FSO.DeleteFile DesktopPath & "\PM Master - ALL.lnk"
FSO.DeleteFile DesktopPath & "\PM Master - ALL - Shortcut.lnk"
WScript.Echo "A shortcut to the PM Master has been successfully created. The older PM Master shortcut has been deleted."
Sub Welcome()
Dim intDoIt
intDoIt = MsgBox(L_Welcome_MsgBox_Message_Text, vbOKCancel + vbInformation, L_Welcome_MsgBox_Title_Text )
If intDoIt = vbCancel Then
WScript.Quit
End If
End Sub
When in doubt, read the documentation. You can use the FileExists method to check whether a file exists before attempting to delete it:
shortcut = DesktopPath & "\PM Master - ALL.lnk"
If FSO.FileExists(shortcut) Then FSO.DeleteFile shortcut

Create a text file with the logged in user as its filename

I have this script that is creating and updating a text file called Machine.txt and populates it with a list of lines which have this format (username, time and date when the script ran).
I’m running this at logon.
What I want to do is to create a new text file for each user who logs in.
Example, if user is called fred I want the script to create fred.txt.
Any ideas?
Script been used is shown below i have replaced file location and name with "filepath and name"
thnak you for any help you can provide.
Set WSHShell = WScript.CreateObject ("WScript.Shell")
Set WSHNetwork = WScript.CreateObject ("WScript.Network")
Set WSHSysEnv = WSHShell.Environment ("PROCESS")
On Error Resume Next
' Check what OS is being used
Dim valOS
valOS = WSHShell.RegRead ("HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProductName")
Dim valPath, valLocation
Set WshShell = WScript.CreateObject("WScript.Shell")
Set objEnv = WshShell.Environment("Process")
valMachine = objEnv("COMPUTERNAME")
valDate = date
valTime = time
' This section of the script look for a hidden file on a users private share. This file is used to store a list of
' machines that a client has logged onto and the date and time that they logged on
Set oFSO = CreateObject ("Scripting.FileSystemObject")
If oFSO.FileExists ("filepath and name") Then ' If this file exists open the file for appending
Set oAppend = oFSO.OpenTextFile ("filepath and name",8) ' Open the text file for appending
oAppend.Writeline valMachine & "," & valDate & "," & valTime ' Write the machine name, date and time the the user logged onto the client
oAppend.close ' Close the text file connection
valPath = ""
valDate = ""
valTime = ""
Else
Set oStream = oFSO.CreateTextFile ("filepath and name") ' Create the file
Set objFile = oFSO.GetFile ("filepath and name") ' Attach to the file
objFile.Attributes = 2 ' Change the file attribute to hidden
oStream.Writeline valMachine & "," & valDate & "," & valTime ' Write the machine name, date and time the the user logged onto the client
oStream.close ' Close the text file connection
valPath = "" ' Clear the valPath variable
valDate = "" ' Clear the valDate variable
valTime = "" ' Clear the valTime variable
End If
wscript.quit
Try this:
Dim objNetwork
Dim userName
Set objNetwork = CreateObject("WScript.Network")
userName = objNetwork.UserName
Assign userName as your file name with a .txt extension

What the win cmd to open a particular spreadsheet in Excel?

I know that you can open an Excel file from the win cmd line. But how would you open a particular spreadsheet in that file using win cmd?
Paste the following code into a text editor (NotePad, WordPad, Word
etc)
Save the file with a "vbs" extension, for example
ExcelSheet2.vbs
Change this line strFileName = "c:\temp\testa.xlsx" to your
desired Excel file path
You can then run this from the commandline by entering the path name of your vbs file
The code has error handling in case the filepath is wrong, or a second sheet isn't present.
[Updated: added further error handling to test for the second sheet being hidden]
Const xlVisible = -1
Dim objExcel
Dim objWb
Dim objws
Dim strFileName
strFileName = "c:\temp\test.xlsx"
On Error Resume Next
Set objExcel = CreateObject("excel.application")
Set objWb = objExcel.Workbooks.Open(strFileName)
Set objws = objWb.Sheets(2)
On Error GoTo 0
If Not IsEmpty(objws) Then
If objws.Visible = xlVisible Then
objExcel.Goto objws.Range("a1")
Else
wscript.echo "the 2nd sheet is present but is hidden"
End If
objExcel.Visible = True
Else
objExcel.Quit
Set objExcel = Nothing
If IsEmpty(objWb) Then
wscript.echo strFileName & " not found"
Else
wscript.echo "sheet2 not found"
End If
End If
Alternatively you could open the workbook from the command line and add the below code to the Workbook to activate "Sheet2"
Private Sub Workbook_Open()
ThisWorkbook.Sheets("Sheet2").Activate
End Sub
You will need to make sure the workbook is in a trusted location and security settings allow the macro to run. #brettdj's solution is much superior but this is an alternative.

VBS Using createshortcut to get the name of a shortcut with spaces

I'm struggling... used google and have come up with no answers to this one!
I have a code that I'm intending to run at user logon which will find a shortcut and update the shortcut location to reflect some network changes - but the shortcut has spaces in it and VBS won't find the full target path... HELP!!!
The current target of the shortcut is:
\\LANG-APPS2\Mandata\Warehouse\Programs\StartApp.exe /sWH /ip192.168.73.124
But it will only return the bit up to .exe - it misses the last bit of /sWH /ip192.168.73.124
Here's my script:
On Error Resume Next
wscript.echo "Checking Warehouse Shortcut..."
Dim fso, folder, files, sFolder
Set fso = CreateObject("Scripting.FileSystemObject")
Set Shell = CreateObject("WScript.Shell")
sFolder = Shell.SpecialFolders("Desktop")
Set folder = fso.GetFolder(sFolder)
Set files = folder.Files
For each folderIdx In files
fullname = fso.GetAbsolutePathName(folderIdx)
Set shortcut = Shell.CreateShortcut(fullname)
shortTarget = LCase(shortcut.TargetPath)
shortWorkPath = shortcut.WorkingDirectory
lnkFind = ".lnk"
lnkSearch = instr(fullname, lnkfind)
if lnkSearch > 0 then
srvFind = "lang-apps2\mandata\warehouse\programs\startapp.exe"
srvSearch = instr(shortTarget, srvFind)
if srvSearch > 0 then
pracFind = "Practice"
pracSearch = instr(fullname, pracFind)
if pracSearch > 0 then
wscript.echo "Warehouse Practice Shortcut Needs Updating!"
wscript.echo "Please wait while I sort that out for you......"
shortcut.TargetPath = """\\Lang-man\Warehouse\Programs\StartApp.exe /sWHPRAC /ip192.168.73.134"""
shortcut.WorkingDirectory = "\\Lang-man\Warehouse\Programs"
shortcut.save
wscript.echo "Warehouse Practice Shortcut Updated!"
else
wscript.echo "Warehouse Live Shortcut Needs Updating!"
wscript.echo "Please wait while I sort that out for you......"
shortcut.TargetPath = """\\Lang-man\Warehouse\Programs\StartApp.exe /sWH /ip192.168.73.134"""
shortcut.WorkingDirectory = "\\Lang-man\Warehouse\Programs"
shortcut.save
wscript.echo "Warehouse Live Shortcut Updated!"
end if
end if
end if
set shortTarget=nothing
set shortWorkPath=nothing
set shortcut=nothing
next
wscript.echo "Finished"
From the description of the TargetPath property on MSDN (bold added by me):
This property is for the shortcut's target path only. Any arguments to the shortcut must be placed in the Argument's property.

Resources