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
Related
I would want to copy a text to the clipboard using batch script so I don't have to open a text file to select all text and copy. I tried this code:
echo | set /p= hello & echo.world |clip
Output
world
Expected clip (desired)
hello
world
but it only clipped:
world
What are options so I can copy and paste multiple line text or paragraph using batch file?
Thanks!
Like Nico wrote, if you have several lines of text, then you’ll need to wrap them in brackets () like this:
echo off | clip
(
echo This is line one
echo This is line two
echo this is line three
)| clip
More information about it you can find here.
I have 2 files as file1 and file2.
File1 has data as below.
$$NewParameter =
File2 has data as below
23/03/2020 14:34:21
I want result as below in file 1
$$NewParameter = '23/03/2020 14:34:21'
I have tried below command.
type file2.txt >> file1.txt
but it gives me expecyed result without single quotes.
Any help regarding this command would be helpful.
Regards,
Mahesh
not sure what your end goal really is, but this is the best I can think of without using a for loop and batch file.
If you want the result on one line
set /p one=<file1.txt & set /p two=<file2.txt & echo %one% '%two%'>File1.txt
If you want the result on separate lines
set /p two=<file2.txt & echo '%two%'>>file1.txt
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 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 >>.