redirect input in command line - redirect

I am not really getting the redirect input in DOS mode.
I know the working example: sort < list.txt
which sorts the content of my list.txt
but why doesn't this work:
dir < arguments.txt
the content of my arguments.txt file is for instance just: -D
I would expect the command
dir < arguments.txt
be equal to
dir /D
why isn't this working?
thanks
juergen

The < operator redirects console input to be from a file. The cooresponding > operator redirects the console output to be to a file.
The sort command reads the console until it reaches end of file (cntl-Z) and then it produces the sorted result.
The dir command does not accept console input, only arguements on the command line, so the the file containing /D is never read.

Related

batch redirect stdin and stdout using start command

I tried to start a exe (jtag console) from a batch file and feed the data from a file to it. This worked well with:
nios2-terminal.exe < test.txt
But there are null chars (0x00) in this file. The chars after the null chars are not passed to the exe, just more zeros, probably because of null terminated strings.
So I splitted the file in 4 files where just the last char is a null and feeded them one after another. The problem is I have to kill the processes, because they wont do it thereself. I need to use the start command.
But I don't know how to feed the file not to the start command but to the exe. This does probably the first:
start /d %path% nios2-terminal.exe < test0.txt
ping 1.1.1.1 -n 1 -w 1 > nul REM// 1ms delay, tried more, too
taskkill /f /im nios2-terminal.exe
start /d %path% nios2-terminal.exe < test1.txt ...
As nios2-terminal.exe puts all chars from stdIn to the jtag of my FPGA, I thougth to use start /b /d %path% nios2-terminal.exe and then read all the files with type testx.txt.
When I do this manualy, I have to press Return first and then the first char I type is put on jtag. In a script I don get this working.
Is ther any other posibility like sending input to the running instance of nios2-terminal.exe or some alternative for the JTAG terminal nios2-terminal.exe, which doesn't mind NULL char inputs.
I hope I didn't missed any threat that allready covers this issue, but I found none.
Thanks a lot
edit:
The title says stdin and stdout redirect because I also want to redirect the output of the program to a file. But I think once I got how to redirect the input with the start command, I can handle this, too.
try this:
nios2-terminal.exe > test.txt
and if you want to append use this cmd:
nios2-terminal.exe >> test.txt

CMD: Export all the screen content to a text file

In command prompt - How do I export all the content of the screen to a text file(basically a copy command, just not by using right-clicking and the clipboard)
This command works, but only for the commands you executed, not the actual output as well
doskey /HISTORY > history.txt
If you want to append a file instead of constantly making a new one/deleting the old one's content, use double > marks. A single > mark will overwrite all the file's content.
Overwrite file
MyCommand.exe>file.txt
^This will open file.txt if it already exists and overwrite the data, or create a new file and fill it with your output
Append file from its end-point
MyCommand.exe>>file.txt
^This will append file.txt from its current end of file if it already exists, or create a new file and fill it with your output.
Update #1 (advanced):
My batch-fu has improved over time, so here's some minor updates.
If you want to differentiate between error output and normal output for a program that correctly uses Standard streams, STDOUT/STDERR, you can do this with minor changes to the syntax. I'll just use > for overwriting for these examples, but they work perfectly fine with >> for append, in regards to file-piping output re-direction.
The 1 before the >> or > is the flag for STDOUT. If you need to actually output the number one or two before the re-direction symbols, this can lead to strange, unintuitive errors if you don't know about this mechanism. That's especially relevant when outputting a single result number into a file. 2 before the re-direction symbols is for STDERR.
Now that you know that you have more than one stream available, this is a good time to show the benefits of outputting to nul. Now, outputting to nul works the same way conceptually as outputting to a file. You don't see the content in your console. Instead of it going to file or your console output, it goes into the void.
STDERR to file and suppress STDOUT
MyCommand.exe 1>nul 2>errors.txt
STDERR to file to only log errors. Will keep STDOUT in console
MyCommand.exe 2>errors.txt
STDOUT to file and suppress STDERR
MyCommand.exe 1>file.txt 2>nul
STDOUT only to file. Will keep STDERR in console
MyCommand.exe 1>file.txt
STDOUT to one file and STDERR to another file
MyCommand.exe 1>stdout.txt 2>errors.txt
The only caveat I have here is that it can create a 0-byte file for an unused stream if one of the streams never gets used. Basically, if no errors occurred, you might end up with a 0-byte errors.txt file.
Update #2
I started noticing weird behavior when writing console apps that wrote directly to STDERR, and realized that if I wanted my error output to go to the same file when using basic piping, I either had to combine streams 1 and 2 or just use STDOUT. The problem with that problem is I didn't know about the correct way to combine streams, which is this:
%command% > outputfile 2>&1
Therefore, if you want all STDOUT and STDERR piped into the same stream, make sure to use that like so:
MyCommand.exe > file.txt 2>&1
The redirector actually defaults to 1> or 1>>, even if you don't explicitly use 1 in front of it if you don't use a number in front of it, and the 2>&1 combines the streams.
Update #3 (simple)
Null for Everything
If you want to completely suppress STDOUT and STDERR you can do it this way. As a warning not all text pipes use STDOUT and STDERR but it will work for a vast majority of use cases.
STD* to null
MyCommand.exe>nul 2>&1
Copying a CMD or Powershell session's command output
If all you want is the command output from a CMD or Powershell session that you just finished up, or any other shell for that matter you can usually just select that console from that session, CTRL + A to select all content, then CTRL + C to copy the content. Then you can do whatever you like with the copied content while it's in your clipboard.
Just see this page
in cmd type:
Command | clip
Then open a *.Txt file and Paste. That's it. Done.
If you are looking for each command separately
To export all the output of the command prompt in text files. Simply follow the following syntax.
C:> [syntax] >file.txt
The above command will create result of syntax in file.txt. Where new file.txt will be created on the current folder that you are in.
For example,
C:Result> dir >file.txt
To copy the whole session, Try this:
Copy & Paste a command session as follows:
1.) At the end of your session, click the upper left corner to display the menu.
Then select.. Edit -> Select all
2.) Again, click the upper left corner to display the menu.
Then select.. Edit -> Copy
3.) Open your favorite text editor and use Ctrl+V or your normal
Paste operation to paste in the text.
If your batch file is not interactive and you don't need to see it run then this should work.
#echo off
call file.bat >textfile.txt 2>&1
Otherwise use a tee filter. There are many, some not NT compatible. SFK the Swiss Army Knife has a tee feature and is still being developed. Maybe that will work for you.
How about this:
<command> > <filename.txt> & <filename.txt>
Example:
ipconfig /all > network.txt & network.txt
This will give the results in Notepad instead of the command prompt.
From command prompt Run as Administrator. Example below is to print a list of Services running on your PC run the command below:
net start > c:\netstart.txt
You should see a copy of the text file you just exported with a listing all the PC services running at the root of your C:\ drive.
If you want to output ALL verbosity, not just stdout. But also any printf statements made by the program, any warnings, infos, etc, you have to add 2>&1 at the end of the command line.
In your case, the command will be
Program.exe > file.txt 2>&1

system( ) function in Matlab not recognizing the parameter file name

I have an executable which when run asks for the name of the parameter file. I have tried all styles of inputting the parameter file's name but I get the same error which is:
GAM Version: 2.905
ERROR - the parameter file does not exist,
check for the file and try again
Stop - Program terminated.
ans =
0
The name of the parameter file is gam.par. The various styles that I have tried for the function to automatically read the parameter file's name are:
system('"gam.exe" -f "gam.par"')
system('"gam.exe" -f "gam.par"')
system('"gam.exe" -f gam.par')
system('gam.exe -f gam.par')
system('"gam.exe" /f gam.par')
system('"gam.exe" /f gam.par /o gam.out')
system('"C:\Users\...\gam.exe" /f gam.par /o gam.out')
system(['"C:\Users\...\gam.exe" /f gam.par /o gam.out'])
Where gam.par and gam.par are parameter (input) file and output file, respectively. However, in each of the above case I get the same error message as shown in the beginning.
All my files (input, output, executable etc.) are in same folder. If I use the system() function without using the parameter file's name then it runs without fault and prompts me to enter the parameter file name and when I enter the same file name (i.e. gam.par) on prompt then everything works fine. I want to be able to do that automatically by entering the parameter file name inside the system() argument rather than entering manually on prompt. It will be helpful if anyone can determine why I am not able to get what I am trying to do. Thanks!
According to this page from Mathworks, the syntax is:
system('filename parameter1 parameter2...parameterN')
or in your case:
system('gam.exe gam.par')
Notice the single quotes around the entire argument as well as the spaces between each parameter being passed to the executable application. There is also the full product documentation but I find it less clear than my previous link.
Here is an example. Imagine you had a text file at: C:\filename.txt:
system('type c:\filename.txt')
Now if the file had spaces in its name (or its path), you need to use double quotes:
system('type "c:\my filename.txt"')
Run program in console: \\location\My programm.exe 'param 1' 'param 2'
Run program in Matlab: system(['location\my proramm.exe' '"param 1"' '"param 2')
pathApplicationForm = strcat('"C:\Users\Master\Google Drive\Bakalaura Darbs\Application Development for the Microscopic Models Calibration\Application Form\bin\Debug\Application Form.exe"');
runParam = strcat(get(vEdit2,'String'), '\', get(vEdit3,'String'));
VISSIM = strcat(get(vEdit1,'String'));
system([pathApplicationForm ' "' VISSIM '" "' runParam '']);
It's working ^^

Greater than in command line

I'm trying to understand this Scons command:
env.Command('foo.out', 'foo.in', "sed 's/x/y/' < $SOURCE > $TARGET")
What do the < and > mean in sed 's/x/y/' < $SOURCE > $TARGET?
It means that input to sed will be coming from file $SOURCE and output will be saved to $TARGET.
I'm not sure what's scons, but < redirects the given file to the input stream of the given command (in your case, writes file to the input of sed); and > redirects the output stream of the command to some other file.
So, basically, you run sed on $SOURCE file and redirect the results to $TARGET file.

How can I redirect the output of a command (running in a batch loop) to a file?

I have a windows batch file, which iterates over files in a folder and runs a command on each file. Specifically I am running xmllint to validate some files:
for %%i in (c:\temp\*.xml) do (
C:\XMLLINT\xmllint -noout -schema "C:\schemas\schema.xsd" "%%~dpnxi" >> c:\output.txt
)
It currently shows the output on screen. I want to show the output of all these commands placed in an output file. How can I achieve this? By using the append operator (>>) nothing is accomplished except for a blank file being created.
Is it because of xmllint?
If you're trying to redirect error output from the program, it might be writing to stderr. You can try to redirect it with:
for %%i in (c:\temp\*.xml) do (
C:\XMLLINT\xmllint -noout -schema "C:\schemas\schema.xsd" "%%~dpnxi" >> c:\output.txt 2>&1
)
Basically the 2>&1 at the end means redirect anything from stderr (which is 2) to stdout (which is 1). Since stdout is redirected to a file, you should now see the stderr stream in the file. Hope this works for you!
I've never used it, but if its documentation is here, have you tried just removing your "-noout" option, or adding an: "-output c:\output.txt"?