I would like to run a batch file with some extra character at the end.
If I write it directly to cmd it is working, like change directory to "c:\Users\Public\Uploader\" and write the following: start.bat "cmd:file.import c:\Users\tom\Desktop\a.xml"
I do not know how to write it in VBScript, because the following script is not working:
Dim WshShell
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.CurrentDirectory="C:\Users\Public\Uploader\"
WshShell.Run "start.bat" & "cmd:file.import C:\Users\tom\Desktop\a.xml"
The a.xml should dynamically change based on the last modified file in the folder.
Thank you for your help!
If you're trying to duplicate what you're doing in the command prompt, you need to add a space between the batch file name and its arguments. You also need to add the quotes.
Dim WshShell
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.CurrentDirectory="C:\Users\Public\Uploader\"
WshShell.Run "start.bat " & chr(34) & "cmd:file.import C:\Users\tom\Desktop\a.xml" & chr(34)
Related
I have spent several hours trying to get this to work. For some reason, my file paths register just fine in command prompt, but if I try to reference the file path using a FileSystemObject it doesn't work and tells me the file can't be found.
Dim g_shell
Set g_shell = CreateObject("WScript.Shell")
strCurDir = g_shell.CurrentDirectory
strValue = someName 'This is actually passed from a function argument
Set objFSO = CreateObject("Scripting.FileSystemObject")
configPath = strCurDir & "\maintLogs\" & strValue & "\MaintGuy_" & strValue & ".config"
Set objStream = objFSO.OpenTextFile(configPath) ' This throws a file not found error
g_shell.Run "cmd /c" & configPath & "& pause" 'This opens the file with no problem
'Let's Try something else:
Set objFSO = CreateObject("Scripting.FileSystemObject")
aPath = objFSO.BuildPath(g_shell.CurrentDirectory, "maintLogs")
bPath = objFSO.BuildPath(aPath, strValue)
cPath = objFSO.BuildPath(bPath, "MaintGuy_" & strValue & ".config")
Set objStream = objFSO.OpenTextFile(cPath) ' still doesn't work
g_shell.Run "cmd /c " & cPath & "& pause" ' This works (opens the file)
So my question is, why does this give me a file not found, when it works in command prompt and is an existing file. What am I missing?
In the example I am trying to read a text file, but I also could not get these file paths to work when I was trying to create a folder, move files into folders, and other file manipulation techniques. I accomplished it through launching the command prompt but could not get them to work using Scripting.FileSystemObject service.
Operating System: Windows 10
Scripting Language: VisualBasic Script
Application: SecureCRT
I figured it out. It turns out, because I was moving a file into the location I was trying to read from, it was trying to read the file BEFORE the file was there. I added a sleep function to the code, and it worked!
I've got registry entries inside of a .reg file. The reg file is for making a URI Scheme. It worked when I was passing %1 argument to a .bat file, but I don't like viewing the .bat file, so I want to use the well known VBScript method of hiding the .bat file. How do I pass the arguments to the batch file?
Apps.reg
REGEDIT4
[HKEY_CLASSES_ROOT\Apps]
#="URL:Apps Protocol"
"URL Protocol"=""
[HKEY_CLASSES_ROOT\Apps\shell]
[HKEY_CLASSES_ROOT\Apps\shell\open]
[HKEY_CLASSES_ROOT\Apps\shell\open\command]
#="\"M:\\Apps\\Apps.vbs\" \"%1\""
Apps.vbs
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "Apps.bat" & Chr(34), 0
Set WshShell = Nothing
The batch file just uses %1 argument. The problem is getting the argument to the batch file. Currently every time I try to use the URI Scheme with the VBScript, I get "apps://foo is not a valid Win32 application." Any help is appreciated!
This works:
WshShell.Run "Apps.bat /foo", 0
If you want to separate your program path which may include spaces from the command line you could try this:
WshShell.Run """Apps.bat"" /foo", 0
I'm trying to simplify an install for a group of people I work with that have very little computer skills. I have a vbs script that copies various configuration files in to their proper directories. However, there is one file that I cannot get to copy.
I am trying to copy a new file called hosts in to the C:\windows\systems32\drivers\etc folder and I keep getting permission denied no matter what I do.
Const OverWriteExisting = True
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CopyFile "C:\users\IBM_ADMIN\Desktop\Colgate Socks\hosts", "C:\Windows\System32\drivers\etc\hosts", OverWriteExisting
Any ideas?
Run as administrator?
You can do this through task scheduler easily.
Another option was from How to run scripts as administrator in Windows 7?
Put this at the start of your script
Set WshShell = WScript.CreateObject("WScript.Shell")
If WScript.Arguments.length = 0 Then
Set ObjShell = CreateObject("Shell.Application")
ObjShell.ShellExecute "wscript.exe", """" & _
WScript.ScriptFullName & """" &_
" RunAsAdministrator", , "runas", 1
Else
[your code here]
end if
Use this %windir%\Sysnative instead of C:\Windows\System32.
I'm trying to run a run the following command line for each specific file type (as example for each .txt file) in the current directory:
"C:\Program Files (x86)\some program\someprogram.exe" "file.txt" "file.txt.mod" -someparameter
When I run this exact command from an open Windows command prompt (including all the quotation marks), it works.
But when I run it through this VB, it does nothing/closes right away.
What am I doing wrong? I have a feeling it has to do with the quotes, but my head can't sort it out.
Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = left(WScript.ScriptFullName,(Len(WScript.ScriptFullName))-(len(WScript.ScriptName)))
Set objFolder = objFSO.GetFolder(objStartFolder)
Set colFiles = objFolder.Files
For Each objFile in colFiles
strFileName = objFile.Name
If objFSO.GetExtensionName(strFileName) = "txt" Then
RunCommand()
End If
Next
Sub RunCommand
Set oShell = WScript.CreateObject ("WScript.Shell")
oShell.run "cmd.exe /C ""C:\Program Files (x86)\some program\someprogram.exe"" """ & objFile.Path & """ """ & objFile.Path & ".mod"" -someparameter"
Set oShell = Nothing
End Sub
You should
Reduce risk of failures
by
using "Option Explicit"
avoiding clever "roll your own" hacks by using standard methods (.GetParentFolderName) instead
using type prefixes correctly (objStartFolder)
avoiding variables just used once (objFolder, colFiles)
not using globals to pass parameters into Subs/Functions (objFile)
avoiding (unnecessary) stress (.Run without wait, new WScript.Shell for each file, "cmd" instead of "%comspec%")
using cscript in a 'dos box' instead of double click/wscript
and
check your assumptions
by
diagnostic output (.Echo objFile.Name immediately before calling RunCommand, use a variable to store and .Echo the command send to .Run)
Check return values of functions that provide diagnostics (.Run)
sanity checks like:
(just to tame the formatter)
>> WScript.Echo goFS.GetExtensionName("A.TXT")
>>
TXT
I am using the following code to call a batch file:
dim shell
set shell=createobject("wscript.shell")
shell.run "a.bat D:\a"
set shell=nothing
How do I call more than 1 batch file, so that when the 1st file's execution is over the 2nd file is executed.
as always, I really appreciate any help offered.
Below
shell.run "a.bat D:\a"
add another line with another
shell.run "b.bat ...."
Or create a batch file that calls all the other batch files, and call that batch file from your script.
Option explicit
Dim oShell
set oShell = Wscript.CreateObject("WScript.Shell")
oShell.Run "RunAs /noprofile /user:Admininistrator ""%comspec% /c 1stcommand && 2ndcommand && 3rdcommand""", 1, false
WScript.Sleep 1000
oShell.Sendkeys "AdmininistratorPassword~"
Wscript.Quit