How let cmd output less info - batch-file

I have a batch file, for example: test.bat, there is only one command in it:
echo Hello
Then I run it:
D:\cmd\test.bat
Then it outputs the result:
//output begin
-- This is a blank line, I don't want this
D:\cmd\echo Hello -- This is not what I want
Hello -- I only want to print this line
-- This is a blank line, I don't want neither.
//output end
I only want to print the output of commands run in the batch file, here is the echo command.
But, there are two blank lines, one is at the head of the result, and the other one is at the end of the result. And the D:\cmd\echo Hello in the result is neither not what i want.
What should I do? Any help would be great, thanks in advance.

Just put
#echo off
in the first line of your batch file. This will prevent output of the command's invocation when they are run (as does prepending a command with #), so it eliminates all the lines that bother you.
Taken from http://ss64.com/nt/echo.html
Type ECHO without parameters to display the current echo setting (ON or OFF).
In most batch files you will want ECHO OFF, turning it ON can be useful when
debugging a problematic batch script.
In a batch file, the # symbol is the same as ECHO OFF applied to the current line only.
Normally a command is executed and takes effect from the next line onwards, #
is a rare example of a command that takes effect immediately.

Related

Store return value of a compiled exe into a batch variable

I've read topics here and on the internet about the argument but the solutions offered do not work for me.
Firstly I'll tell you what I want to do:
I have a compiled c file (.exe) that returns various integers depending on the situation.
I want to store said return value in a variable in batch. From what I've read, there's no specific command to do this (like 'v=$?` in shell that assigns to the variable the last returned value), but I found instead a workaround that uses the for loop.
The code I found is the following:
FOR /F "tokens=*" %%a in ('test.exe') do SET OUTPUT=%%a
echo %OUTPUT%
But when I run the batch file, I get ECHO is off.
I'm a complete beginner in batch, I simply searched "store return value in batch" and the code above is what got spit out. Any insight or help on the problem is appreciated, thanks in advance!
Answer given by Mofi
Dillon Wreek wants the exit code (also called exit value, return
value, return code or in CMD syntax errorlevel) of test.exe and not
the output of the text.exe written to handle stdout. So all needed by
Dillon Wreek is a batch file with first line #echo off, second line
test.txt, third line echo %errorlevel% resulting in writing into
console window the value 1 or 2 or 3 or ... 12 and fourth line pause
to see the output on double clicking on batch file.
Basically:
test.exe
set var=%errorlevel%
echo %var%

How to pass commands in a text file to an EXE running through the command line with batch?

I have a text file with lots of commands in it and I want to sent those commands to a software called thermocal. It is a console application. I found the command below, but it doesn't work for me.
Do I need to put this .exe file in the same folder of the batch file to make it work or any thing else?
type somefile.txt | Thermocal.exe
Batch scripts can be considered as a collection of lines you could also type in a command line prompt one after another. With respect to this it might be helpful for you, to play with cmd in order to get a feeling for what is happening.
About starting thermocal: Assuming thermocal is not part of PATH then the batch file either needs to change the current directory to the one with termocal.exe. Alternatively you might be able to call thermocal.exe with adding a path like C:\ProgramFiles\Thermocal\thermocal.exe . Play with cmd to find out, what works and what doesn't
When you are able to start thermocal from the command line prompt window, you can start experimenting with the call. You will probably end of with something like this in your command line window:
C:\ProgramFiles\Thermocal> thermocal argument1 argument 2
If this works, you can start with batch programming :)
Assuming your arguments are stored in somefile.txt like this:
argument1 argument 2
TYPE does nothing more than printing a file:
TYPE somefile.txt
Now you need to use the result of the output as command line arguments:
for /f %%i in ('type somefile.txt') do (thermocal.exe %%i)

How to create a program to execute shell command in Windows?

I am trying to execute a shell command or batch file in LiveCode, however, for reasons unknown, it is not working. I would like to use another intermediate program to execute the batch file that records the output to a text file and then read that output with LiveCode as a workaround. What is a simple way to create an executable that can process a batch file?
There is not really any relevant code to share other than
put "test.bat" into tCommand
put shell (tCommand) into fld "output"
The following script works in LiveCode 6.7.6:
set the hideConsoleWindows to true
put shell("C:\test.bat")
My bat file contains
#echo off
echo 'test'
pause
and the value returned by shell() is
'test'
Press any key to continue . . .
The last character of the value returned is a linefeed.
Perhaps you should try to reproduce this simple test.

Why won't a batch file run when being called from within another batch file?

I have a batch file that first creates another batch file containing a ClearCase cleartool command and second, runs it:
ECHO cleartool lsactivity -long "%ACTIVITY%"^>"%OUTPUTFILE%">FILETORUN.bat
CALL FILETORUN.bat
When running the batch, FILETORUN.bat is generated with the correct format, but the CALL to it is completely ignored.
If I ECHO output after the CALL to a log file, I can see that the script just skips over it.
What could it be?
I have tried removing CALL but it makes no difference.
EDIT: SOLUTION
Thank you all for the input. I found the problem. Before the write to batch and batch call in the script there was a command that read information into a variable from a file:
SET /p FILETODELETE=<rmname_%CLEARCASE_USER%.tmp
It reads only the first line. For some reason this created a conflict with temporary batch file, and I have no idea why. I used a different solution for reading the first line from a file and the conflict doesn't happen anymore:
(set FILETODELETE=)
for /f "delims=" %%q in (rmname_%CLEARCASE_USER%.tmp) do if not defined FILETODELETE set FILETODELETE=%%q
If anyone can shed some light it would be great!
SET /P waits for user input, so it actually will finish the command with what you are trying to execute after that and consume the input buffer, which might produce different results on each machine.
See set command reference for more details

Stupid Batch File Behavior. Tries to execute comments

I have tried prefixing lines with semicolons, 'REM', etc.. but no matter what when I run my batch file I keep getting "unknown command REM whatever"
"REM test" It is not recognized, and it is windows vista. I simply get "rem" output back to my console.
That's entirely normal behavior. Batch files are simply sequences of commands that are run one after another. So every line will get output to the console as if it were typed there.
H:\>echo rem test > test.cmd
H:\>test
yields the output
H:\>rem test
as if I typed rem test directly to the console.
You can suppress this by either prefixing the line with #:
#rem test
or by including echo off in the batch file:
#echo off
rem test
If I put ":: test" and execute it I get back "Test".
Can't reproduce here.
If I put "; test" it recursively executes itself
A semicolon at the start of the line seemingly gets ignored.
If you're talking about cmd.exe batch files under Windows, you can use:
rem this method or
:: this method.
For bash and a lot of other UNIX-type shells, you use:
# this method.
I'm pretty certain you're not using cmd.exe since that would give you an error like:
'rem' is not recognized as an internal or external command,
operable program or batch file.
rather then:
Unknown command ...
If you are using a UNIX-type shell, the # character is almost certainly what you're after. If you let us know exactly the shell you're using, we can probably help out further.
you probably created an UNICODE file. These files contain 2 bytes header named BOM
which is not shown by any editor but cmd attempts to execute them and fails.
To make sure this is indeed an issue: type any other command at the very beginning
of your file and see it throws the same error - for example #echo test
To fix it, just create a new plain text file and copy content of the original file there.
then remove the original file and replace it by the newly created one.
In my case the problems are line endings. Somehow Maven or the Jenkins pipeline running on a Linux machine changed the line endings from Windows style (CR LF) to Unix style (LF). Changing them back solves the issue for me.

Resources