Create file and copy from VB script

Microsoft Windows xp professional w/serv...
March 19, 2010 at 04:50:38
Specs: Windows XP
I have a VB script and want it to create a new .vbs/bat file and copy sections of the original script into it.


If value = something1 Then
'        create file and insert: 
   Sh.Run("%systemdrive%\Software\McAfee\EPO\FramePkg.exe"), 0, True

If value = something2 Then
'        create file and insert: 
   Sh.Run("%systemdrive%\Software\McAfee\Standalone\Install.bat"), 0, True

I need to do this as my 1st script does computer rename and then installs software but some software will register itself using computer name so want it done after computer reboots and script 2 is run.

Any help is much appreciated!

See More: Create file and copy from VB script

Report •

March 19, 2010 at 10:04:53
here's a snippet to start with:
(i used a bat file for output for greater simplicity)

dim fso
dim a(2)
set fso=createobject("scripting.filesystemobject")
set bat=fso.opentextfile("autorun.bat",2)
bat.writeline x1&a(n)

Report •

March 19, 2010 at 14:46:14
Sorry I suck at scripting but its not working for me. It's saying file doesn't exist.

For line: set bat=fso.opentextfile("autorun.bat",2). Does autorun.bat need to be precreated?

If possible can you please explain in 2 parts.
1) Create .bat from .vbs
2) Append to .bat from .vbs


Report •

March 19, 2010 at 15:25:01
hell, you're absolutely right! I forgot, vbscript wants the file to pre-exist prior to open, even when opened for output.
set bat=createtextfile("autorun.bat")
bat.writeline "@echo off"

1) Create .bat from .vbs:
we pretty much did just that, so i guess that one's covered.
2) Append to .bat from .vbs
textfilemodes used by filesystemobject:
const forreading=1
const forwriting=2
const forappending=8
example append:
set bat=opentextfile("existing.bat",8)
bat.writeline "appended line here"

(don't ask me what modes 3-7 are, i've no clue! my scripting abilities are also sucky, we're in the same boat.)
for those of us who are "challenged", msdn has pretty good site:
a large library of script to use as examples, as well as how-tos.

Report •

Related Solutions

March 19, 2010 at 16:10:01
Getting error type mismatch 'createtextfile'.

Is this working for you?

Report •

March 19, 2010 at 17:31:18
way sorry man! i didn't get enought coffee today, really sorry.
i forgot to put fso.createtextfile:
set bat=fso.createtextfile("autorun.bat")
(where "fso" can be any name for:
set fso=createobject("scripting.filesystemobject")

(I tend to prefer shorter var.names that i can pronounce)

Report •

March 19, 2010 at 17:43:30
Thanks alot.. I've got the script doing everything I want to now :)

Is it possible to do a Write from a range of lines?

bat.WriteLine 10-15


Thanks again!

Report •

March 19, 2010 at 19:10:10
Not clear what you mean... you can't do it in one statement, but you can of course do it line by line. this won't work:
bat.writeline 10-20

but you can of course write lines 10-20 with whatever.
you can cheat and make a lot of lines with crlfs:
bat.writeline xx

will effectively write 4 lines of output to the file.

sorry about my messups, i don't work with vbscript enough.
I'm trying to spend a lot more time with visbasic & vbscript,
but now that weather is getting nice, i'll probably be slacking!

Report •

March 20, 2010 at 10:23:21
haha yep the weather is finally getting warmer, still rainy and miserable tho.

Using your above code, how do I insert the code I want written into each line?

Basically I have 40+ lines that I want written and was hoping instead of specifying each line individually there was a grouping/range where you could say writelines 10-20 ect. Apparently other languages can do this. If its not possible in VB no big deal as it at least works :)


Report •

March 20, 2010 at 14:25:54
I haven't exactly seen an example of multi-line writing like you're describing - can you give and example? (any language), i'm curious to see what it looks like.
as for writing lots of data, that depends alot on the situation.
is it repetetive data, or random/dynamic? where does the data originate? there's ways to automate almost any task like this, but not ONE way to automate all of the them in one piece of code. f/e, writing static data:
for i=10 to 40
ofile.writeline "line"&i
or, here you get data from file "ifile":
for i=10 to 40
ifile.readline xx
ofile.writeline xx

Report •

Ask Question