windows comp command in batch script: remove prompts - windows

Every time I use the Windows comp command, a message containing:
Compare more files (Y/N) ?
is shown. Can I avoid it by typing N by default?
thanks

You can pipe an N in there:
echo N | comp file1.txt file2.txt

You could also consider using another, also built-in, command, FC. It can perform binary comparison (you need to specify the /B switch: FC /B file1 file2), which seems closer to what COMP does, although the results are still displayed in a different format.
Anyway, it doesn't prompt you for more files to compare, it just does the job and terminates.

Related

Windows batch file - automatic answer to all prompts

I am running a program that generates a batch file. The batch file has a bunch of prompts that I want to answer with RETURN:
Example:
Type X to quit or <RETURN> to proceed
I have tried:
echo. | batchfile.bat
This only answers the first prompt. I want to answer all prompts, and there are a lot of prompts.
You have 3 ways to do it.
1. As #MarcB mentioned in the comment you can create a file with as much returns as you need, then use the type file.txt| batchfile.bat command
2. Similar like the 1st one, but the command is: batchfile.bat < file.txt
3. Chaining commands (answer with 3 returns): (echo.&echo.&echo.) | batchfile.bat normally, this also can be formatted like this:
(
echo.
echo.
echo.
) | batchfile.bat
Also if your answer (in your case not) contains some special character which is an operation like & | > < you will need to escape them with ^ sign. Like (echo Stan ^& Pan&echo old series) result will be in this case:
Stan & Pan
old series
Side note: Also I don't know what was your usage, but if I had access to modify that batch file, than I would add a new option (like y) and if that option is present, I don't ask anything in the batch file and I answer the question inside the batch file (if there's other programs which asks something). This will make a bit easier the script usage (like: batchfile.bat y).

How to automate application based on command line?

I have a Java based application which I run using command prompt. I am currently testing it manually. I want to automate the process. What tool or scripting should I use?
For example:
First I will run the java command to run the .java file.
My application will give me 5 options. For example
i. Add
ii. Subtract
iii. Multiply
iv. Divide.
It should select one of the options and add the number and verify the result with the expected result. All this will be done using command prompt.
I realize that you're asking for a command prompt solution, but you really should look into JUnit. It will be a much more maintainable solution for a larger variety of test conditions. If you use the command line, you'll have to parse all of the input and output. In order to truly test your code, you shouldn't have to go through the intermediate of the command line (stdin, stdout); you should have Java code that runs your Java code, since it can access the variables.
If you really are set on using the command line, you would have to give us more details on how your program runs. This is just a shot in the dark:
inputs.txt (These are the inputs that will be fed to your program)
Add
4
6
test.bat
#ECHO OFF
:: I just assume that your program prompts for input and waits for stdin
java YourMainClass < inputs.txt > testOutput.txt
:: Program prompts for an operation; "Add" is read from inputs.txt
:: Program prompts for values; "4" and "6" are read from inputs.txt
:: Program prints out the result, which is redirected to testOutput.txt
:: Now you have to read in the testOutput.txt file and look at the last line
FOR /F %%i IN (testOutput.txt) DO SET RESULT=%%i
:: The variable RESULT now contains your program's answer
:: Create a variable to compare your actual result with the expected result
SET EXPECTED=10
IF %RESULT% == %EXPECTED% ECHO "You were correct"
IF NOT %RESULT% == %EXPECTED% ECHO "You were not correct"
You need to use a batch file. Put your commands in a notepad file and save it as .bat.
If they are not native batch commands though you may need to change some things, but you would need to post you commands before I can confirm.
Standard input can be fed into java application. If you're using bash it can be done like the following:
java myapp.java < test1
Where myapp.java is your application with main-method, and test1 being your test input (a plain old textfile).

Printing a sample from a huge file

I have a rather large text corpus, of which I would like to check a few lines to see if the format is correct (and to just generally get some idea of its contents). Is there a simple one-liner that can be used to print just the first few lines of a huge text file?
Personally I'm using PowerShell, but answers are appreciated for bash and several other shells.
In powershell
get-content c:\filename.txt -TotalCount 3 #here just the first 3 line.
The n first lines
head -n filename
The n first bytes
dd if=filename bs=1 count=n
$ head yourfile.txt
I'm certain there's an equivalent in PowerShell. I mean, there must be, right?
Edit: Yep. Windows equivalent of the 'tail' command
You can use less. It's very efficient with large files. And, if you need to see more* you can continue paging through the file.
*"less is more"

Hiding lines of DOS output starting with a certain word

I have a java program that I am running inside of a command prompt shell which generates a lot of output. Many of the lines start with the word "Prepared" or "prepared", and are not really necessary for me to see when running the program- they actually make it harder to see the data that I am interested in. Is there a way to hide any lines starting with these words while running the program?
Change your program to use log4j or something like it, and then control which levels of message are sent to the console.
you can pipe the output to:
FIND /V /I "string you want to omit"
This will remove all iterations of said string from your output.
myJavaApp | findstr /iv prepared

Is there replacement for cat on Windows

I need to join two binary files with a *.bat script on Windows.
How can I achieve that?
Windows type command works similarly to UNIX cat.
Example 1:
type file1 file2 > file3
is equivalent of:
cat file1 file2 > file3
Example 2:
type *.vcf > all_in_one.vcf
This command will merge all the vcards into one.
You can use copy /b like this:
copy /b file1+file2 destfile
If you have control over the machine where you're doing your work, I highly recommend installing GnuWin32. Just "Download All" and let the wget program retrieve all the packages. You will then have access to cat, grep, find, gzip, tar, less, and hundreds of others.
GnuWin32 is one of the first things I install on a new Windows box.
Shameless PowerShell plug (because I think the learning curve is a pain, so teaching something at any opportunity can help)
Get-Content file1,file2
Note that type is an alias for Get-Content, so if you like it better, you can write:
type file1,file2
Just use the dos copy command with multiple source files and one destination file.
copy file1+file2 appendedfile
You might need the /B option for binary files
In Windows 10's Redstone 1 release, the Windows added a real Linux subsystem for the NTOS kernel. I think originally it was intended to support Android apps, and maybe docker type scenarios. Microsoft partnered with Canonical and added an actual native bash shell. Also, you can use the apt package manager to get many Ubuntu packages. For example, you can do apt-get gcc to install the GCC tool chain as you would on a Linux box.
If such a thing existed while I was in university, I think I could have done most of my Unix programming assignments in the native Windows bash shell.
If you simply want to append text to the end of existing file, you can use the >> pipe. ex:
echo new text >>existingFile.txt
So i was looking for a similar solution with the abillity to preserve EOL chars and found out there was no way, so i do what i do best and made my own utillity
This is a native cat executable for windows - https://mega.nz/#!6AVgwQhL!qJ1sxx-tLtpBkPIUx__iQDGKAIfmb21GHLFerhNoaWk
Usage: cat file1 file2 file3 file4 -o output.txt
-o | Specifies the next arg is the output, we must use this rather than ">>" to preserve the line endings
I call it sharp-cat as its built with C#, feel free to scan with an antivirus and source code will be made available at request
I try to rejoin tar archive which has been splitted in a Linux server.
And I found if I use type in Windows's cmd.exe, it will causes the file being joined in wrong order.(i.e. type sometimes will puts XXXX.ad at first and then XXXX.ac , XXXX.aa etc ...)
So, I found a tool named bat in GitHub https://github.com/sharkdp/bat which has a Windows build, and has better code highlight and the important thing is, it works fine on Windows to rejoin tar archive!
Windows type command has problems, for example with Unicode characters on 512 bytes boundary. Try Cygwin's cat.
If you have to use a batch script and have python installed here is a polyglot answer in batch and python:
1>2# : ^
'''
#echo off
python "%~nx0" " %~nx1" "%~nx2" "%~nx3"
exit /b
rem ^
'''
import sys
import os
sys.argv = [argv.strip() for argv in sys.argv]
if len(sys.argv) != 4:
sys.exit(1)
_, file_one, file_two, out_file = sys.argv
for file_name in [file_one, file_two]:
if not os.path.isfile(file_name):
print "Can't find: {0}".format(file_name)
sys.exit(1)
if os.path.isfile(out_file):
print "Output file exists and will be overwritten"
with open(out_file, "wb") as out:
with open(file_one, "rb") as f1:
out.write(f1.read())
with open(file_two, "rb") as f2:
out.write(f2.read())
If saved as join.bat usage would be:
join.bat file_one.bin file_two.bin out_file.bin
Thanks too this answer for the inspiration.

Resources