Return ErrorLevel or console message if NIRCMD.exe fails - batch-file

I am looking for a way to identify if NIRCMD fails to do what i ask it to do. Is there a way to have NIRCMD to tell me if it fails?
Example:
"nircmd.exe" win activate title "Untitled - Notepad"
The above code works because I have this file open.
"nircmd.exe" win activate title "Untitled - Notepad12345"
The above code DOES NOT work because this window does not exist yet Nircmd acts like it runs ok in the CMD prompt and not giving any type of message letting me know it did not actually do what i asked it to do.
Also, the "NIRCMD showerror" results in no errors.

Related

Batch file to open fullscreen website

How do you open a website from startup, that'll run in fullscreen mode as well?
Using /max doesn't seem to do anything. That's for maximizing the screen. I want it to be like when F11 is pressed when you visit a website.
This is my file
#echo off
start chrome --profile-directory="Profile 1" "https://stackoverflow.com/"
I've also looked into automatically having a key typed, but I can't get it to work. This is my first dabble with batch-file. Maybe it'll help you though:
WScript.CreateObject("WScript.Shell").SendKeys("{F11}");
EDIT:
The answer to this similar question isn't what helped me. As I've stated, /max is just to maximize the screen, whereas I want it to go fullscreen.
To send keys exactly to the window you want you have to focus on it first with AppActivate function.The sendKeys.bat do both with a single script
here's an example (at the moment I have no chrome driver so I'm using the standard chrome):
"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" "http://stackoverflow.com/"
call sendKeys.bat "Stack" "{F11}"
the first argument is the title and the second the keys to be send.
According to this list --start-fullscreen argument should also work
Here are a few ways to do this.
start "" "iexploerer.exe" -k "https://someurl.com"
start "" "chrome.exe" -kiosk "https://someurl.com"
start "" "firefox.exe" -kiosk "https://someurl.com"

Windows CE 5 batch IF EXISTS

I'm trying to run a batch file on a WINCE device that will ping another networked device and capture the results in a text file.
However, whenever I try to execute the section that checks to see if the log file already exists, I get this error:
IF: incorrect command syntax
Can anyone shed some light as to why?
Here is the bit that creates the text file to log the results. It may or may not be worth mentioning, but it works fine on a windows 7 PC:
SET DATETIME=%date:~0,2%\%date:~3,2%\%date:~6,4% 0%time:~1,1%:%time:~3,2%:%time:~6,2%
if NOT exist %DATEVARIABLE%-log.txt type "New Log" > %DATEVARIABLE%-log.txt
Thanks
Doug
Something looks like this :
#echo off
SET "DATETIME=%date:~0,2%-%date:~3,2%-%date:~6,4%_%time:~1,1%-%time:~3,2%-%time:~6,2%"
echo "%DATETIME%"
pause
if NOT exist "%DATETIME%-log.txt" echo "New Log" > "%DATETIME%-log.txt"
pause

Winzip Self Extracting exe would end abruptly without showing "All Done" in cmd

I have got a batch script that ends with the following:
TITLE ALL DONE. You can close this window
ECHO.
ECHO ALL DONE. You can close this window
ECHO.
Pause
EXIT
it works fine when i run just the bat file (see below)
All Done! You can close this window.
Press any key to continue . . .
But when i create a self extracting exe out of the same bat script it wouldn't show this window after the execution and would just exit without any proper message.
Since the script works fine by itself, i am a bit lost on what could be causing this? How could i make the self extracting exe to show that window and prompt the user to close it like above.
It depends on the content of the executable, if your including other files to your archive be sure the program you run after extraction is your batch file.
If your only including that file i suggest you use BatToExe Converter
much more efficient way to build it, also it supports commandline, including other files, product info, admin manifest etc

Locking focus on batch file

I have a simple batch file in windows that I run on startup that presents the user with a menu to start certain applications. However by default, whenever I open one of these applications the focus goes to that window and off of the batch file. Is there a way to maintain, or re-divert focus onto the batch window?
Thanks
EDIT: Got it to do what I wanted. Used foxidrives suggestion to start them minimized but they were still taking focus. So I made a short VBScript to make the cmd window the active window after each call and the combination of the two worked. Thanks!
There is no command to steal the focus. As Bill_Stewart posted, that would be a dangerous feature that grants the program too much power over the user. You can however start the applications minimized (they will still be the active window), and then build and call a VBScript to make your batch window the active window:
start "" /MIN "application.exe"
cscript /nologo myVBScript.vbs
myVBScript.vbs
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.AppActivate "myBatchFile"
WScript.Sleep 2000
WshShell.AppActiavte "myBatchFile"
I've read that several people have had trouble with AppActivate on Windows 7. It was not functioning for me, and instead of bringing my target window to the foreground it just blinked in the task bar. Calling it a second time however, for some reason, brought it to the foreground and made it the active window, which is what I wanted. Hope this helps anybody else with a similar issue.
you can't lock the focus to your command prompt window. But what you could do is to set the TopMost flag of the command prompt window. There is a Win32 function called SetWindowPos which does that. Maybe there are some ready to use command line tools around which are doing this for you. Or, if you have visual studio installed, try to compile this one here: How to set a console application window to be the top most window (C#)?
If you use the start command with the /min switch then the application will be minimised and the batch file should remain visible:
#echo off
pause
echo launching notepad
start "" /min notepad
echo notepad is active
pause

Gracefully trap error on start cmd

On a cmd prompt or bat file, I issue the following:
start textpad myfile.txt and it works fine.
If the program textpad does not exist on the computer, then an error sound and a popup occurs which the OK button must be pushed.
I desire to trap this error so that I could do something like
start textpad myfile.txt || start notepad myfile.txt
where the || implies that if the start of textpad is not successful, then the start of notepad should occur. HOWEVER, I still get the error sound and requirement of hitting OK.
My intent is to avoid the sound and the requirement of any user intervention.
I have also tried the following bat approach below, to no avail.
start textpad
if not %ERRORLEVEL% == 0 GOTO END
start notepad
:END
Any help would be great.
thanks
ted
You can use the following little snippet to find out whether the program you intend to launch exists:
for %%x in (textpad.exe) do set temp=%%~$PATH:x
if [%temp%]==[] echo Didn't exist.
But may I suggest that you simply use
start foo.txt
instead of forcing a specific editor onto the user? File type associations are there for a reason.
I do not believe you will find a way to make that work. Perhaps look for the existence of textpad.exe on the machine? If you can predict what directory it would be loaded from, this should be pretty easy using IF EXIST.
There are some techniques to detect the presence of a specific tool, but this only works for command line tool, or with GUI applications also supporting command line options.
For these tricks, take a look at this page.
"/wait" parameter would do the trick for you..
START /wait NOTEPAD.EXE SOME.TXT
echo %ERRORLEVEL%
# This gives zero as output.
START /wait TEXTPAD.EXE SOME.TXT
echo %ERRORLEVEL%
# This gives non-zero output.
You probably already have an answer, but my over-sized ego has forced me to post my answer.
So, this should work.
start textpad 2> nul||start notepad
This will start notepad if the command start texpad fails, while also redirecting any error message you may get from the first command.

Resources