How to create a file without a Command Prompt window - command-line

I'm using WinRAR SFX module to create an installation, and use its presetup option to run some preliminary tests.
Since wscript can only accept vbs file, and not the script itself, I first run "cmd /c echo {...script code...} > setup.vbs", and then I run "wscript setup.vbs". The run of the first cmd command opens a brief command window, and I would really like to avoid this. I thought of using RunDll32 to write this data, but couldn't find any suitable API to use.
Can anyone think of a way to bypass it and create a small file with a small VBScript text without opening a Command Prompt window?
Thanks a lot,
splintor

Is the script code already in a file? If so,
You can use the TYPE command to send the script to a file:
TYPE [script_file] > setup.vbs
or COPY the script file:
COPY [script_file] setup.vbs
If the script code is in the body of your cmd, you can use the START command to run the cmd without a window (/b flag):
START /B cmd /c echo {...script code...} > setup.vbs

Rather than use cmd /c echo {...script code...} > setup.vbs as a presetup step, perhaps you could package a VBscript with your install that does your preliminary tests and creates setup.vbs, and then calls setup.vbs for you. You'd have to put this in the setup portion of the WinRAR script.
You can call another VBScript from VBScript like this:
Set WSHShell = CreateObject("WScript.Shell")
WSHShell.Run "wscript d:\setup.vbs, ,True
See this MSDN link for the syntax of the Run command.

Related

Perl running a batch file #echo command not found

I am using mr on Windows and it allows running arbitrary commands before/after any repository action. As far as I can see this is done simply by invoking perl's system function. However something seems very wrong with my setup: when making mr run the following batch file, located in d:
#echo off
copy /Y foo.bat bar.bat
I get errors on the most basic windows commands:
d:/foo.bat: line 1: #echo: command not found
d:/foo.bat: line 2: copy: command not found
To make sure mr isn't the problem, I ran perl -e 'system( "d:/foo.bat" )' but the output is the same.
Using xcopy instead of copy, it seems the xcopy command is found since the output is now
d:/foo.bat: line 1: #echo: command not found
Invalid number of parameters
However I have no idea what could be wrong with the parameters. I figured maybe the problem is the batch file hasn't full access to the standard command environment so I tried running it explicitly via perl -e 'system( "cmd /c d:\foo.bat" )' but that just starts cmd and does not run the command (I have to exit the command line to get back to the one where I was).
What is wrong here? A detailed explanation would be great. Also, how do I solve this? I prefer a solution that leaves the batch file as is.
The echo directive is executed directly by the running command-prompt instance.
But perl is launching a new process with your command. You need to run your script within a cmd instance, for those commands to work.
Your cmd /c must work. Check if you have spaces in the path you are supplying to it.
You can use a parametrized way of passing arguments,
#array = qw("/c", "path/to/xyz.bat");
system("cmd.exe", #array);
The echo directive is not an executable and hence, it errors out.
The same is true of the copy command also. It is not an executable, while xcopy.exe is.

launch cmd run command isn't working

I am working on a script or batch file (or combo of the two) which imports an outlook prf file, then launches a new cmd.exe window runs a application specific program which when passed a server cluster name pulls in an outlook data file in the previously created outlook profile. So i have the vbs script that checks for the outlook profile if it doesn't exist it imports the prf. That's working fine, now the program i need to is called addiman.exe the server cluster name is gsiapp...the manual method is i launch a cmd windows and type "addiman gsiapp" i wish to automates this by calling it in a routine called :Filesite the below command has been unsuccessful, it launches a new cmd.exe window but doesn't run the command.
:ImportPRf
call cscript \\gsf1\Apps\Scripts\public\deployprf.vbs
GOTO :FileSite
:FileSite
start cmd.exe /c "c:\program files\interwoven\worksite\addiman.exe" GSIAPP
GOTO :EXIT
:Exit
Exit
start cmd.exe /c "c:\program files\interwoven\worksite\addiman.exe GSIAPP"
try this, because cmd.exe interprets the part between "" as comand and ignores the GSIAPP statement
wild guess. Try adding another call before the "start" - like this
:FileSite
call start cmd.exe /c "c:\program files\interwoven\worksite\addiman.exe" GSIAPP
problem solved, the full path isn't needed. just had to putt "addiman GSIAPP". Thanks everyone who provided suggestions.

From command line on win7, how do I launch a process as administrator with elevated UAC

I have a program which requires Administrative privileges that I want to run from a batch file. What command can I run from command line will run my program with administrative privileges? I'm okay with the pop-up window asking for permission. Additionally, the file needs to be able to run from anywhere on a computer so additional files are run from ./src. The problem is that if I right-click and choose "run as administrator" it changes my current directory so ./src no longer works. If I disable UAC on my machine then everything runs fine. Thank you!
Look here: https://superuser.com/a/269750/139371
elevate seems to be working, calling
C:\Utils\bin.x86-64\elevate.exe -k dir
executes dir in the "current directory" where elevate was called.
This is tough, Microsoft provides no utility to do this (mostly because giving a batch file that ability breaks security), except for RunAs, and that requires that the Administrator account be activated.
There IS a JScript program that can do something similar, by using SendKeys to open the Start menu and type cmd[CTL]+[SHIFT]+[ENTER] which will launch a Command-Line shell.
Save the following as as .js file, like StartAdmin.js:
var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.SendKeys("^{esc}cmd^+{ENTER}"); The equivilent of [CTRL]+[ESC] cmd [CTRL]+[SHIFT]+[ENTER]
To run StartAdmin.js from a batch file, you will need the following line:
wscript StartAdmin.js
To launch from a particular directory and launch a batch file, change line 2 in StartAdmin.js to something like:
WshShell.SendKeys("^{esc}cmd /C "cd %userprofile% & batchfile.bat"^+{ENTER}");
/C switch tells it to run the commands, then close the command-line window.
/K would leave the command window open after it exited the batch file.
To help you understand the SendKeys commands:
+=[Shift Key]
^=[Control Key]
{esc}=[Escape Key]
{enter}=[Enter Key]
To learn more about using CMD.EXE, type CMD /? at the command prompt.
This is a very untidy and ugly way to do it, but it's the only way I know how using only the tools that come with Windows.

How to execute hidden command in cmd and exit

I have this line:
C:\Windows\system32\cmd.exe start /minimized /wait cmd.exe /c picture.jpg
I use this line in Target field of shortcut.
When I run it, it opens minimized cmd, and picture. What I need is to close that cmd, after opening picture, or to run cmd in hidden mode.
Can anyone help? Thanks in advance.
I am not sure it this may work in your scenario. There are two possible workouts to this.
1) Use a batch script and in the last line, simply add exit to the script. save it as something.bat and run it whenever needed. This will execute the scripts and in the end, the cmd will exit.
2) Alternately you may use want to run the cmd in hidden mode (not minimized). to do this, create a batch job. then pack this batch file using this particular exe compressor - 'iExpress'. (type iexpress under the run box) This has an option to run the scripts in hidden mode. they will run in background.
Isn't
C:\Windows\System32\cmd.exe /c start picture.jpg
doing what you need ?
Why don't you just point the shortcut directly to
picture.jpg
?
Why you need cmd to open a picture?
Just do it like
rundll "C:\Program Files\Windows Photo Viewer\PhotoViewer.dll" "image.png"

Disable powershell expansion of command's extension?

We have a lot of existing batch files that help with the running of different perl and ruby scripts.
A batch file (e.g. test.bat) would normally be invoked like:
$ test
and within the batch file, it will set some settings and finally try to run the corresponding script file (e.g. test.pl) like this:
perl -S "%0.pl" %*
All works with cmd.exe, but today, I decided to switch to PowerShell and found out that it expands the commands. So trying to run "test" will actually run "Full\path\test.bat" and my script would complain that there is no file test.bat.pl.
Is there a way to prevent this command expansion? Rewriting all batch files is not an option.
One way is to call cmd explicitly:
cmd /c test