Passing variables from VBs to Batch File - batch-file

I'm just struggling with this seemingly simple code:
a = objNode.Text
dim WshShell
set WshShell=Wscript.Createobject("Wscript.shell")
WshShell.Run chr(34) & "C:\Users\c1921\Ayla_Data\ccode.bat " & Chr(34) & a & filename 'this line here does not work right.
Set WshShell = Nothing
The code does almost what I want it to. This is the batch file code:
#echo off
echo test passed variables:
echo %1
echo %2
pause
But echo %1 returns both values and echo %2 returns echo off:
219270AC000N000009132
ECHO OFF
I don't understand what I'm doing wrong. I've tried adding extra quotes in this line:
WshShell.Run ""C:\Users\c1921\Ayla_Data\ccode.bat "" a & filename 'variation 1
WshShell.Run """C:\Users\c1921\Ayla_Data\ccode.bat """ a & filename 'variation 2
WshShell.Run chr(34) & "C:\Users\c1921\Ayla_Data\ccode.bat " & Chr(34) & a + filename 'variation 3
I don't understand what I'm missing here?
Any help in the right direction would be great. If there's any helpful documentation on using vbs and batch that would be great too.
DM

... & a & filename
is the problem. You just concatenate the arguments, instead of separating them with a space. That's why the batch file only sees the first argument which contains both at once. echo %2 will then yield ECHO is off. because %2 is empty.
... & a & " " & filename
should work. For some values of work, I guess. If either a or filename contains spaces it'll probably blow up. You can add quotes around them to prevent that:
... & Chr(34) & a & Chr(34) & " " & Chr(34) & filename & Chr(34)
Then %1 and %2 in the batch file will include the quotes, though. To remove them there you can use %~1 and %~2 respectively.

Related

How can I save addresses from batch file to text?

I want to save this code from batch file to txt but cant.
I write this code but this code not working, cant save the address
how can i save "some code" like this from batch file to txt.
i have many codes and need to save it at the right time from batch file to txt and not execute them just save as txt.
tnq
this code want to save:
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "C:\Program Files\Windows Task\WindowsTask.bat" & Chr(34), 0
Set WshShell = Nothing
this is my solution and not working:
#echo off
(echo Set WshShell = CreateObject^("WScript.Shell"^) )> sina.txt
(echo WshShell.Run chr^(34^) & "C:\Program Files\Windows Task\WindowsTask.bat" & Chr^(34^), 0) >> sina.txt
(echo Set WshShell = Nothing) >> sina.txt
pause
You have far less trouble with paranthesis and escaping if you put the redirection at the beginning of the line.
#echo off
> sina.txt echo Set WshShell = CreateObject("WScript.Shell")
>> sina.txt echo WshShell.Run chr(34) ^& "C:\Program Files\Windows Task\WindowsTask.bat" ^& Chr(34), 0
>> sina.txt echo Set WshShell = Nothing
pause
You need to double the %.
>> sina2.txt echo SET TodayYear=%%DATE:~10,4%%
The result in your file will be:
SET TodayYear=%DATE:~10,4%

Batch Job to Write to VBS file

I am trying to use a batch file to write this code to a VBS file but I cannot get a successful output. Can someone help?
This is what I am trying to write to Output.vbs
set w = CreateObject("WScript.Shell")
W.Run chr(34) & "C:\Program Files\Test\Test.bat" & chr(34), 0
set w= Nothing
I tried this:
>"C:\Program Files\Test\Output.vbs" (
echo set w = CreateObject("WScript.Shell")
echo W.Run chr(34) & "C:\Program Files\Test\Test.bat" & chr(34), 0
echo set w= Nothing
)
But the data I am getting in Output.vbs is only this:
set w = CreateObject("WScript.Shell"
It obviously doesn't like these ( and ) Is there a way around this? Not super skilled in CMD other than the basics. I assume I need to escape this somehow?
Any advice?
Thanks,
You need to escape the ampersands and nested closing parentheses, just like this:
#( Echo Set w = CreateObject("WScript.Shell"^)
Echo w.Run Chr(34^) ^& "C:\Program Files\Test\Test.bat" ^& Chr(34^), 0
Echo Set w = Nothing) 1> "C:\Program Files\Test\Output.vbs"

How to get particular code as output from a text file using batch?

I have a text file with a youtube link,
text file = url.txt , which consist,
https://www.youtube.com/watch?v=Videocode
my need is, by running a batch file how to get only that Videocode as output in another text file.
For example, if am running url.bat which need to convert "https://www.youtube.com/watch?v=Videocode" into "Videocode"
I hope you understand my need. Please gave me some solutions. Thanks in advance.
Taking the question as asked literally, a text file named url.txt containing a link in the format https://www.youtube.com/watch?v=Videocode, url.bat could just contain this:
#For /F "UseBackQ Tokens=2 Delims==&" %%A In ("C:\Users\niranja\Desktop\url.txt") Do #(Echo %%A)>"output.txt"
Change the path to your url.txt, C:\Users\niranja\Desktop\, to suit; or if it is in the same location as url.bat remove that path completely. The video ID you were looking for should be written to a file named output.txt in the same directory as url.bat.
Note: If question as written does not match your real intent, take a look at the link provided by Hackoo and start putting something together yourself!
Here is an idea with a vbscript using a Regex to extract the "Videocode"
Data = "https://www.youtube.com/watch?v=Videocode" & vbCrlf &_
"http://www.youtube.com/watch?v=iwGFalTRHDA" & vbCrlf &_
"http://www.youtube.com/watch?v=iwGFalTRHDA&feature=related" & vbCrlf &_
"http://youtu.be/iwGFalTRHDA" & vbCrlf &_
"http://youtu.be/n17B_uFF4cA" & vbCrlf &_
"http://www.youtube.com/embed/watch?feature=player_embedded&v=r5nB9u4jjy4" & vbCrlf &_
"http://www.youtube.com/watch?v=t-ZRX8984sc" & vbCrlf &_
"http://youtu.be/t-ZRX8984sc"
Data_Extracted = Extract(Data,"http(?:s?):\/\/(?:www\.)?youtu(?:be\.com\/watch\?v=|\.be\/)([\w\-\_]*)(&(amp;)?‌​[\w\?‌​=]*)?")
WScript.echo Data_Extracted
'************************************************
Function Extract(Data,Pattern)
Dim oRE,oMatches,Match,Line
set oRE = New RegExp
oRE.IgnoreCase = True
oRE.Global = True
oRE.Pattern = Pattern
set oMatches = oRE.Execute(Data)
If not isEmpty(oMatches) then
For Each Match in oMatches
Line = Line & Match.SubMatches(0) & vbcrlf
Next
Extract = Line
End if
End Function
'************************************************
EDIT : Using an hybrid code batch with a vbscript
#echo off
Title Extract Videocode from Youtube links
Set "Tmpvbs=%temp%\Tmpvbs.vbs"
Set "InputFile=URL.txt"
Set "OutPutFile=OutPutCode.txt"
Call :Extract "%InputFile%" "%OutPutFile%"
Start "" "%OutPutFile%" & exit
::****************************************************
:Extract <InputData> <OutPutData>
(
echo Data = WScript.StdIn.ReadAll
echo Data = Extract(Data,"http(?:s?):\/\/(?:www\.)?youtu(?:be\.com\/watch\?v=|\.be\/^)([\w\-\_]*)(&(amp;)?‌​[\w\?‌​=]*)?"^)
echo WScript.StdOut.WriteLine Data
echo '************************************************
echo Function Extract(Data,Pattern^)
echo Dim oRE,oMatches,Match,Line
echo set oRE = New RegExp
echo oRE.IgnoreCase = True
echo oRE.Global = True
echo oRE.Pattern = Pattern
echo set oMatches = oRE.Execute(Data^)
echo If not isEmpty(oMatches^) then
echo For Each Match in oMatches
echo Line = Line ^& Match.SubMatches(0^) ^& vbcrlf
echo Next
echo Extract = Line
echo End if
echo End Function
echo '************************************************
)>"%Tmpvbs%"
cscript /nologo "%Tmpvbs%" < "%~1" > "%~2"
If Exist "%Tmpvbs%" Del "%Tmpvbs%"
exit /b
::**********************************************************************************

How to open exe file with parameters

I have batch file & vbs file that runs exe application in hidden mode.
Now I would like to open this exe applicatio, but with parameters passed to it.
Batch file:
wscript.exe "C:\~some path~\invisible2.vbs" "C:\~some path~\Rserve_d.exe"
invisible2.vbs:
CreateObject("Wscript.Shell").Run """" & WScript.Arguments(0) & """", 0, False
Above code makes sure it runs hidden. But now I need to pass this parameter to the Rserve_d.exe when starting it:
--RS-conf "E:\~some path~\Rconf.cfg"
Please advise. I've tried with adjustments, but it seems, that there is always something wrong in the syntax.
Build the arguments string for your command from the arguments to the script:
Function qq(str)
qq = """" & str & """"
End Function
args = ""
For i = 1 To WScript.Arguments.Count - 1
If InStr(WScript.Arguments(i), " ") > 0 Then
args = " " & qq(WScript.Arguments(i))
Else
args = " " & WScript.Arguments(i)
End If
Next
CreateObject("Wscript.Shell").Run qq(WScript.Arguments(0)) & args, 0, False
Ansgar Wiechers posted his answer before I did so he should deserve the credits. Unfortunately, I had already made the effort of posting an answer as well. To provide some additional functionality to your batch script, you could also check for the return value of the executed VBScript.
Batch file:
setlocal
set "script=c:\~some path~\invisible2.vbs"
set "program=c:\~some path~\rserve_d.exe"
set "params=--RS-conf "e:\~some path~\rconf.cfg""
cscript "%script%" //nologo "%program%" %params%
:: %errorlevel% = 0 - VBScript was executed successfully
:: %errorlevel% = 1 - Missing arguments
:: %errorlevel% = 2 - Shell object creation failed
:: %errorlevel% = 3 - Run method was unable to execute the program
VBScript:
Option Explicit
On Error Resume Next
Dim objShell,_
strCmdLine,_
intCount
If (WScript.Arguments.Count < 1) Then
WScript.Quit(1)
End If
Set objShell = WScript.CreateObject("WScript.Shell")
If (Err.Number <> 0) Then
WScript.Quit(2)
End If
For intCount = 1 To WScript.Arguments.Count - 1
strCmdLine = strCmdLine & " " & """" & WScript.Arguments.Item(intCount) & """"
Next
objShell.Run """" & WScript.Arguments.Item(0) & """" & strCmdLine, 0, False
If (Err.Number <> 0) Then
WScript.Quit(3)
End If

Passing variable from vbs to bat does not showing full value in batch file

I Want To Get The Current Folder Path From
where the vbs file is running,and store it into a variable
then pass the variable to a batch file.
My VBS is:
dim fso: set fso = CreateObject("Scripting.FileSystemObject")
CD = fso.GetAbsolutePathName(".")
dim WshShell
set WshShell=Wscript.CreateObject("Wscript.shell")
WshShell.run "a.bat " & CD
My BAT Code is:
#echo off
echo get path is='%1'
pause
exit
The VbS is working but problem Is if the folder is like
"c:\Program Files\" the batch file echo only "C:\Program"
How Can I Get The Full Path that passed from the vbs.
I think you need to add a:
CD = """" & CD & """"
Which will help the string, when interpreted by the batch file, be interpreted as a whole. (You will be passing "C:\Program Files\" rather than C:\Program Files\ which will be interpreted as a.bat "C:\Program" "Files\")
For example:
'Dim fso: Set fso = CreateObject("Scripting.FileSystemObject")
CD = """" & fso.GetAbsolutePathName(".") & """"
'Dim WshShell
'Set WshShell=Wscript.CreateObject("Wscript.shell")
'WshShell.run "a.bat " & CD
or:
'Dim fso: Set fso = CreateObject("Scripting.FileSystemObject")
'CD = fso.GetAbsolutePathName(".")
CD = """" & CD & """"
'Dim WshShell
'Set WshShell=Wscript.CreateObject("Wscript.shell")
'WshShell.run "a.bat " & CD
or as Ekkehard.Horner and MC ND have pointed out:
'Dim fso: Set fso = CreateObject("Scripting.FileSystemObject")
'CD = fso.GetAbsolutePathName(".")
'Dim WshShell
'Set WshShell=Wscript.CreateObject("Wscript.shell")
WshShell.run "a.bat " & """" & CD & """"
As MC ND has used, you could substitute """" for char(34) if you think it is easier to read.
To further improve readability as Ekkehard.Horner has suggested, you could create a function:
Function qq(s)
qq = """" & s & """"
End Function
And use as:
'Dim fso: Set fso = CreateObject("Scripting.FileSystemObject")
'CD = fso.GetAbsolutePathName(".")
'Dim WshShell
'Set WshShell=Wscript.CreateObject("Wscript.shell")
WshShell.run "a.bat " & qq(CD)
You need to (double) quote the path
WshShell.run "a.bat " & """" & CD & """"
Otherwise each space separated part of CD will be passed as one argument.
You could use a function like
Function qq(s)
qq = """" & s & """"
End Function
to avoid the noise in more complex concatenations.
WshShell.run "a.bat " & qq(CD)
WshShell.run "a.bat " & Chr(34) & CD & Chr(34)
WshShell.run "a.bat """ & CD & """"
A string with a space in it, when passed to a batch file as argument, is received as two strings. So, it is necessary to enclose it in quotes to pass it as only one argument.
Then in batch file you will get the path with the quotes. So from batch file
echo %1
will show the quoted path and
echo %~1
will show the path without quotes

Resources