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

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

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

VBS Command "launch.run" no longer executing

We have a vbs script that runs once a month. The script ran well for the most part but as a couple months ago one command stop working
The vbs script is executed by Windows Scheduler
Inside this script we set a variable called "launch"
Set launch = WScript.CreateObject( "WScript.Shell" )
and later in the program we use this command
launch.run("runthis.BAT")
We do not specify the full path in the command and to my knowledge we never have. This bat file resides in the same directory that the vbs script does. I guess this is why it was setup to run that way.
We are not sure what happened and the sys admins are not sure what could have changed to cause it to no longer work.
Please keep in mind we are not VBS specialists and this is something that has been in place for several years.
Any suggestions or resources to look at would be appreciated.
In order for us to provide a better answer, please update your question with the script you are having trouble with. We don't know what the launch object is - hopefully something other than WScript.Shell. Nevertheless you should be able to run a batch file this way:
Dim objShell
Set objShell = CreateObject("WScript.Shell")
objShell.Run "C:\your_folder\your_batch_file.bat"
Replace the launch.run part of your script with this code and update it with the correct path and name of your batch file (update "C:\your_folder\your_batch_file.bat" part).

vbs scheduled path not found

I cannot figure out what I'm doing wrong.
I have a batch file starting a vbs script.
The script just makes some operations in some files, like moving them, create, delete...
It works fine.
Executing the bat it starts the vbs script and everything work.
The bat file just makes a cscript file.vbs
The problem is that I've scheduled this bat file .
When the times come, it gets executed but I get the error "path not found" in the vbs script.
It's not a schedule task problem because I've 11 task running batch files and they run smootly, and the script is executed (I've put controllers on it).
But the vbs script returns always the same path not found error.
Again,if I execute the script manually, it runs without problems.
The task is scheduled with the same account I use to manual execute the file, so it's not a permission issue. I just doubleclick the batch and it runs, click on execute on the task schedule manager and it fails.
The system is windows server 2008 r2 standard.
I already tried to reboot, deleting and making a new task....
Thanks to everyone
[UPDATE]
I paste here part of the code
FILE: D:\scripts\conf.ini
[script1]
fileA=D:\Rep\exportA.csv
fileB=D:\Rep\exportB.csv
fileC=D:\Rep\exportC.csv
dirHistory=D:\Rep\history
FILE: D:\scripts\merge.vbs
Dim iniObj
Set iniObj=New ClsINI
If iniObj.OpenINIFile("D:\scripts\conf.ini") = False Then
wLog("Impossible to read file ini")
Set iniObj = Nothing
Chiudi()
End If
Dim errIni,tmpVal
Dim fileA,fileB,fileC,dirHistory
errIni = iniObj.GetINIValue("script1", "fileA", fileA)
tmpVal = iniObj.GetINIValue("script1", "fileB", fileB)
errIni = errIni+tmpVal
tmpVal = iniObj.GetINIValue("script1", "fileC", fileC)
errIni = errIni+tmpVal
tmpVal = iniObj.GetINIValue("script1", "dirHistory", dirHistory)
errIni = errIni+tmpVal
If errIni > 0 Then
wLog("Error loading file ini")
wLog(errIni)
iniObj.CloseINIFile()
Set iniObj = Nothing
Chiudi()
End If
wLog("File ini Caricato")
Dim objFso,posizioneFile,Fase
Dim arrElement,resArray,actionArray,cedoleArray,varArray ,i
Dim conn,rs,strCon
Dim maxPos,maxTemp
Dim objExcel, objSheet,cella
Set objFso = CreateObject("Scripting.FileSystemObject")
if objFso.FileExists(fileA) then
objFso.DeleteFile(posizione)
wLog("File posizione moved")
else
wLog("File posizione not found")
end if
At this line a get the error of "Path not found"
Set posizioneFile = objFso.OpenTextFile(fileA, 8, True)
If not objFso.FileExists(fileB) then
SendEmail("nego")
Fase=false
Else
Set tFile = objFso.OpenTextFile(fileB, 1)
strFile=tFile.ReadAll
tFile.Close
posizioneFile.WriteLine strFile
objFso.MoveFile fileB, dirHistory&"\Negoz_"& CreaId(2) & ".csv"
End If
posizioneFile.Close
FILE: D:\scripts\merge.bat
echo Start Merge %date% %time% >> Started.log
cscript D:\scripts\merge.vbs
Sorry if I didn't put it before, but I was thinking it was a windows issue, because I thought the code was fine.
Thanks
This sounds like an issue with the working directory although it's a bit difficult to tell, since you chose not to show the content of the batch script. If you start the script manually (by double-clicking), the working directory is the directory in which the batch script (and probably the VBScript as well) resides. If you run the batch script as a scheduled task, the working directory is %SystemRoot%\system32 unless you expressly set a working directory in the task's properties.
Now, if your batch script looks like this:
cscript.exe your.vbs
it will look for your.vbs in the working directory and won't find it if the working directory is not the directory containing your.vbs. If my assumption that both scripts are in the same directory is correct you could either set the working directory in the properties of the scheduled task or (better) change the batch script to something like this:
cscript.exe "%~dp0your.vbs"
%0 is the path to the batch script itself as it was called. %~dp0 expands %0 to the absolute path of the parent directory (including a trailing backslash).

Having difficulty running a program from my script

I'm struggling to run a program from my VBScript .vbs file. Substituting the line for a MsgBox works fine, so I know its definitely the "open program" line (oShell.Run("Release\My Application.exe")).
Anyway, this is a VBscript called start.vbs which is run from the root directory of a USB drive. It checks to see if .net framework 4.0 is installed and if it is then it should run My Application.exe (note the space... it might make a difference). My Application.exe is in a folder called Release.
As it is a USB drive I can't use explicit drive letters, eg: oShell.run("e:\Release\My Application.exe") although trying this also doesn't appear to work. Typing "e:\Release\My Application.exe" into a cmd window works fine, and runs the program as it is meant to work.
In the vbscript it just doesn't appear to do anything. Any reason?
Here is my code:
Option Explicit
Dim oShell
Dim value
On Error Resume Next
Set oShell = CreateObject("WScript.Shell")
value = oShell.RegRead("HKLM\SOFTWARE\Microsoft\.NETFramework\Policy\v4.0\30319")
If Err.Number = 0 Then
'Here I am struggling
oShell.Run("Release\My Application.exe")
Else
MsgBox("Version 4.0 of the .NET Framework is NOT installed.")
End If
I have commented above the line that doesn't work.
Thank you.
Not quite sure if this is the best answer, but I tried it and it work, so I'll just go ahead and post my idea.
I just used explorer.exe as a mediator and was able to open the file.
So the line that you're stuck with should probably become something like:
oShell.Run("explorer ""Release\My Application.exe""")
I tried it if it'll work in folder that is not in the System path and was able to open the executable.
Note : There may be better approaches.
Update: Thomas reported that the following code worked for him (the approach is better), the idea behind both approaches however is to quote the path (as you would do this in the Command Prompt, so filenames / folders with spaces in the path are properly resolved):
oShell.run("""Release\My Application.exe""")

Windows Script Host cannot find the file on startup

I am having here a vbs-file which starts a batch script. The batch should run invisible, so i use this vbs-script:
WScript.CreateObject( "WScript.Shell" ).Run "loop.bat",0,0
The vbs is registered to the run-key in the registry-
Everytime on startup it tells me that he cannot find the specified file.
Maybe the Windows Script Host is starting to late for my vbs-script?
What should I do?
vbs with delayed start? This needs another loop, but my script shall be invisible.
vbs in the startupfolder? Better taking the registry. For other reasons I cannot use the startupfolder.
Thanks for your answers :)
I know it's old... But i thought I'd put this in in case anyone stumbles here.
if you add something to run, when it starts unless otherwise specified it's working directory is C:\Windows\System32 so if you point to a file, "mybat.bat" it will assume it's in c:\windows\system32\mybat.bat
that's why you need to specify the whole path in the vbs.
if you are in a bat file then add this to the top of your file:
PUSHD %~dp0
Martyn

Resources