Passing variable from vbs to batch file [duplicate] - batch-file

This question already has answers here:
Passing-variable-from-vbscript-to-batch-file with arguments
(2 answers)
Closed 9 years ago.
I am trying to pass a string that is contained in a variable named sDateFile into test.bat using the following line:
WshShell.Run "test.bat sDateFile"
And to check whether test.bat has received the variable. I get test.bat to do the following:
echo %1
But my output is sDateFile, not the string in the variable.
Am I approaching my problem incorrectly? Would there be a better way to approach it?

You have to concat your bat file and the value of sDateFile. There is a SPACE after test.bat
WshShell.Run "test.bat " + chr(34) + sDateFile + chr(34)
Run command

Related

how do i get echo to type out a % in a batch file [duplicate]

This question already has answers here:
BATCH- echo % (print an percent sign) [duplicate]
(1 answer)
Escape percent in bat file
(1 answer)
Ignore percent sign in batch file
(6 answers)
How does the Windows Command Interpreter (CMD.EXE) parse scripts?
(8 answers)
What is the difference between % and %% in a cmd file?
(2 answers)
Closed 6 days ago.
I want the code below to work or something that does the same thing.
#echo off
echo 100%
pause
When you open the .bat file it shuld look like this.
100%
Press any key to continue . . .
So i want the echo to read out the % and not to be a part of a code.
I want it to work the same as for example
#echo off
echo hi
pause
I have only tried echo 100% because i am a noob at coding so i dont know what else to try.
The percent sign has special meaning in (Windows) batch files, it is used to indicate a variable. For instance, echo %username% will print the contents of the username variable.
To echo a literal % sign, you need to double it, i.e. echo 100%%

Batch file doesn't run correctly within Windows 10 [duplicate]

This question already has answers here:
Works in Command Prompt but not in BAT-file or CMD-file [duplicate]
(2 answers)
Closed 2 years ago.
I have a merge.bat file which includes the following line:
for %f in (*.txt) do type "%f" >> mergedfile.txt
This code line works perfectly if I enter it directly into a cmd window.
All the txt files are located in the same folder as this merge.bat file.
However, when I try to run it as a batch file under cmd i.e. call merge.bat it shows the following message:
f" >> mergedfile.txt was unexpected at this time
What I would like to do is setup the merge.bat file to run in the Windows 10 task scheduler at a set time, but I just can't get the .bat file to run correctly.
What am I doing wrong?
you have to add double % signs in batch file in loops.use the following code:
for %%f in (*.txt) do type "%%f" >> mergedfile.txt

How can i run a batch file hiddenly using shell script? [duplicate]

This question already has an answer here:
Prevent VBscript app from showing Console Window
(1 answer)
Closed 2 years ago.
I am working on shell script/ .vbs extension file, and I want to run a batch file hiddenly or in background from shell script. I am mentioning my code but its not executing a batch file. I have saved my file with a name test.vbs.
Set WshShell = CreateObject()
WshShell.Run chr(34) & "%USERPROFILE%\AppData\Local\RE.bat" & Chr(34), 0
Set WshShell = Nothing
Thanks in advance.
when you write CreateObject(), you have to mention in those circle brackets. Your code looks fine just add WScript.shell in circle brackets like this.
Set WshShell = CreateObject("WScript.shell")
You need CreateObject("WScript.Shell"), not CreateObject():
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run Chr(34) & "%USERPROFILE%\AppData\Local\RE.bat" & Chr(34), 0
Otherwise VBScript wouldn't know what you want to create! So it can't know you want the shell object that has the Run method...

Why Vbs show this message [duplicate]

This question already has answers here:
Why oShell.Run not work
(2 answers)
Closed 6 years ago.
This code work fine
set oShell = CreateObject ("WScript.shell")
eAppData = oshell.ExpandEnvironmentStrings("%appdata%")
wscript.echo Appdata
oshell.run(Appdata & "\Test.bat"),0,False
This .vbs in %AppData%\Microsoft\Windows\Start Menu\Programs\Startup
But when windows start show this message why ?
The Test.bat is in %appdata%\Test.bat
And when you get rid of this line : wscript.echo Appdata
This code will come like that if you don't want to display this message again
set oShell = CreateObject ("WScript.shell")
AppData = oshell.ExpandEnvironmentStrings("%appdata%")
oshell.run(Appdata & "\Test.bat"),0,False
You're using wscript.echo Appdata, which will cause this box to pop open containing the shown folder path.

Put a command in a variable [duplicate]

This question already has answers here:
Assign output of a program to a variable using a MS batch file
(12 answers)
Closed 7 years ago.
I want to store this command in a variable called code1
dir /a:d /b
so the output would be:
Folder1
Folder2
Folder3
etc...
Any ideas how to do it?
I've checked other questions and when it just sais echo is off.
Simply store the command in the variable like you say. To execute, simply expand the value:
#echo off
setlocal
:: Define a simple "macro"
set "code1=dir /a:d /b"
:: Execute the "macro"
%code1%

Resources