Trying to make a Batch file create a vbs code - batch-file

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.

Related

Creating file with & in batch

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

Passing a windows path to a vbs script which then runs a batch file

I'm trying to run a batch file hidden. I have read many threads here but I still can't get it working. Win7 - I have created a new toolbar in the taskbar that shows me all .enc files in a specific folder. I want to click to select one of those filenames and have it sent to the vbs script that will run a batch file hidden with that filename as the first argument for the batch file. I am using total commander and have set up a file association of the below for .enc files -
hidebat.vbs %1
That is supposed to take the filename I selected and send it to the script.
This is the script hidebat.vbs -
CreateObject("Wscript.Shell").Run "G:\test\clipTest.bat" WScript.Arguments(0), 0, True
That is from another thread on this topic to run batch files hidden but with my addition of the WScript.Arguments(0) part. Supposedly that grabs the first argument to the vbs script.
When I try this out I get a window stating that the filename I selected is not a valid win32 application. Is it obvious what's wrong?
If it all could be done within the vbs script, all the better. I'm only doing two things in the batch file -
1. echo|set /p=%1|clip (echoing that filename to the clipboard)
2. start "" "g:\test\Process.lnk" (running this shortcut)
You forgot the & as well as a space between the script and your argument.
Try this:
CreateObject("Wscript.Shell").Run "G:\test\clipTest.bat " & WScript.Arguments(0), 0, True
Try the following. Chr(34) resolves to a quote, in case your paths contain spaces:
CreateObject("Wscript.Shell").Run chr(34) & "G:\test\clipTest.bat" & chr(34) & " " & chr(34) & WScript.Arguments(0) & chr(34), 0, True
I had to change my file association line to -
wscript "g:\path\to\hidebat.vbs" "%1"
Now it works, thanks!

How to make .bat do not call cmd

Soo
#echo off Turn off command echoing and don't echo the command turning it off. But how to make bat file do not call CMD at all ?
This makes so little sense. If you're asking how to stop a batch file from showing the command prompt window, you cannot.
You can however use a VBScript file to launch the batch file silently. The script would look something like:
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "C:\path-to-batch-file\batchfile.bat" & Chr(34), 0
Set WshShell = Nothing
I think what you're trying to do probably isn't a job for a batch file.
A batch file is a script, not a binary executable. A script always needs an interpreter to run. For batch scripts, the interpreter is cmd.exe, so you cannot avoid invoking cmd.exe at all. It makes no sense to run a script without its interpreter. This is true everywhere.
All you can do is hiding the console window that cmd.exe brings up when it's invoked, most easily done with the help of a VBScript:
CreateObject("Wscript.Shell").Run "C:\path\to\your.bat", 0, False

Running a batch (.bat) file from a VB macro (.vbs)

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)

Running master bat file in invisible mode

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.

Resources