Bat file prints multiple statements - batch-file

I am invoking bat command in C++.
The command below writes to console "connecting to bootrom: connected . writing 0x001A13".
system("D:\abc\abc.exe -u load D:\abc\13oct\agi\agit")
but when i execute same command to dump the above string into file like this:
system("D:\abc\abc.exe -u load D:\abc\13oct\agi\agit">>D:\abc\13oct\tempFile.txt");
It appears that the temp file is having multiple instances of connected . writing 0x001A13 in tempFile.txt
Does any body point me an appropriate fix for this.
Thanks in advance!

OK, I hope I understood this well: The same output you get once if the first command appears twice in the temp file.
That's probably because you're using >> for redirection, which doesn't replace the ouput file, but appends to it.
That means, if you execute echo Test >> tempfile.txt twice, it will have two lines reading Test.
If you want to save only the ouput of the last command to the file, use > instead of >>.

Related

writing the a local file using parameters on command line

I am trying to write a little project thing to get my javascript skills up. The goal is for, when using curln, the .cmd file will write %~dp0\#temp with the curl %1 (the curled html result of the first parameter)
expected with curln "https://www.google.com": (long html)
actual: "<< was unexpected at this time."
I have tried looking things up, this was a mesh of what I found online. I have tried using < and << as, a beginner, I don't understand the difference.
Code:
%~dp0\#temp << cmd curl %1
I would like the output of curl to be written to the current directory's (%~dp0) file named #temp. %1 is a preset *.cmd parameter variable that is shown in this example:
example "this is %1"
> and >> are output. The first creates a new file, the second appends to an existing file. < is input. It accepts input (reads) into a program from another source, which is typically a file or the output of another program. It doesn't work with writing to files.
You're most likely looking for curl %1 >>"%~dp0#temp" instead. You don't need the cmd in front of it, as you're already in a command window when the batch file executes.
For a simple transfer I use repeatedly using curl the minimal call is
curl -o "%~dpnx0" remote.url
That ensures the fileName and eXtension are saved in the current DrivePath
There is no error checking or security communication so is only useful for a single .html or .js or .pdf etc.
Thus a complex call can run to dozens of lines.

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.

How to write standard error in text file using batch command

For one file it's showing standard error on console i.e. command prompt. I want to print it on text file.
some_file.exe >> text.file 2>&1
this will print all output to text file.For more info.
You should use
command 2>file
See http://support.microsoft.com/kb/110930

How to write sequential commands in batch file

I want ot execute the following commands using batch file:
1.ftp 127.0.0.1
2.USERNAME
3.PASSWORD
4.put
5.D:\\zz.xml
6.mmm.xml
Each command is a sequence of the prev command
I want to move file from local to ftp server
I googled for that and I found that insert ; between commands is the solution but itsn't correct for me
Please support me
1 is a command on the command prompt, but 2..6 are FTP commands.
I would put lines 2..6 in a file called commands.txt then redirect that file to stdin.
ftp 127.0.0.1 < commands.txt
Edit:
You could use -s:commands.txt instead of the < according to the ftp help.
-s:filename Specifies a text file containing FTP commands; the
commands will automatically run after FTP starts.
Use a CRLF between each command.
I would rather use the expect command that allows you to script inputs according to outputs.
You can find some information here: http://en.wikipedia.org/wiki/Expect
You have an example on that page that describes exactly what you want to do.
#Albin Sunnanbo
Thanks for your reply
I solved the problem now from the following tutorial
http://www.dostips.com/DtTipsFtpBatchScript.php
I write the foillowing commnads in batch file:
FTP -v -i -s:commands.txt
then commands.txt contaisn the following commands
open 127.0.0.1
UserName
Password
put
D:\\zz.xml
mmm.xml

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