VB Script to Delete Desktop shortcut - vbscript

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

Related

CMD script press button

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

How to Secure HTML Application (HTA)?

I have created one GUI using below code in HTA:
Sub result ( )
Dim strComputer, objShell, objExec, strPingResults, WshShell
strComputer = Trim( UCase( ComputerNameTextBox.Value ) )
Set WshShell = CreateObject("WScript.Shell")
Set objExec = WshShell.Exec("cmd.exe /c C: \Program Files (x86)\VMware\VMware vSphere CLI\bin & esxcli -s "&strComputer&" -u root -p NTMC_Admin network nic list ")
strPingResults = LCase(objExec.StdOut.ReadAll)
ResultsTextArea.Value = ""
ResultsHiddenText.Value = strComputer & vbTab
ResultsHiddenText.Value = strComputer & vbcr & vblf & strPingResults
ResultsTextArea.Value = ResultsTextArea.Value & strPingResults
CopyButton.Disabled = False
ResetButton.Disabled = False
' change mouse pointer back to default
Document.Body.Style.Cursor = "default"
End Sub
Please help me out to secure my code so that no one can see the this function under any view source.
You should try to convert your vbscript and your HTA to executables files !
(VbsEdit/HTAEdit), it's a package includes (VbsEdit 32-bit, VbsEdit 64-bit, HtaEdit 32-bit and HtaEdit 64-bit). has a good feature is to compile and convert your vbs or your HTA files into .exe and it is totaly free.
hta_code.exe extract All resources And HTA either Into Temp folder Of System Or User specified Folder.u can easily reach temp folder while hta is running and copy extracted folder to a safe location because after Hta been closed Extracted command in .exe Delete all files from temp folder.to locate hta in temp folder we can use window search by typing .hta............#teemu is right #hackoo m not sure about htaedit but i will check this out today

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.

VBScript that Opens an ini file and a Config file in notepad

I work in a hospital environment and right now im doing PC deployments. Part of the deployment requires us to view 2 files on a network drive looking for information regarding the old systems. They use specific ports and or TTY's to view information in each department.
I am trying to create a VBS file that can open 2 files in 2 different notepad windows. The first one opens up but the pcview.cfg keeps giving me an error. Im trying to link to the same location that the HBOWEM32 is pointed to. Can anyone solve? For security reasons I have taken out the exact location of the network drive. The code below prompts for a specific folder name which is the old pc name. After entering that data it opens the HBOWEM32 files fine but says it cannot find the other part. I Have manually looked inside the folder and the pcview.cfg file DOES exist. I just want a faster way of opening these rather than brute forcing through the run prompt.
Here is the code.
CONST strDir = "<Netowrk Location)"
Dim WshShell
set objShell = CreateObject("WScript.Shell")
set objFSO = CreateObject("Scripting.FileSystemObject")
function findFolder(strDir, strFlag)
set objFolder = objFSO.GetFolder(strDir)
for each objSubFolder in objFolder.SubFolders
if (inStr(objSubFolder.Name, strFlag)) then
findFolder = objSubFolder.Path
exit function
else
findFolder = findFolder (objSubFolder.Path, strFlag)
end if
next
end function
strFlag = inputBox("Enter Computer Name:")
strWeb = findFolder(strDir, strFlag) & "\HBOWEM32.ini"
objShell.Run strWeb
Set WshShell = CreateObject ("WScript.Shell")
WshShell.Run ("notepad.exe """ + "\\<same location as above>\Pcview.cfg""")
Use Option Explicit
Don't create variables you don't use (WshShell, objShell)
Improve your variable names (strFlag seems to be a computer name, strWeb seems to be the full specification of a file)
Don't lump different info into one variable (strWeb contains the folder path to re-use and the specific file name)
Use diagnostics output (at least while developing)
In code:
Option Explicit
...
Dim strComputer : strComputer = InputBox("Enter Computer Name:")
Dim strFolder : strFolder = findFolder(strDir, strComputer)
Dim strIniFSpec : strIniFSpec = objFSO.BuildPath(strFolder, "HBOWEM32.ini")
WScript.Echo "will run '" & strIniFSpec & "'"
objShell.Run strIniFSpec
Dim WshShell : Set WshShell = CreateObject("WScript.Shell")
Dim strCfgFSpec : strCfgFSpec = objFSO.BuildPath(strFolder, "Pcview.cfg")
Dim strCmd : strCmd = "notepad.exe """ & strCfgFSpec & """"
WScript.Echo "will run '" & strCmd & "'"
WshShell.Run strCmd
(not tested, please be carefull)

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