VBS CurrentDirectory trouble - vbscript

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)))

Related

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

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

VB Using "Runas"

I am having a heck of a time getting some syntax correct here.
I have a small VB Script which prompts for credentials and then uses those credentials to run another vb script:
set objShell = WScript.CreateObject("WScript.Shell")
strAdminName = inputBox("What is your username (Domain\Username)")
objShell.Run "runas /user:" & strAdminName & " ""Wscript.exe \\xxx.xx.xxx\dfs\Tumw-IS\Juniper Tools and Utilities\Juniper Removal Tools\delete_folders.vbs"" "
When I run the script everything works, except it can't find the script I am calling. I get an error stating "There is no file extension in "\xxx.xx.xxx\dfs\tumw-is\Juniper"
Obviously the problem is in the fact that there are spaces in the file location and also that I am using a DFS link. When I run this and target a VBS file with no spaces in the location it works fine.
I just can't get the syntax down to handle the DFS link or the spaces in the name. I assume it's pretty straightforward, but I just can't get it to work.
Any ideas?
Thanks
-John
Try this:
objShell.Run "runas /user:" & strAdminName & " ""Wscript.exe \""\\xxx.xx.xxx\dfs\Tumw-IS\Juniper Tools and Utilities\Juniper Removal Tools\delete_folders.vbs\"""""

writing registry values in ROISCAN script

I am using following VBScript in ROISCAN script to write registry values, but it is not working, if I execute the script independently it is working fine. not sure why, appreciate your help.
Set WshShell = CreateObject("WScript.Shell")
myKey = "HKEY_LOCAL_MACHINE\SOFTWARE\MyKey\MySubKey\MSOfficeBit"
WshShell.RegWrite myKey,12,"REG_SZ"
Set WshShell = Nothing
ROISCAN Script URL:
http://gallery.technet.microsoft.com/office/68b80aba-130d-4ad4-aa45-832b1ee49602
it has to be a permissions issue in ROISCAN. www.microsoft.com/sysinternals there is a program called filemon(and you can filter for just one executable so you don't see everything executing on your computer, which will show you RIOSCAN running and what user and if it found the file, and if there was file permission errors

VBS script (VBScript) errors 800A0035 and/or 800A004C only on the first execution of the script

I am pretty new to all this VBS stuff because basically all I need to do is to make one simple VBS script, which I have possibly written, however, my problem is that it gives me 800A0035 or 800A004C error when I execute it for the first time on a particular PC, as soon as I execute it for the second time, it runs just OK and does what it is supposed to do. Incidentally, on my own computer it works OK even on the first execution.
I know that the errors have something to do with the wrong paths but I have checked my script several times and I am 100% positive that they are correct.
Here is the script:
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.MoveFile "rar.bat" , "rarp.bat"
'HideBat.vbs
CreateObject("Wscript.Shell").Run "rarp.bat", 0, True
What the script is supposed to do is to rename the rar.bat file to rarp.bat and run that batch file (rarp.bat) without popping up the command prompt. What the batch file does is not relevant, I guess, but anyway, it just runs WinRAR.
The rar.bat file and the VBS script are in the same folder, that's why I have used relative paths in the script. I cannot use absolute paths because I need to run the script on several computers.
I have read somewhere on the internet that by default VBS script first looks for the files in C:\Windows\System32 when relative paths are used. I have even tried using absolute paths in the script but it didn't work either. Here is how I need them to look like: %systemdrive%\users\%username%\appdata\roaming\rar.bat but this simply didn't work in the VBS script.
I really think that what I need is really a simple script but apparently it's pretty hard to get it working properly. I will be very grateful to those who help me.
Thank you a lot in advance.
Regards.
The only way your script - at least the part published - can give an error is by not finding the source file for renaming, you should have added full script and error message to be sure.
I suppose this is caused by a security setting on your pc that are more forgiving than on the rest of the pc's, eg UAC ? On the other pc's, try to put the files in a map like c:\test and then run it again after checking that the file rar.bat does exist in the same map. Do you have the same credentials (admin) on the other pc's ?
If you just want to run the bat file hidden, why the renaming ?
how do you download the bat ? and how then is invoked the script ? could be a timing issue that the second time is no longer a problem. In that case check in your script if the file is allready there and do a sleep in a loop while it doesn't
If you want to use the absolute path you could try this
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set WshShell = CreateObject("WScript.Shell")
Set WshSysEnv = WshShell.Environment("PROCESS")
path = WshSysEnv("USERPROFILE") & "\appdata\roaming\rar.bat"
wscript.echo path
objFSO.MoveFile path , "rarp.bat"
CreateObject("Wscript.Shell").Run "rarp.bat", 1, True

Wscript.Shell Run doesn't work consistently

I'm trying to run the following bit of code in a vb6 dll:
Dim objWSShell As Object
Set objWSShell = CreateObject("Wscript.Shell")
objWSShell.Run strPath & "test.bat", 0, True
The dll process gets hung up. The batch file will not run, no matter what its contents. I even tried an empty batch file and it still hung up. However, if I try this same piece of code, with this change:
Dim objWSShell As Object
Set objWSShell = CreateObject("Wscript.Shell")
objWSShell.Run "calc", 0, True
It works fine. I can't figure out why exe files work and bat files don't. Any ideas?
You don't need to use the shell scripting stuff, you can make things simpler & use the built in Shell() function:
shell environ$("COMSPEC") & " /C c:\xxx\yyy.bat", vbNormalFocus
Ditto for:
shell "calc", vbNormalFocus
You need to run cmd.exe and pass your BAT file to it.
objWSShell.Run "%COMSPEC% /c " & strPath & "test.bat", 0, True
I had a similar issue where batch files couldn't be run directly from WScript.Shell, but I didn't have access to modify the VBScript. It turns out there was a registry override on the .bat extension.
While using COMSPEC worked for me, deleting the registry key actually fixed more than just the WScript problem.

Resources