Create a desktop link by vbscript, ping with arguments, doesn't work [duplicate] - vbscript

This question already has an answer here:
VB Script: create shortcut to open file with specific program (mspaint)
(1 answer)
Closed 3 months ago.
I must create a desktop icon by a vbs script that execute a ping -t , like below, but doesn't work because in the target field of the link, is added double quote character at the beginning and at the end.
That is to say, instead of
"C:\WINDOWS\System32\PING.EXE" -t 192.168.000.187
is written
"C:\WINDOWS\System32\PING.EXE -t 192.168.000.187"
How can I solve this problem?
Set oWS = WScript.CreateObject("WScript.Shell")
sLinkFile = "C:\Users\nome cognome\Desktop\Ping POS GENGA01.lnk"
Set oLink = oWS.CreateShortcut(sLinkFile)
oLink.TargetPath = "C:\WINDOWS\System32\PING.EXE -t 192.168.000.187"
oLink.WorkingDirectory = "C:\WINDOWS\system32"
oLink.IconLocation = "E:\BATCH\POSPagoPA\PayPOSEth_Mod1_2.ico, 0"
oLink.Description = "Ping POS GENGA01"
oLink.WindowStyle = "1"
oLink.Hotkey = ""
oLink.Save

Thanks to #user692942 for the solution.
Set oWS = WScript.CreateObject("WScript.Shell")
sLinkFile = "C:\Users\andrea.rossetti\Desktop\Ping POS GENGA01.lnk"
Set oLink = oWS.CreateShortcut(sLinkFile)
oLink.TargetPath = "C:\WINDOWS\System32\PING.EXE"
oLink.Arguments = "-t 192.168.000.187"
oLink.WorkingDirectory = "C:\WINDOWS\system32"
oLink.IconLocation = "E:\BATCH\POSPagoPA\PayPOSEth_Mod1_2.ico, 0"
oLink.Description = "Ping POS GENGA01"
oLink.WindowStyle = "1"
oLink.Hotkey = ""
oLink.Save

Related

How to create link for executable file and indicate work folder in batch or VBS?

OS: Windows 7
I am trying to create a link to the desktop folder via batch like this:
mklink "%userprofile%\Desktop\MyExe" "%~dp0\MyExe.exe"
The command worked, but how to indicate MyExe.exe execute at "%~dp0"?
MyExe.exe looks like run at the current folder, so it can't load my config file.
Update:
Got the different problem by using VBS, run code in below will create a shortcut
for C:\Users\jiu\Desktop\MyExe.exe, But I want MyExe.exe.
Set oWS = WScript.CreateObject("WScript.Shell")
userProfilePath = oWS.ExpandEnvironmentStrings("%UserProfile%")
currParentFolder = CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName)
linkPath = userProfilePath + "\Desktop\MyExe.LNK"
Set oLink = oWS.CreateShortcut(linkPath)
oLink.TargetPath = "MyExe.exe"
oLink.WorkingDirectory = currParentFolder
oLink.Save
Based upon the information you have provided, here's a strange looking batch file which should create your desktop shortcut for you:
;#Rundll32 AdvPack.dll,LaunchINFSection "%~0",,1
;#GoTo :EOF
[Version]
Signature="$Windows NT$"
[DefaultInstall]
ProfileItems=AddLnk
[AddLnk]
Name="MyExe",8,16
CmdLine=1,,"MyExe.exe"
InfoTip="Execute MyExe.exe"
WorkingDir=1
You can optionally modify:
The shortcuts display nameby replacing the string inside the doublequotes on line 8.
The name of the target executableby replacing the string inside the doublequotes on line 9.
The shortcuts description commentby replacing the string inside the doublequotes on line 10.
This is I found:
Set oWS = WScript.CreateObject("WScript.Shell")
userProfilePath = oWS.ExpandEnvironmentStrings("%UserProfile%")
currParentFolder = CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName)
linkPath = userProfilePath + "\Desktop\MyExe.LNK"
targetPath = currParentFolder + "\MyExe.EXE"
Set oLink = oWS.CreateShortcut(linkPath)
oLink.TargetPath = targetPath
oLink.WorkingDirectory = currParentFolder
oLink.Save
mklink doesn't create a *.lnk shortcut but a symbolic/hard link which is just a new name for the physical file. As the link is in desktop, obviously the current folder will be desktop if you double click the link
You must create a shortcut. One way of doing that is by using the below vbs script
Set oWS = WScript.CreateObject("WScript.Shell")
strDesktop = WshShell.SpecialFolders("Desktop")
sLinkFile = strDesktop & "\MyExe.lnk"
Set oLink = oWS.CreateShortcut(sLinkFile)
oLink.TargetPath = WScript.Arguments(0) & "\" & WScript.Arguments(1)
' oLink.Arguments = ""
' oLink.Description = "MyExe"
' oLink.HotKey = "ALT+CTRL+F"
' oLink.IconLocation = WScript.Arguments(0) & "\" & WScript.Arguments(1) & ", 2"
' oLink.WindowStyle = "1"
oLink.WorkingDirectory = WScript.Arguments(0)
oLink.Save
Save it as and then call
cscript "%~dp0" "MyExe.exe"
You can also create it from powershell or various other tools
$objShell = New-Object -ComObject WScript.Shell
$lnk = $objShell.CreateShortcut("$home\Desktop\MyExe.lnk")
$lnk.TargetPath = ".\MyExe.exe"
$lnk.Save()

execute multiple batch file from VBscript

I am trying to execute multiple batch files in a folder using vbscript. can anyone help me how to do it.
Here is my code.
Varr1 = hostname
UN = username
password = pass
set ObjFSO = createobject("Scripting.FileSystemObject")
set FilePath = ObjFSO.getfolder("C:\test\script")
set BatFile = FilePath.files
for each m in BatFile
If LCase(objFSO.GetExtension(FilePath.files)) = "bat" Then
Set WShell = CreateObject("WScript.Shell")
WShell.Run ("CMD /K C:\test\script "&BatFile &" " & Varr1 &" "& UN &" "& password )
End If
Next
Given .BAT files like:
#echo off
echo a, $1, $2
in the current directory, a .VBS like:
Option Explicit
Const u = "user"
Const p = "passw"
Dim goFS : Set goFS = CreateObject("Scripting.FileSystemObject")
Dim goWS : Set goWS = CreateObject("WScript.Shell")
Dim f, c
For Each f In goFS.GetFolder(".\").Files
If "bat" = goFS.GetExtensionName(f.Name) Then
c = Join(Array("%comspec%", "/K", f.Name, u, p))
WScript.Echo "will call", c
goWS.Run c
End If
Next
will execute all of them in new consoles.
output:
cscript 47609016.vbs
will call %comspec% /K b.bat user passw
will call %comspec% /K a.bat user passw
(and some windows containing something like "a 'user' 'passw'")

Auto Import Script not Working

I'm an absolute beginner with VB hence I might ask some silly questions.
I have a VB script getting triggered via a Batch file which results in data being imported for last day.
Below is the code for VB and Batch file.
Please let me know if you see any error in the code.
VB Script
rem
rem XLink_Import.vbs
rem
Set oShell = WScript.CreateObject("WScript.Shell")
' filename = oShell.ExpandEnvironmentStrings("today_xlink.bat")
' Set objFileSystem = CreateObject("Scripting.fileSystemObject")
' Set oFile = objFileSystem.CreateTextFile(filename, TRUE)
Dim i
Dim ImportStartOffset, ImportedNumberOfDays
If WScript.Arguments.length > 0 Then
For i=0 to WScript.Arguments.length-1
Arg = WScript.Arguments(i)
If Left(Arg,1) = "-" Then
If ( Arg = "-o" ) Then
ImportStartOffset = WScript.Arguments(i+1)
End if
If ( Arg = "-n" or Arg = "-l" ) Then
ImportedNumberOfDays = WScript.Arguments(i+1)
End if
End if
Next
End If
rem Prepare the import start date
Dim Dy, Mth
Dim ImportDate
ImportDate = Now + ImportStartOffset
Dy = Day(ImportDate)
Mth = Month(ImportDate)
If Len(Dy) = 1 Then Dy = "0" & Dy
If Len(Mth) = 1 Then Mth = "0" & Mth
ImportStartDate = Dy & "/" & Mth & "/" & Year(ImportDate)
rem Prepare import script to run (not useed yet)
rem oFile.WriteLine("isps_ul.exe -t -d " & todaydate & " -L 1")
rem oFile.Close
rem Run XLink import
wscript.echo "isps_ul.exe -t -d " & ImportStartDate & " -L " & ImportedNumberOfDays
oShell.Run "isps_ul.exe -t -d " & ImportStartDate & " -L " & ImportedNumberOfDays, 1, true
Batch File
#echo off
rem
rem XLink_Import.bat
rem
rem Manually starts an Xlink import starting today + a StartOffset of some days.
rem Imported number of days can also be set.
rem
set ImportStartOffset=0
set ImportedNumberOfDays=1
cscript XLink_Import.vbs -o %ImportStartOffset% -n %ImportedNumberOfDays%
pause
You don't need both a batch and a script, one of either would be enough, doing the whole thing in batch would require with some suggling with special parameters and I'm not into that so I'll adapt your script a bit like below.
Since you keep your 2 confiuration variables in the windows environment you can read them from vbscript as well, other option would be to read from a configuration file, from the command line like you did or keep the in the script itself.
Your middle part, makig sure the date is speleld correct could be omitted if you set those dates correctly in the configuration (environmentvariables).
If your import is going to work you should check before by running what is displayed as command, so eg "eisps_ul.exe -t -d28/11/2016 -L" should run, otherwise search on that problem first.
What I meant in my comment about being DRY means you should not repeat things, in case of your command you can store the concatenated command in a variable and use that for viewing and running.
Dim ImportStartOffset, ImportedNumberOfDays, oShell, command, Dy, Mth, ImportDate, ImportStartDate
Constant WaitOnReturn = true, WindowStyle = 1 '1 = Activate and display
'read configuration environment variables
Set oShell = CreateObject( "WScript.Shell" )
ImportStartOffset = wshShell.ExpandEnvironmentStrings( "%ImportStartOffset%" )
ImportedNumberOfDays = wshShell.ExpandEnvironmentStrings( "%ImportedNumberOfDays%" )
'Prepare the import start date (not necessary if environmentvariables would be configured well)
ImportDate = Now + ImportStartOffset
Dy = Day(ImportDate)
Mth = Month(ImportDate)
If Len(Dy) = 1 Then Dy = "0" & Dy
If Len(Mth) = 1 Then Mth = "0" & Mth
ImportStartDate = Dy & "/" & Mth & "/" & Year(ImportDate)
'Run XLink import
command = "isps_ul.exe -t -d " & ImportStartDate & " -L " & ImportedNumberOfDays
wscript.echo command
oShell.Run command, WindowStyle, WaitOnReturn
Set oShell = Nothing

calling CMD with VBS hangs at echo

im exierience a strange problem.
im trying to call a cmd with a vbs script and it hangs at the uttermost strange point.
heres the oExecShell.StdOut.ReadLine of my cmd that hangs :
set targetDir=\\Backupstorage\Servername$\2014-11-28_12-00
if not exist \\Backupstorage\Servername$\2014-11-28_12-00 (mkdir \\Backupstorage\Servername$\2014-11-28_12-00 )
Rem Works Fine
set LogDir=\\\Backupstorage\Servername$\2014-11-28_12-00\Logs
Rem Does not work for some strange Reason
echo "here"
"here"
and then it hangs, the line that kills it/should follow is :
echo %LogDir%
echo "we have the problem"
anybody has an?
heres some of the code that starts the cmd :
strAction = "C:\makeBackup_test.cmd"
Dim WshShell : Set WshShell = CreateObject("WScript.Shell")
strAction = Wshshell.expandenvironmentstrings(strAction)
Dim oExecShell : Set oExecShell = WshShell.Exec(strAction)
Do While oExecShell.Status = 0
WScript.Sleep 100
Loop
writelog("... ExitCode is: " & oExecShell.ExitCode)
LastShellOutput = ""
Do While not(oExecShell.StdOut.AtEndOfStream)
LastShellOutput = LastShellOutput & oExecShell.StdOut.ReadLine & vbCrlf
Loop
writelog("StdOut: " & LastShellOutput)
anyone has an idea?
Calling int with CMD /C solved it. (strAction = "CMD /C C:\makeBackup_test.cmd")

VBS File - Pass InputBox input to SLinkFile?

So, I have no experience in vbs really, the most I know is writing pretty basic strings in access. So, after spending an hour trying to figure this out, I'm coming here for help.
What I am trying to do is write a .vbs script that prompts the user to enter the IPAddress of the local machine and then amends that to the of the oLink.TargetPath (After the =)
So far all I have is:
Dim IP_Address
Ip_Address = Inputbox("Enter The Machine IP")
Set oWS = WScript.CreateObject("WScript.Shell")
sLinkFile = "C:\Dropbox\Personal\Latitude e6430 - Work\Desktop\timekeeping.LNK"
Set oLink = oWS.CreateShortcut(sLinkFile)
oLink.TargetPath = "http://w-sch-fooddb:8675/lfserver/timecard?ip_address="
oLink.Description = "Clock in or out"
oLink.IconLocation = "%SystemRoot%\system32\SHELL32.dll, 111"
oLink.Save
String concatenation is done via the & operator:
>> Ip_Address = "1.2.3.4"
>> TargetPath = "http://w-sch-fooddb:8675/lfserver/timecard?ip_address=" & Ip_Address
>> WScript.Echo TargetPath
>>
http://w-sch-fooddb:8675/lfserver/timecard?ip_address=1.2.3.4
>>

Resources