I want to use echo to add data into a text file progressively. I wrote a small batch code as follows:
#echo off
echo >text.txt
set DllCopier_d=./DllCopier
echo %DATE:~04%>text.txt
echo %TIME:~0,5%>text.txt
echo %~dp0%>text.txt
When I look at text.txt at the end, i found only one line:
C:\omsstest_automation\win32\
Which is result of last line.
Why is the "echo" resulting into replacing previous contents of text.txt?
> means create a new file with these contents (replace the old if it exists), >> means append or create a new file if none already exists.
So to spell out the answer to your question, replace subsequent usages of > after the very first one with >>.
Related
i have a problem when I want to output several variables to txt file in shell. I want them in one line, but there appear line break after first and third variable. I have for example this
for s in `ls $TESTDIR/results_orig.txt`
do
t_a=(`grep " a" $s`);
t_b=(`grep "Bytes written to file: " $s`);
t_c=(`grep " Total Time:" $s`);
t_d=(`grep "QP " $s`);
a=${t_a[2]};
b=${t_b[4]};
c=${t_c[3]};
d=${t_d[2]};
echo "$a $b $c $d" >> $TESTDIR/results.txt;
done
I got the variables t_a to t_d are after I parse original txt file to find some values in it. Than I want to find some number ant write them in file without other text that I have in original file. And I want it to write those parsed numbers in one line in new file, like
13.2 1678 3231.5 2422.1
And I get
13.2
1678 3231.5
2422.1
When I echo any variable separately to the console I get right values. So variables are parsed fine. What could be wrong?
Thank you.
Right let me rewrite this try to make it more clear.
Picture added to make this even clearer:
I have two files
File 1, contains words.
file 2, contains commands.
I need to put words from FILE 1
into FILE 2
I cannot copy-paste them one by one, because there is a LOT of words in FILE 1
File 1 is listed in alphabetical order (by first letter)
File 2 the command does not change
The issue is getting words from file 1 into file 2
but they have to be moved into quotes " " in file 2
so a script that could for example..
Take apple from file 1 and move it between quotes admin.executemotecommand "apple"inside file 2 as it goes down the list keeping the words in order as they move them across.
This could perhaps be done the same way around in which, the script writes the command in front of the words in file 1 as it goes down file 1's list
Is this even possible? I've never seen this done anywhere else and completely clueless if batch is even the right language for it.
The question is a little confusing, but based on your responses in the comments my understanding is that you don't necessarily need the script to edit a preexisting file 2, because you're repeating the same command(s) for each word, so the script can just create a new file based on the words in file 1.
You can do it at the prompt like this:
FOR /F %a IN (words.txt) DO ECHO admin.executeremotecommand "%a" >> commands.txt
The original version of the question indicated that you want more than one command for each word. I take it you changed that in order to simplify that question, and figured you'd just run the script once for each command? However, it's quite simple to have it produce more than one command for each word:
FOR /F %a IN (words.txt) DO (ECHO first.command "%a" & ECHO second.command "%a") >> commands.txt
In a batch file, you'd do it this way:
#ECHO OFF
FOR /F %%a IN (words.txt) DO (
ECHO first.command "%%a"
ECHO second.command "%%a"
) >> commands.txt
BTW, in the code in some of your comments, you surrounded the variable with %'s (%A%). That's incorrect; it would evaluate to the value of %A followed by a literal %. Surrounding with %'s is used only for environment variables. (Note that the %'s around environment variables do not get doubled in a batch file. For example, to get the current date, use ECHO %date% both at the prompt and in a batch file.)
Using a batch file (.bat), I'm making a script that requires dynamic paths so that it can work on multiple computers. My problem is when I echo something to a file, it adds a line and an a return carriage.
Say I have a text file named foo.txt in the directory of the batch file, and its contents are completely empty.
In the batch file, I run:
echo test > foo.txt
The contents of foo.txt will be:
L1: foo
L2:
There would be a space after foo in the first line and a second empty line. Now, this would be completely okay and I would entirely ignore it, but filename paths do not ignore it.
importing text from foo.txt like so:
set /p foo=< foo.txt
...and then:
set /p name=< C:\A.D.V.E.N.T.U.R.E.\test\%foo%\test2.txt
...would be interpreted as:
set /p name=< C:\A.D.V.E.N.T.U.R.E.\test\foo \test2.txt
Including an unwanted space. Is there anyway to make it so you can write text to a file without a space, or a command one could use to delete the carriage return and the space?
You can also use parentheses to make sure unwanted space is not included in the output:
(echo test) >foo.txt
The data should be test, not foo
Clasically, try
>foo.txt echo test
but make sure that there are no trailing spaces after test.
(to APPEND to foo.txt use >> in place of >)
above given answers works.
But, the actual reason I found for it was the space before >
So, instead of
echo test > foo.txt
it must be
echo test> foo.txt
NOTE. Don't put any space between test and >. This, results in a trailing space.
I am need creating a batch file to take an input and separate the content in different lines to a file.
For example:
Apple Banana Orange
And make the file look like:
Apple
Banana
Orange
Is there a way to do this?
You can create a script based on the following to ask for input and write the different items to the file result.txt
#echo off
set /p fruit=Give input:
for %%f in (%fruit%) do (
echo %%f >> result.txt
)
If result.txt already exists, it will append the new items to the existing file. If you do not want that, then just delete result.txt at the beginning of the script.
You can either enter the input by hand, or take input from a file using something like fruit.bat < input.txt
I have a batch file that does this.
ECHO A41,35,0,a,1,1,N,"Mr ZACHARY KAPLAN">> test.txt
There are about 30k similar lines. It takes the batch file about 5 hours to run.
Is there a way to speed this up?
/Jeanre
Try this:
Put an ECHO OFF at the top of the batch file.
Then change each line to:
ECHO A41,35,0,a,1,1,N,"Mr ZACHARY KAPLAN"
and call your batch file:
mybatch.bat >> test.txt
Edit the first line to remove the echo off print out.
If You do this, it will be way quicker:
JAM DO %%Fo CAt Pa (set /p %yodo%=jol)
RUn
Write a custom script or program to open the file test.txt once, and write all the data into it in one shot.
Right now each line is executed separately by the command interpreter, and the file is opened and closed each time.
Even a small qbasic program should be able to strip out the data between the echo and >> and write it to a text file more quickly than your current method.
-Adam
you can use a scripting language to strip the leading ECHO and trailing >> test.txt with a small regular expression
here's an example in python:
>>> import re
>>> text = 'ECHO A41,35,0,a,1,1,N,"Mr ZACHARY KAPLAN">> test.txt'
>>> re.sub( r"ECHO\s*(.*?)>>\s*test.txt", r"\1", text )
'A41,35,0,a,1,1,N,"Mr ZACHARY KAPLAN"'
do this for all lines in the file:
import re
f = open("input.bat")
of = open("output.txt", "w" )
for line in f:
of.write( re.sub( r"ECHO\s*(.*?)>>\s*test.txt", r"\1", line ) )
I didn't test this code ...
Here's an example using a Java program - with BufferedReader/PrintWriter
http://www.javafaq.nu/java-example-code-126.html
You can also use a BufferedReader and BufferedWriter
http://java.sun.com/docs/books/tutorial/essential/io/buffers.html
http://leepoint.net/notes-java/io/10file/10readfile.html