Append data from one file to another windows command line - windows

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

Related

How to nslookup and write to txt file using a batch script.

Im in the middle of a small programming assignment right now, and am stumped. What needs to be done is->
Create a batch file to perform the following steps ( written the best I can in pseudocode )
IF USERDOMAIN==COMPUTERNAME
THEN
CreateFile cake.txt
cake.txt = %USERNAME% + " " + %COMPUTERNAME% + " " + nslookup(www.disney.com)
How far I've gotten on the other hand is so=
IF USERDOMAIN == COMPUTERNAME GOTO Text
:Text
echo.>"C:\cake.txt"
rem Saved in C:\cake.txt
echo USERNAME >> cake.txt
echo COMPUTERNAME >> cake.txt
I only know C#, and using stackoverflow I have found this similar question:
Batch script for loop & nslookup
But, the syntax in that question is just of nslookup in general, I dont understand the windows batch syntax at all, I got this far through a couple hours of searching.
It should be:
#echo off
rem case insensitive string comparison
if /i %USERDOMAIN% == %COMPUTERNAME% (
rem clear file
break>"C:\cake.txt"
rem output all commands in following block to file
(
echo %USERNAME%
echo %COMPUTERNAME%
nslookup www.disney.com
) >> "C:\cake.txt"
)
if you want to filter the output of nslookup you have to do it differently but as you don't tell about how the output should look like this mabe should be enough.
I dont understand the windows batch syntax at all
If you would be more specific about your problems it would be possible to explain you the things you didn't get.
A good reference about batch scripting is the site SS64. Every command is very well explained there and completed by examples.
Why did you use #echo off? in the start, and use echo later on? Isn't it just a way to output status info of how the code is progressing? The purpose of the /i is also unknown to me, and finally, why did you break before handling the file? I read that a single > is for removing and clearing text, so why would that need a break of some sort?
1.
#echo off and echo:
The #echo off command is used so that the commands itself are not displayed during the batch processing, consider the following example:
#echo off
echo Hello world with echo off!
#echo on
echo Hello world with echo on!
In your command prompt it would print something like:
Hello world with echo off!
C:\Users\...\Desktop>echo Hello world with echo on!
Hello world with echo on!
The echo command is just used to output something to the console.
2.
if /i:
The /i will compare the strings case insensitive so that "ABC" == "abc" or "abC" == "AbC" will be true.
3.
break and > or >>:
To clear a file you used echo.>"C:\cake.txt". This is not completely wrong but that will save a new line into the file and not clear it as echo. means new line. The break command will generate no output (see here on SO), if that will be redirected to a file with > the file will be completely empty.
You differ between > and >> when talking about redirection. A single > means overwriting the existing file if it already exists and a double >> means append to file or create a new file if it doesn't exists.

How to replace the string in the file through command line in the most simple way?

Suppose the file 'file', in which there is the string
c:\home users\XXX\
How to replace this string into
d:\street users\YYY\ ?
P.S. The question holds after my failure with fart, see here.
set /p "d:\street users\YYY\"<nul >test.txt
dir /b c:\home users\XXX\file >>test.txt
Explanation:
First line creates a file that does not end with a new line. Second line appends a "BARE" listing of file. Hence:
type test.txt
should give you
d:\street users\YYY\file
If you want to execute a command then instead of a text file create a batch file.

Word Sorting in Batch

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.)

Make a Batch File Separate Words to Different Lines

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

Optimizing a batch file which prints 30,000 lines of text into a file

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

Resources