Get an echo batch-file answer with VBA - excel

In my VBA code I call the bat using Call Shell(....).
I need now to get the "Echo answer" inside the batch-file in VBA.
How can I do that?
My batch-file:
#echo off
IF exist %3 (robocopy %1 %2 /e ) ELSE (echo 1)
I want to get that "1" in VBA.

You can't return answers using Call Shell. You need to use WScript.Shell, and can use the read lines from the execution object it returns.
Dim sh As Object
Set sh = CreateObject("WScript.Shell")
Dim ex As Object
Set ex = sh.Exec("C:\Path\To\File.bat")
Dim ans As String
ans = ex.StdOut.ReadAll
A shorthand, if you want to save lines:
Dim ans As String
ans = CreateObject("WScript.Shell").Exec("C:\Path\To\File.bat").StdOut.ReadAll

Related

Check if userinput contains substring

Hello please help me with this one!
I would like to check if the user input URL contains the defined SUBSTRING or not.
If yes I would like to GOTO LONG else GOTO SHORT
Thank you!
#echo off
setlocal enabledelayedexpansion enableextensions
SET /P "URL= Input the link of the video: "
SET "SUBSTRING=?filter=archives&sort=time"
ECHO !URL! | FINDSTR /C:"!SUBSTRING!">nul
IF ERRORLEVEL 1 (GOTO SHORT) ELSE GOTO LONG
:LONG
SET LINK=1
ECHO THIS IS A LONG LINK
ECHO "THE LINK NUMBER IS %LINK%"
ECHO !URL!
GOTO END
:SHORT
SET LINK=0
ECHO THIS IS A SHORT LINK
ECHO "THE LINK NUMBER IS %LINK%"
ECHO !URL!
GOTO END
:END
pause
Using double quotes properly helps fix some of your problems. There shouldn't be any need to use delayed expansion if you use quotes for the echo as well.
#echo off
SET /P "URL=Input the link of the video:"
SET "SUBSTRING=?filter=archives"
ECHO "%URL%"|FINDSTR /C:"%SUBSTRING%">nul
IF ERRORLEVEL 1 (GOTO SHORT) ELSE GOTO LONG
:LONG
SET LINK=1
ECHO THIS IS A LONG LINK
ECHO "THE LINK NUMBER IS %LINK%"
GOTO END
:SHORT
SET LINK=0
ECHO THIS IS A SHORT LINK
ECHO "THE LINK NUMBER IS %LINK%"
GOTO END
:END
pause
Update Showing execution of batch file.
C:\Users\Squashman\Desktop>test.bat
Input the link of the video:https://www.twitch.tv/videos/578427308?filter=archives&sort=time
THIS IS A LONG LINK
"THE LINK NUMBER IS 1"
Press any key to continue . . .
So this is the working code. I will now mention the problems I had so others can learn from it
I must not enable setlocal enabledelayedexpansion
I have to use quotes around variables for the SET command
Because I have delayedexpansion disabled I must use percentages instead of exclamation marks when I want to use a variable in a command later
Correct me if I am wrong but thats what I figured out with these guys' help and on my own by trial and error
I made this list and updated the code so if a newbie like me gets stuck they can have a look at this post.
Once again thank you guys!
#echo off
SET /P "URL=Input the link of the video:"
SET "SUBSTRING=?filter=archives"
ECHO "%URL%"|FINDSTR /C:"%SUBSTRING%">nul
IF ERRORLEVEL 1 (GOTO SHORT) ELSE GOTO LONG
:LONG
SET LINK=1
ECHO THIS IS A LONG LINK
ECHO "THE LINK NUMBER IS %LINK%"
GOTO END
:SHORT
SET LINK=0
ECHO THIS IS A SHORT LINK
ECHO "THE LINK NUMBER IS %LINK%"
GOTO END
:END
pause

error when passing string argument from batch to vbs

I'm calling a vbs string with a batch file. I'm passing a string through batch to vbs.
Complete batch file:
C:
cd C:\folder
Set arg = "sample foo"
Wscript titi.vbs "%arg"
pause
But, when I read the argument into the VBScript with str = Wscript.Arguments(0) the value of str is sample and not to sample foo
How can I fix it?
The problem is a simple one and one which is commonly made by those having used other programming languages.
Set arg = "sample foo"
sets the variable: %argspace% to the string: space"sample foo"
The way to assign variables should be:
Set arg="sample foo"
or:
Set "arg="sample foo""
I prefer the latter.
BTW, you also missed a closing % when using "%arg" instead of "%arg%".
Because you are using the argument as "%arg%" there is no neeed to set the double quotes into the actual variable string.
Just use:
CD /D "C:\folder"
Set "arg=sample foo"
Wscript titi.vbs "%arg%"
Pause
I tried below and working as expected,
1.vbs
str = Wscript.Arguments(0)
WScript.Echo(str)
1.bat
Wscript 1.vbs "sample foo"
cmd line output
The batch file you posted should have the VBScript output arg due to your malformed variable syntax. Even if you corrected %arg to %arg% you should get an empty string as output, because your variable assignment is malformed as well (set arg =... defines a variable %arg %).
Change
Set arg = "sample foo"
Wscript titi.vbs "%arg"
to
set "arg=sample foo"
Wscript titi.vbs "%arg%"
and the problem will disappear.

Append or put variable within string in vbscript?

I am wanting to include the variable domainUser within the quoted command sent to the commandline but I am not having any luck. What I am trying to accomplish is to create a log file titled with their domain name but I keep getting errors or just getting a txt file with no title.
Dim domainUser
domainUser = Example123
objInParam.Properties_.Item("CommandLine") = "cmd /c ECHO Test >> c:\UserLogs\"""domainUser""".txt"
So line 4 would be read like this (or whatever domain user I put in on line 2)...
objInParam.Properties_.Item("CommandLine") = "cmd /c ECHO Test >> c:\UserLogs\Example123.txt"
You need concatenation to splice a variable's content into a string and double double quotes to put double quotes into it:
>> Dim domainUser
>> domainUser = "Example123"
>> Dim cmd
>> cmd = "cmd /c ECHO Test >> ""c:\UserLogs\" & domainUser & ".txt"""
>> WScript.Echo cmd
>>
cmd /c ECHO Test >> "c:\UserLogs\Example123.txt"

Substitute substring in cmd window

I have the following string in a batch file script:
ABCE#$1 TroubleMaker[FFFFF A=MyCountry US=1 CA=1 JP=1 EU=1
and it's stored in _var,when I do
set _var=%_var:* A=% - it cuts all the characters before " A" (including the 'A') and i'm left with =MyCountry US=1 CA=1 JP=1 EU=1
how can I change the set command to cut also the = mark from the string?
tried set _var=%_var:*==% - didn't work.
Thanks.
#ECHO OFF
SETLOCAL
SET "string=ABCE#$1 TroubleMaker[FFFFF A=MyCountry US=1 CA=1 JP=1 EU=1"
FOR /f "tokens=1*delims==" %%s IN ("%string%") DO SET "string=%%t"
ECHO "%string%"
GOTO :EOF
This assumes that you want to delete up to and including the initial =
The = disturbs the substring replacement syntax, because it contains a = on its own.
You could go for the following work-around:
set _var=%_var:* A=%
set _var=%_var:~1%
The second line constitutes the substring expansion syntax (type set /? for details), which splits off the first character, that is supposed to be a =.
This of course works only if the = immediately follows the A substring.
You can check whether the first character is = before removing it, like:
set _var=%_var:* A=%
if "%_var:~,1%"=="=" set _var=%_var:~1%
If you just want to search for the (first) = character and to ignore the A substring, you could establish a loop structure like this:
:LOOP
if "%_var%"=="" goto :END
if "%_var:~,1%"=="=" (
set _var=%_var:~1%
goto :END
) else (
set _var=%_var:~1%
goto :LOOP
)
:END
This cuts off the first character and checks whether it is a =. If it is, the remaining string is stored in _var and the loop is left; if not, the loop continues checking the next character. The first line is inserted to not hang in case the string does not contain a = character.

Batch file: How to get variable's value using variable's name already stored in a string

1) Quick example:
set a="Hello"
set d="a"
Now, how do I get the value of "a" using (the value of) variable d? For example, the variable name could have been entered by a user using a prompt, or the name of the variable could have been sent to a function.
None of these ideas work:
set e=%%d%%
set e=%%%d%%%
set e=set e=%%d%%
%e%
After an hour of brainstorming and Googling, I have come up with this, but it seems too complicated and clumsy - is there really no other/easier way?:
set a="Hello"
set b="Good day"
set c="Good night"
set /p d="Give me a variable name"
call :GetVarVal %%%d%%% "e"
REM This now gives the correct value:
echo %e%
goto :eof
:GetVarVal
set "%~2=%~1"
goto :eof
2) Also, sort of similar, is there a better way to do this (ideally without a custom function):
set a="C:\Users\Blah\Documents\MP4Box\MP4Box.exe"
call :get_drive_and_path a
echo %b%
goto :eof
:get_drive_and_path
set b=%~dp1
goto :eof
Thanks!
example without delayed expansion (preserves exclamation marks):
#echo off &setlocal
set "a=Hello!"
set "d=a"
call echo %%%d%%%
output:
Hello!
If I understood you correctly, the trick called "delayed expansion" should work:
SETLOCAL ENABLEDELAYEDEXPANSION
SET a=Hello
SET b=a
ECHO This is the value of a: !%b%!
SETLOCAL DISABLEDELAYEDEXPANSION
Check how it works here
Part 2
set file="C:\Users\Blah\Documents\MP4Box\MP4Box.exe"
for %%a in (%file%) do echo %%~dpa
its just easy:
as you type e.g.
SET VarName=VarValue
Then the SET-function gets given a string looking like "VarName=VarValue" - it does not matter whether you use the marks there or not, the following commands do the same:
SET VarName=VarValue
SET "VarName=VarValue"
so now reflecting, what tools we have to build a string, we come up to the idea to use this:
SET "Name=VarName"
SET "%VarName%=VarValue"
Echo %Varname%
Better late than never - have a good time

Resources