I want to create a VBS file that includes & in it that changes volume using batch (Below)
(
echo Set WshShell = CreateObject("WScript.Shell")
echo WshShell.SendKeys(chr(&hAF))
)> KXV.vbs
^ which should create KXV.vbs including that text
I've browsed for some time, but if I missed something I'm sorry. I tried using ^& but that makes it print instead. The volume change is fine but & keeps making it error:
(WshShell.SendKeys(chr(
'hAF))' is not recognized as an internal or external command,)
operable program or batch file.
Sorry if my spelling, grammar, or anything else is off.
You need to escape the closing parentheses too in order to create your vbs file correctly :
(
echo Set WshShell = CreateObject("WScript.Shell"^)
echo WshShell.SendKeys(chr(^&hAF^)^)
)>KXV.vbs
Related
I was making a batch file that was able to create a vbs coded file I had already made but when I tried running the file it kept trying to run the vbs code as a command and not put it in the file the code that gave the errors are
set line5=set objWMIService = getobject("winmgmts://"_& strComputer & "/root/cimv2")
echo %line5% >>password.vbs
this one tried to run the strComputer as a command
set line20=strComputer = "."
echo %line20% >>password.vbs
this one tried to run the strComputer part but I have one like that and it works just fine
set line21=strExe = "explorer.exe"
echo %line21% >>password.vbs
in this one it is trying to load the strEXE as a command
set line49=WScript.echo "Created: " & strExe & " on " & strComputer
echo %line49% >>password.vbs
for this last one the program is trying to run " on " as the command
So if anyone reading does not understand my problem I will say it again what I am trying to do is save the vbs code into a file but the batch window keeps on trying to execute random commands that I am trying to put in the file which makes it not put them in and give me errors making the file not work correctly I am going to say this in advance thank you to anyone who tries to help
try this:
#echo off
cls
set line1=FirstLineOfCodeHere
set line2=SecondLineOfCodeHere
(echo %line1%
echo %line2%)>filename.vbs
UPDATE: Use '>' to overwrite the file. Use '>>' to write more on the original file.
I have a scripting system driven by powershell which calls BAT files
I want to record the Envs at the end of the BAT session so that they are available for the next BAT file. I have found what I did, did not capture changes from the BAT file...
More
I had code like
& cmd.exe "/c $script $optionalArgs & (echo Name^=Value&SET) > ""c:\path\end_envs.csv"""
$script == dosomework.bat
which appeared to work, i.e. the csv file is created with env's BUT does not have the ones created by $script... Rather seem to be the ENVs of the initial cmd.exe call...
After adding the 'echo xx' lines to a BAT file and calling this at the bottom of the bat file referenced in $script, all is OK, I see the env at the end of the $script BAT file
Is there a way to fool a cmd.exe /c to consider my extra echo info as part of the called script ?
Thanks
OR...
sae this as $$script.bat
#ECHO OFF
(
echo Name=Value
CALL $script %*
SET
)>"c:\path\end_envs.csv"
and modify your code to like
& cmd.exe "/c $$script $optionalArgs"
whee $$script.bat may naturally be any name you like.
Note that this assumes that $script.bat (the one you are actually executing) does not contain a setlocal instruction.
If $script is variable, then in the batchfile $$script.bat try
#ECHO OFF
SETLOCAL
(
echo Name=Value
set "params=%* "
CALL %1 %%params:* =%%
set "params="
SET
)>"u:\envs.csv"
GOTO :EOF
This assumes there are no confounding factors (parameters containing characters to which cmd is sensitive, for instance - normal alphameric strings should be fine)
So
write out the header line
set params to the supplied parameter list + a space
execute a subroutine firstparameter with parameter(s) remainingparameters. The call ...%%params:* =%% syntax means "the value of params, up to and including the first space"
clear params
list the remining environment variables.
The destination filename is of course up to you. I used a name that's convenient for my system.
So - you would call this script with a parameter of (the first name of the script you want to run) + (any other parameters you require)
Simplest would probably be to add
(echo Name^=Value
SET) > "c:\path\end_envs.csv"
to the end of $script (or at least, force those two lines to be executed just prior to exit)
I am having issues getting a simple batch file (opening command prompt) to run from a vbs macro, I know this question gets asked a lot and I have tried many different suggested solutions for this without success. I am using notepad ++ to run the scripts/VB code for testing.
I have verified that the .bat file will execute correctly by itself, any suggestion on how to get this to work correctly would be greatly appreciated.
Here is my code for each instance.
VB CODE:
Sub CallBATCH()
Dim argh As Double
argh = Shell.Run "C:\Temp\cmdPrompt.bat"
End Sub
BATCH FILE:
start cmd.exe /k
EDIT: The following is the .bat file that I actually intend on calling up:
#echo OFF
title AutoCAD DWG Duplicator
color 0a
:start
set /P TemplateName=Please enter the template name you wish to copy:
set /P NumberOfCopies=Please enter how many copies you wish to make:
set Pathname="<filepath>"
cd /d %Pathname%
:init
for /L %%f in (1,1,%NumberOfCopies%) do copy %TemplateName%.dwg C:\Temp\%%f%TemplateName%.dwg
You seem to be calling a .BAT file that in turn opens a command prompt with START. I'm unclear on why you need the .BAT at all.
Dim oShell
Set oShell = WScript.CreateObject ("WScript.Shell")
oShell.run "cmd.exe /K"
Set oShell = Nothing
The /K parameter will open the command prompt window and keep it open. You've supplied no parameters for START and no commands to execute when the command prompt opens so this should do what you are looking for. More at: Run Method (Windows Script Host)
I have a master.bat file which has...
call file1.bat
call file2.bat
call file3.bat
call file4.bat
I want to schedule it on my Windows server 2008 to run in silent/invisible mode.I'm looking for some way to run this master.bat without anything visible to the user (no window, CMD interface ,no taskbar name etc..)
I don't want to install any batch to exe software.
I tried by changing the User running the task to "SYSTEM" and it has the work done but I can't do this in actual.
I have found that Windows Script Host’s Run Method allows you to run a script in invisible mode as.....
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "C:\Batch Files\master.bat" & Chr(34), 0
Set WshShell = Nothing
but no more file please :) any other suggestion for this.
EDIT1
Considering the limited options available..it would be OK to use Windows Script Host’s Run Method,but how i can schedule master.vbs in task scheduler.. ?
For an extended view of it, check for hybrid batch / vbscript / javascript files here in stackoverflow.
Save this as master.cmd and adapt as needed.
#if (#This==#IsBatch) #then
#echo off
rem **** batch zone *********************************************************
rem Check if started from javascript part of script.
rem We are checking an environment variable set from javascript part.
if "%_run_hidden_%"=="true" (
goto startBatchWork
)
rem if not started from javascript, call javascript part to restart batch.
wscript //E:JScript "%~dpnx0"
exit /b
:startBatchWork
rem Here starts the real work of the batch file
msg %username% "Batch file running hidden"
rem End of batch area. Ensure batch ends execution before reaching
rem javascript zone
exit /b
#end
// **** Javascript zone *****************************************************
// Instantiate the needed component to interact with Shell
var shell = WScript.CreateObject('WScript.Shell');
// Set the environment variable that the batch part will check to know
// it's running hidden
shell.Environment('Process').Item('_run_hidden_')='true';
// start the batch part of the script calling %comspec% with the current
// script as parameter, hidden (0) and not waiting for it to end (false)
shell.Run('"' + shell.ExpandEnvironmentStrings('%comspec%') + '" /c "' + WScript.ScriptFullName + '"', 0, false );
// All done. Exit
WScript.Quit(0);
CMDOW is a tool that will allow the batch to run hidden.
It is flagged as a hack tool by various AV programs.
I need to keep some part of a file and deal with those items whom I pull from this file. I call a VBScript file from a batch to process those items I want to transform. My goal is to have all of them (items which are transformed by the VBScript process and those which are not concerned by this process) into the same file.
Batch file:
#echo off
cscript run.vbs findstr /r "^I.=" "%~f1"
run.vbs:
set objF=createobject("scripting.filesystemobject")
x=objF.opentextfile(wscript.arguments(0),1).readall
Every time that I run batch, it occurs an execution error in my VBScript at the line x=objF.opentextfile(wscript.arguments(0),1).readall. I understand this error because the parameter given at run.vbs from batch is not a file.
Try storing the value in a variable before passing it to vbs script may be you can add few checks for exception in the batch script. Can you post some examples.
If you want to process the output of a batch command in VBScript, you have to do something like this:
#echo off
for /f %%a in ('findstr /r "^I.=" "%~f1"') do (
cscript run.vbs "%~fa"
)
It might be easier to do this entirely in VBScript, though:
Set fso = CreateObject("Scripting.FileSystemObject")
Set re = New RegExp
re.Pattern = "^I.=(.*)"
Set f = fso.OpenTextFile(WScript.Arguments.Unnamed(0))
Do Until f.AtEndOfStream
Set m = re.Execute(f.ReadLine)
If m.Count > 0 Then
text = fso.OpenTextFile(Trim(m(0).SubMatches(0))).ReadAll
'do stuff with text
End If
Loop
f.Close