Batch to create .txt file based off two files

January 28, 2011 at 14:41:57
Specs: Windows 7
Hi, first time posting but long time follower...

Batch experience: noob..

Here is what I need to do:
I need to created 160 individual files titled %name%.txt based off the entries in another .txt file. But contents of of each txt file needs to have parts changed depending on another file.. These are going to be 160 .vnc connection settings based off an IP listing in a text file.

So, here is what I have..
YardCPlist.txt = this is my list of IP's on our local network.(example: 10.2.1.39)
VNC.txt = this is a *.vnc connection renamed to *.txt, this carries the information needed for connection. The one line that needs to change per IP is "host=10.2.1.39" (no quotes)

So, here is my cod i have now..
@for /f %%a in (YardCPlist.txt) do call :work %%a
goto :EOF

:work

ECHO mytext>%1.vnc

This will create individual *.vnc connections based off my YardCPlist.txt file (10.2.1.39.vnc), though it will only put in 'mytext' in the file.. which i planned as a test to see how to create multiple files based off a list. but no i need to take the contents of VNC.txt and create connections based off YardCPlist.txt.

I need to make a variable somehow so it creates a file named from YardCPlist.txt, adds the entry i need in the text from VNC.txt

Now I have tried to just copy and past my whole VNC connection text into the batch but it fails to create files. something like this..

ECHO [connection]
host=%1
port=5900
proxyhost=
proxyport=5900
password=
[options]
use_encoding_0=1
use_encoding_1=0
use_encoding_2=1
use_encoding_3=0
use_encoding_4=1
use_encoding_5=1
use_encoding_6=1
use_encoding_7=1
use_encoding_8=1
use_encoding_9=1
use_encoding_10=0
use_encoding_11=0
use_encoding_12=0
use_encoding_13=0
use_encoding_14=0
use_encoding_15=0
use_encoding_16=1
preferred_encoding=16
restricted=0
viewonly=0
nostatus=0
nohotkeys=0
showtoolbar=1
AutoScaling=1
fullscreen=0
autoDetect=0
8bit=1
shared=1
swapmouse=0
belldeiconify=0
emulate3=0
emulate3timeout=100
emulate3fuzz=4
disableclipboard=0
localcursor=2
Scaling=0
scale_num=1
scale_den=1
cursorshape=1
noremotecursor=1
compresslevel=6
quality=6
ServerScale=1
EnableCache=0
QuickOption=3
UseDSMPlugin=0
UseProxy=0
DSMPlugin=
>%1.vnc

Thanks for reading any advice..


See More: Batch to create .txt file based off two files

Report •


#1
January 28, 2011 at 15:18:09
I think you missed something. Shouldn't your code be...

@for /f %%a in (YardCPlist.txt) do call :work %%a
goto :EOF

:work
ECHO mytext > %1.vnc
goto :EOF

I think you are trying to to append your text to the contents of a VNC file. The VNC file name is being determined by the contents of YardCPlist.txt. Atleaste that is what your code is written to do. If the YardCPlist.txt is not a list of file names and you want to add the contents of YardCPlist.txt file to an existing VNC file then your code would simply be...

echo YardCPlist.txt >> afile.vnc

This will append the contents of your YardCPlist.txt to the bottom of the afile.vnc.

I really am having a hard time figuring out what you want. Give us examples of what is in the YardCPlist.txt file and what you want your final afile.vnc to be named and what you want the contents to look like.


Report •

#2
January 28, 2011 at 15:27:43
I think what you want to do is create the following...

footer.txt

then in the footer.txt place the following...

port=5900
proxyhost=
proxyport=5900
password=
[options]
use_encoding_0=1
use_encoding_1=0
use_encoding_2=1
use_encoding_3=0
use_encoding_4=1
use_encoding_5=1
use_encoding_6=1
use_encoding_7=1
use_encoding_8=1
use_encoding_9=1
use_encoding_10=0
use_encoding_11=0
use_encoding_12=0
use_encoding_13=0
use_encoding_14=0
use_encoding_15=0
use_encoding_16=1
preferred_encoding=16
restricted=0
viewonly=0
nostatus=0
nohotkeys=0
showtoolbar=1
AutoScaling=1
fullscreen=0
autoDetect=0
8bit=1
shared=1
swapmouse=0
belldeiconify=0
emulate3=0
emulate3timeout=100
emulate3fuzz=4
disableclipboard=0
localcursor=2
Scaling=0
scale_num=1
scale_den=1
cursorshape=1
noremotecursor=1
compresslevel=6
quality=6
ServerScale=1
EnableCache=0
QuickOption=3
UseDSMPlugin=0
UseProxy=0
DSMPlugin=

Then you could use the following code...

@echo off
@for /f %%a in (YardCPlist.txt) do call :work %%a
goto :EOF

:work
echo host=%1 > %1.vnc
echo footer.txt >> %1.vnc
goto :EOF

I guess this is what you are trying to do.


Report •

#3
January 28, 2011 at 15:29:44
or maybe simply this will work...

@echo off
@for /f %%a in (YardCPlist.txt) do call :work %%a
goto :EOF

:work
echo host=%1 > %1.vnc ::Makes file with IP address for the name and adds host= and the IP.
echo vnc.txt >> %1.vnc ::Appends the contents of vnc.txt to the bottom of the new file.
goto :EOF

Try both methods and let me know if one of them was the wright guess.


Report •

Related Solutions

#4
January 28, 2011 at 15:45:00
@ace, Thank you so much for a fast response too!

Ok both examples you gave me the individual files i needed but the contest of the files are only listing

host=10.2.128.39
vnc.txt

not the whole code from VNC.txt

Sorry my questions were confusing..
I have two goals, create individual files named *.vnc based off my YardCPlist.txt file. This works great..
2nd goal is to add the text from VNC.txt into each file but it needs to change the entry " host= " to include the IP address..

So, in my YardCPlist.txt is a list of IP's (there's about 160 of these ip's)
10.2.1.39
10.2.2.39
10.2.2.38
10.2.3.39
10.2.4.39
Now, these IP's will be used to name my individual vnc files. like the first one for example, will create a file called 10.2.1.39.vnc (this works great)

Now inside this 10.2.1.39.vnc file should be text information that will all be static except for one line. This host= will change for each file based off the YardCPlist.txt file.

[connection]
host=10.2.1.39
port=5900
proxyhost=
proxyport=5900
password=
[options]
use_encoding_0=1
use_encoding_1=0
use_encoding_2=1
use_encoding_3=0
use_encoding_4=1
use_encoding_5=1
use_encoding_6=1
use_encoding_7=1
use_encoding_8=1
use_encoding_9=1
use_encoding_10=0
use_encoding_11=0
use_encoding_12=0
use_encoding_13=0
use_encoding_14=0
use_encoding_15=0
use_encoding_16=1
preferred_encoding=16
restricted=0
viewonly=0
nostatus=0
nohotkeys=0
showtoolbar=1
AutoScaling=1
fullscreen=0
autoDetect=0
8bit=1
shared=1
swapmouse=0
belldeiconify=0
emulate3=0
emulate3timeout=100
emulate3fuzz=4
disableclipboard=0
localcursor=2
Scaling=0
scale_num=1
scale_den=1
cursorshape=1
noremotecursor=1
compresslevel=6
quality=6
ServerScale=1
EnableCache=0
QuickOption=3
UseDSMPlugin=0
UseProxy=0
DSMPlugin=



Report •

#5
January 28, 2011 at 16:21:35
Oops yep, my bad. Try this...

@echo off
@for /f %%a in (YardCPlist.txt) do call :work %%a
goto :EOF

:work
echo host=%1 > %1.vnc ::Makes file with IP address for the name and adds host= and the IP.
type vnc.txt >> %1.vnc ::Appends the contents of vnc.txt to the bottom of the new file.
goto :EOF


Report •

#6
January 28, 2011 at 16:28:18
@ace, Awesome... now I need to tweak it..

Here is the end product (or the top part of it) of inside this 10.2.1.39.vnc file

host=10.2.1.39
[connection]
host=
port=5900
proxyhost=
proxyport=5900
password=
[options]

I need to get that "host=10.2.1.39" moved down to just under [connection].
I can remove the "host=" from my VNC.txt file but that [connection] part needs to be at the top of the file.

not sure how to write in back a "enter" in a text file (to move down to the next line).


Report •

#7
January 28, 2011 at 16:30:52
Modify your VNC.TXT file and remove the first two lines. Just open it in notepad and make the changes.


Report •

#8
January 28, 2011 at 16:40:08
Then use this...

@echo off
@for /f %%a in (YardCPlist.txt) do call :work %%a
goto :EOF

:work
echo [Connection] > %1.vnc
echo host=%1 >> %1.vnc 
type vnc.txt >> %1.vnc 
goto :EOF


Remember ">" means new file and ">>" means append to an existing file.


Report •

#9
January 28, 2011 at 16:41:10
@eco,

You are the man! (or woman), that worked perfectly.. I didn't know that doing a

echo some text here > %1.vnc
echo next line of text here >> %1.vnc

would create an "enter", lol never thought of it that way!!

thanks again for your help....


Report •


Ask Question