Teratem - Invalid host - host

I feel quite useless with Teraterm.
I tried to create a simple macro macro.ttl that sends some stupid commands:
connect '/C=3'
sendln '?'
sendln '|3'
in order to:
connect '/C=3'- connect to Port 3 (althought it´s already in the config),
sendln '?' - display help,
sendln '|3' - enter this menu,
etc.
This works when I open Teraterm and go to Macros and open the file but my problem now is how to handle this from the cmd window.
I tried this:
start ttermpro.exe TTPMACRO "Teraterm_macros\macro.ttl"
basically: start Teraterm, open the macro and path and file name of the macro file.
But it reports error:
Invalid host
Any hints, please?
Thnx

The command, start ttermpro.exe TTPMACRO "Teraterm_macros\macro.ttl", will result in Tera Term attempting to connect to the host "TTPMACRO", which likely does not exist. Thus, Tera Term prints the "Invalid Host" error message.
Based on the documentation for the ttermpro command line options (here), a command to start Tera Term with a macro is:
start ttermpro.exe /M="Teraterm_macros\macro.ttl"
where the /M option is defined as:
/M=
Start Tera Term with macro.
If this value is not a full path, it is understood as a relative path from ttermpro.exe.

I thought about writing all the steps I followed just in case that someone like me (kind of d umb) was struggling with the same:
1 - Relate the .ttl with the Teraterm macro application:
1a - I went to my macro, click R-mouse, "Open with...", "Choose another app", "More apps", "Look for another app in this PC" and checked box "Always use this app to open .ttl files".
1b - I could not find the app first, then I found it in c:>Program Files (x86)>
teraterm and selected the executable "ttpmacro.exe".
2 - Modify the macro to start the communication because Teraterm has to be closed for this to work, so when the macro starts Teraterm you need to specify the port:
2a - I added in my Teraterm macro: connect '/C=3' (in my case I connect Teraterm with COM port 3)
2b - Whole macro: it looks like this:
connect '/C=3'
sendln '?'
sendln '|3'
sendln 'c'
sendln '3'
This connects through com port 3 and then enters one menu and sends c and then 3.
So now if I double click on the macro it executes the macro alone.
3 - Script to run the macro: Also I decided to create a batch file to run it from CAPL, and I did this:
3a - first close Teraterm if it´s already opened because the teraterm exe and the macro exe can´t connect at the same time:
tasklist /fi "IMAGENAME eq ttermpro.exe" | find /i "ttermpro.exe" >
nul if not errorlevel 1 (taskkill /im ttermpro.exe) else (echo
ttermpro already closed)
ttermpro.exe is the application name that runs when starting Teraterm normally.
3b - Start the macro:
"C:\workset\Teraterm_macros\mytmacro.ttl"
So it looks like this:
#echo off
echo.
echo .................................
echo .................................
echo ..... My teraterm macro .....
echo .................................
echo .................................
echo.
echo If Teraterm is launched it will be closed now.
TIMEOUT /T 5 /NOBREAK
REM this was to give the user 5seconds to cancel.
tasklist /fi "IMAGENAME eq ttermpro.exe" | find /i "ttermpro.exe" > nul
if not errorlevel 1 (taskkill /im ttermpro.exe) else (echo ttermpro already closed)
REM this is to close Teraterm if it´s opened already.
echo The calibration macro will be launched now.
"C:\Teraterm_macros\mytmacro.ttl"
REM This is to start my macro. As I alread linked .ttl with the macro app it´ll open.
IF ERRORLEVEL 1 GOTO ERROR
GOTO END
:ERROR
echo my teraterm macro failed.
GOTO END
:END
::exit
4 - I call it from CAPL because what I want is to include this macro is one test: I add this line:
TestWaitForSyscall("C:\Teraterm_macros","\"scriptwithmytmacro.bat",0,20000);
this in CAPL opens the batch file. It could open directly the macro but I wanted to have a bat script so that I can add stuff like closing teraterm if it´s already open among other things.

Related

Network connection - batch file runs and adds my error message at end

I'm having an issue when I run a batch file. When the script successfully connects to the internet and runs, the failed message is still coming up. When I intentionally fail the script, I get a "network error 53". How do I check for this? I did not see any information on errorlevels besides:
"if errorlevel 1 goto failed:" and then create a failed message
Script runs successfully: Error message stills come up after everything runs properly. Meaning..my "failed: " message still occurs.
Network connection fails: ":failed" part runs as it should
#echo off
cls
ping XXXXXXX
#echo off
net use o: \\XXXXXXXXX
if errorlevel 1 goto failed:
#echo on
ping XXXXXXXXX
killdisk.exe -XXXXXX -XXXX
:failed
echo Unable to access the network share. Please confirm your Ethernet connection and try again.
echo Please press a key to exit or the prompt will exit after 30 seconds.
PAUSE 30
echo EXITING
To facilitate a variety of possible use cases, the Windows Command Prompt doesn't stop executing when it reaches a label (i.e. :failed).
In order to stop execution before the label, you can put one of the following commands before your label:
EXIT - This command closes the command prompt. If you call other batch files or subroutines within your batch, this is not what you want. The EXIT command will close the command prompt window and stop processing everything.
GOTO:EOF - The GOTO command generally takes a label such as your :failed, but in this case :EOF is a special reserved label that tells the command prompt to skip to the end of the current batch file. This would then return control to the batch file that called this one.
Further reading:
Exit - SS64
Goto - SS64

Using batch to close open image file or word doc

I am coding a choose your own adventure game. Im at the point where a player would have a choice to look at a poster or a document on a desk and the game would open a .jpg file with the poster art or document on the desk i then want the game to close the .jpg file after 5 seconds. I have found so many questions about how to close programs and .exe with batch using exit/b and taskill but there is nothing about closing specific open files viewed by a program like an image viewer. In my case, the .jpg is being opened using windows photo viewer. the title of the window is "BADGE.jpg - Windows Photo Viewer" if i could and when the game is played on another computer there might be a different default image viewer, so i need a way just to close the file regardless of what program has it open.
below is a snippet of my code. :Test1 is where my problem lies
#ECHO OFF
: START
CLS
ECHO THIS IS A TEST PROGRAM TO SHOW THE USER AN IMAGE
ECHO OR FILE AND THEN CLOSE AFTER 5 SECONDS
PAUSE >NUL
GOTO TEST1
: TEST1
ECHO PRESS ENTER TO OPEN IMAGE
ECHO.
PAUSE
START "" "%~dp0\BADGE.jpg"
PING -n 2 0.0.0.0 1>NUL
exit "%~dp0\BADGE.jpg"
:: TASKKILL /F /IM BADGE.jpg
PAUSE
goto end
: end
echo.
echo IF THE IMAGE CLOSED AFTER 5 SECONDS THEN THIS TEST WAS A SUCCESSFUL
PAUSE
GOTO START
use the /fi switch (Filter), where wildcards are allowed:
taskkill /fi "windowtitle eq BADGE.jpg"
Doesn't matter, if * is - Windows Photo Viewer or - IrfanView or something different - as long, as the window title starts with the given file name.
Downside: it will kill every process with the given window title (although it's not very likely, there are more than one)
Note: start starts another cmd window - this is not needed, use just "%~dp0\BADGE.jpg" instead, which will open the jpg wtih it's default application. Also exit doesn't work, as you seem to think. You can't exit another process, you will always exit your batch file.
I've tested below script that I believe will work for you;
#echo off
start file9876.jpg
timeout 5
for /F "tokens=2 delims= " %%i in ('tasklist /v /fo table ^| findstr /i file9876') do set pid=%%i
taskkill /PID %pid%
It's best if the file you want to open/display has a very unique name so you can put it in the findstr command. In my case its file9876. Don't put a full file name with the extension - your program might not display it. If it also doesn't display a file name too just open it manually and check window name (that's what the script looks looks for).
And very important - don't move timeout line after the loop because your program might not actually be able to start enough and it won't be included in the task list so it won't find it and it will stay open (also tested on my own skin).
You can improve it to check if the program actually started and then look for the PID, also check if it was closed etc - some basic check to make it more bulletproof.
I tried to make this script as short and neat as possible - hope this helps you :)

blocking an if statement error message from a batch file

I am trying to design a batch program that scans the D:\ on my computer for a CD album and then opens a program to play it. I don't want to use Autoplay because want to design my own custom program for the purpose. I am using an IF statement and a GOTO statement that loops back to the IF statement after a delay. Here is what I have:
:rescan
if exist D:\Track01.cda start (my soon-to-be music playing program)
timeout 2 /NOBREAK
goto rescan
The problem is that if there is no CD in the drive, the IF statement causes a seprate error message window to pop up.
Is there any way to block this message?
I do not think there is a way to block this message as it is occurring outside of the control of your batch file. However I checked the existence of the drive before entering the loop and this seemed to work for my tests.
IF EXIST D:\ GOTO rescan
echo Drive does not exist
goto end
:rescan
if exist D:\Track01.cda echo start (my soon-to-be music playing program)
timeout 6 /NOBREAK
goto rescan
:end
Hope it helps you out.
use another command:
dir D:\track01.cda 1>nul 2>&1 && start (your program)
If there is no disk, dir returns The device is not ready. and an %errorlevel%of 1. If there is a disk, but no audio-disk (no "track01.cda"), dir returns "File not found" and an %errorlevel%of 1
(All output of dir is redirected to nul to keep the screen clear - change that, if you want that output).
&& works as "if previous command was successful then" ("if %errorlevel% is not 0")

CMD Batch command "start /wait" not working in Windows 8.1?

I'm working on a very simple bat file script that worked fine in Windows 7, but seems to be having issues in Windows 8. It relies heavily on "start /wait" to execute 1 file at a time. It seems that the "start" command works just fine. However, given the folder structure, I have referenced each item as such in a command:
start /wait ./folder1/app1.exe
start /wait ./folder2/app2.exe
start /wait ./folder3/app3.exe
While this worked just fine in W7. In Windows 8.1, this results in an error message stating: "Windows cannot find '.\folder1\app1.exe'. Make sure you typed the name correctly, and then try again."
So... Did MS replace the .\ wildcard in Windows 8 with something else? Or change it just slightly?
Yes, I know. This isn't really programming (still studying C/CPP), but any insight would be greatly appreciated.
./ is not a wildcard. * and ? are wildcards - they match any number of any characters /one any-character.
/ is a switch indicator. \ is a directory separator. Dangerous to confuse the two - it confuses cmd.
.\ means 'relative to the current directory`. Perhaps you should display the current directory using
echo %cd%
to make sure that cmd's idea of the current directory and yours agree. Also make sure the targets exist and are not hidden files.
I had the same problem with a cmd file on my usb drive.
In Windows 7 my application started with ...
START /WAIT %usbDrive%\Truecrypt\TrueCrypt.exe /volume %usbDrive%\%Container% /k %usbDrive%\Truecrypt\Truecrypt.key /cache /wipecache /quit
... but in Windows 8.1 it has not waited to put my password in the TrueCrypt screen.
As a workaround for both Windows versions this works for me fine now.
START %usbDrive%\Truecrypt\TrueCrypt.exe /volume %usbDrive%\%Container% /k %usbDrive%\Truecrypt\Truecrypt.key /cache /wipecache /quit
:CHECK
ping -n 2 localhost 1>NUL 2>NUL
TASKLIST /FI "IMAGENAME eq TrueCrypt.exe" > TrueCrypt.txt
FIND /N "TrueCrypt.exe" TrueCrypt.txt 1>NUL 2>NUL
IF %ERRORLEVEL%==0 ( GOTO CHECK ) ELSE ( DEL TrueCrypt.txt )
At first you start the program you want, then you need a mark to go back and check if your program is still running. The next thing is to wait about 2 seconds (for ping -n ... you can also use timeout). Then you can filter the tasklist with the exeutable name and write it in a check text file. With the find command you can grep in the check file... Finally the last line, if the application is not in the tasklist anymore the check text file will be deleted otherwise the cmd is jumping to the check mark.
I hope it will help

How to evaluate exit code of an application started from within a batch file?

I am trying to execute an application from a batch file by using START command (to hide console when executing) and I need to get errorlevel after execution. For example after System.exit(10) I'd like to restart Java application:
:while
START javaw ...
IF errorlevel 10 GOTO while
But it doesn't work because errorlevel condition is evaluated before the java process has finished.
I've also tested the next code but I get an empty text file because of the same reason:
:while
start javaw ... >exit.txt 2>&1
set /p status=<exit.txt
if "%status%"=="10" goto :while
Then, is there any way to launch a Java application, without console (/WAIT is not an option), and using a loop to restart the app when a problem occurs?
See answer on question How to call a batch file in the parent folder of current batch file? to understand the 4 different methods on running an application or batch file from within a batch file.
And see also Difference between java/javaw/javaws.
The execution of the batch file results already in opening a console window.
Therefore I suggest to use simply
:while
java.exe ...
IF errorlevel 10 GOTO while
Or use following code if you don't want to see any output by the Java application in console window of the batch file:
:while
java.exe ... 1>nul 2>nul
IF errorlevel 10 GOTO while
1>nul redirects output written to stdout to device NUL and 2>nul redirects the error messages written to stderr also to device NUL.
The only solution I can think of running a Java application without displaying a console window and additionally check successful execution of the Java application would be to write a Windows (GUI) application in C/C++/C# which does not open a window like javaw.exe and which runs javaw.exe with the appropriate parameters as process with evaluating the return code.
But with a batch file it is impossible to avoid opening a console window completely as far as I know. The console window can be opened minimized, but not hidden completely.
using this solution: Windows batch assign output of a program to a variable you can do something like this:
#echo off
:loop
application arg0 arg1 > temp.txt
set /p status=<temp.txt
if "%status%"=="10" goto :loop
echo "Done."

Resources