"Path not found" error in VBScript when creating a file - vbscript

I am making a simple VBS application which has a bit where it creates a file in the startup directory. When I did it a while back, it worked, but I've since updated my code and need it to do other things too.
This is the code that creates the file:
Dim fso, openFile
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CreateTextFile "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\startup\file.bat"
It gives some sort of error saying it does not know where to go, I don't know if there's a problem with the file path I've supplied, because C:\ProgramData\Microsoft\Windows\Start Menu\Programs\ is a folder, obviously. I've also just discovered that there is no startup\ folder in it, is that what's causing it?
If so, is there something I can specify that creates the directory if it does not exist?
I'm very new to VBS and I have a very limited knowledge of it, so a simple way to do this would be much appreciated.
Thanks! :)

Is the filepath correct? (can you find it mannualy?) If yes, then try to copy the path in a variable and call the variable with the path.
Dim fso, openFile
Dim filepath = "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\startup\file.bat"
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CreateTextFile(filepath)
And also make sure, that the file don't exists before you start the programm.
Good Luck
Jonas

Related

Extracting ZIP file to absolute path

I am trying to create a VBS file that can extract the content of a ZIP file from a relative path to an absolute file path destination. The reason I am trying to create this VBS file is because my overall goal is to create an EXE file through iExpress that will copy a bunch of code to a user's computer and install nodeJS at the same time.
My thoughts on taking this approach have been to create a batch script that would copy the directory of code to a set position on the user's computer, and then execute an MSI file to install nodeJS. I have been able to get a batch script that can do that, but now I want to package it all nicely into an EXE. For this I believe I need to put the code directory into a ZIP file so I can add it to the EXE file, and then my EXE needs some functionality to extract this.
To add this functionality, my research seems to indicate that VBS is the best option for this (especially considering I am hoping to also compensate for users who have restricted access to PowerShell cmdlets). Searching around seems to indicate that a file of this sort of variety is meant for what I want to do.
ZipFile="test.zip"
ExtractTo="C:\test-install\test"
Set fso = CreateObject("Scripting.FileSystemObject")
sourceFile = fso.GetAbsolutePathName(ZipFile)
destFolder = fso.GetFolder(ExtractTo)
Set objShell = CreateObject("Shell.Application")
Set FilesInZip=objShell.NameSpace(sourceFile).Items()
objShell.NameSpace(destFolder).copyHere FilesInZip, 16
Set fso = Nothing
Set objShell = Nothing
Set FilesInZip = Nothing
This does not seem to work though as on this line...
objShell.NameSpace(destFolder).copyHere FilesInZip, 16
... it complains to me about destFolder needing to be an object. I am not too sure what I am meant to change to fix this error (I am not too familiar with VBS) and most examples on the internet set the ExtractTo variable as a relative file path initially and run a command to turn this into an absolute file path. I don't want to do this though as when iExpress turns it all into an EXE my understanding is that the relative file path starts in some random TEMP folder somewhere.
Any guidance on the error in my VBS file, or indication of a better path to take for extracting ZIP files would be greatly appreciated, thank you.
It seems my inexperience with VBS was the root of my troubles here. The trouble ended up being that on this line:
objShell.NameSpace(destFolder).copyHere FilesInZip, 16
It needed to be this:
objShell.NameSpace(destFolder).copyHere(FilesInZip), 16
I am not sure if this is specific to a certain version of the language or not, because my error was copied from numerous examples of a similar script across the internet, but I haven't really been able to find further details on this (I will edit this answer if more is found).

FSO - DeleteFile issue

I'm running a routine delete all files within a folder, then delete the folder. It works, but always seems to leave one file behind. Assume some kind of file lock, but ideas on how to clear the lock would be appreciated.
If I manually delete the 'rogue' file, and rerun the routine the folder is deleted - so all of my folder permissions are correct.
thanks
fso.DeleteFile objFolder & "\*.pdf", true
fso.DeleteFolder objFolder, true
I've located the issue - it was down to a PDF object further up my page. Once I'd set this to 'Nothing' the fso delete command worked a treat.
thanks

VBS CurrentDirectory trouble

I have a very simple script, which only prints current directory. That's the code:
set WshShell = WScript.CreateObject("WScript.Shell")
Wscript.Echo (WshShell.CurrentDirectory)
This script is called from .exe file. It works fine until the calling executable was run directly. If I create a link to exe-file and launch it, then it runs my .vbs and it prints the directory of link, not the .exe itself! How can I fix this?
Get help from FileSystemObject, (vbscript example) :
scriptdir=CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName)
Wscript.Echo scriptdir
OK, maybe it's somehow clumsy, but I've discovered a workable solution. The idea is simple: get full script name and a short one. Then subtract the second from the first.
set WshShell = WScript.CreateObject("WScript.Shell")
Wscript.Echo (Left(WScript.ScriptFullName, Len(WScript.ScriptFullName) - Len(WScript.ScriptName)))

Script for changing shortcut targets in a batch?

My external hard drive was recently affected by the recycler.exe virus when I lent it to a friend. The virus affects only external drives. It changes the folders into shortcuts to those folders via an EXE file that it creates.
I am searching for a script to change all the shortcut targets to K:\{shortcut name}. However, I don't know scripting and in the last two days that I have tried to learn scripting, I am not sure which one I should use. VBScript seems the best option, but that's just my opinion.
Problem:
For example, earlier I had a folder called 'Anime'. Now I have a shortcut linking to that folder with the following target:
%windir%\system32\cmd.exe /c "start %cd%RECYCLER\894133bf.exe &&%windir%\explorer.exe %cd%Anime
The virus also creates a folder(and file) \RECYCLER\894133bf.exe and the shortcuts are linked through that EXE file (seen above).
I would like a batch file to convert the target path to:
K:\Anime
This way the shortcut can directly link to the file. The virus and folder were removed by Norton when I got my hard drive back. However, the shortcuts remain and they don't work unless I change the target path.
Since I have over 37 folders on my hard drive which have been converted to the shortcuts, I was thinking that maybe a script would be helpful doing the following:
Extracting the filename from the shortcut
Removing the .lnk from filename
Changing the target to K:\{shortcut name}
Go to next folder and loop until last folder
I came across a script to extract a filename here:
http://blogs.technet.com/b/heyscriptingguy/archive/2006/05/30/how-can-i-extract-just-the-file-name-from-the-full-path-to-the-file.aspx
However, I do not know how to put it in a loop to do it for each folder.
Any help will be much appreciated. Thank you very much.
objFolder.Name in the For Loop is the string for each Folder it's iterating through.
Option Explicit
Dim strFolderToSearch, objFSO, objRootFolder, objFolder, colSubfolders, strOutput
strFolderToSearch = InputBox("Enter Path:")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objRootFolder = objFSO.GetFolder(strFolderToSearch)
Set colSubfolders = objRootFolder.SubFolders
For Each objFolder in colSubfolders
strOutput = strOutput & objFolder.name
strOutput = strOutput & vbCrLf
MsgBox StrOutput
Next

InstallShield removing a file using VBScript & CustomAction fails when there's no file

When uninstalling a previous installation (that I'd built using InstallShield 2009), I wanted to delete the entire folder that the program was in at the end of the uninstall. I couldn't figure out how to do that using a Custom Aaction, so using the code below, I settled for deleting the file as soon as the installation starts. This works fine if the program was already installed... but if it wasn't installed before, it throws an error 1701 because, obviously, the folder didn't exist! I have no idea how to fix this issue and I know almost no VBScript. I started to do a try-catch to just gloss over the error, but apparently that doesn't exist in VBScript.
Dim fso, Folder2Delete
Folder2Delete = "C:\Program Files\MyProgramDir"
Set fso = CreateObject("Scripting.FileSystemObject")
fso.DeleteFolder(Folder2Delete)
So either, How can I stick a Custom Action into an uninstall in InstallShield, or how can I set the VB script to only delete a file if it exists? Or last ditch, how can I get it to not show an error when it doesn't exist... ?
Thanks a lot, this is driving me crazy!
You can try this code:
Dim fso, Folder2Delete
Folder2Delete = Session.Property("CustomActionData")
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FolderExists(Folder2Delete) Then
fso.DeleteFolder(Folder2Delete)
End If
For this custom action you can then set the action data (CustomActionData property) to:
[INSTALLDIR]
This way your action will delete whatever installation path your users set.

Resources