Advertisement

There's no such thing as a stupid question, but they're the easiest to answer.
Login
Search

Advertisement

Software Development Software Development
Search Search
Search for:
Tech Support Guy > > >

VBS Script


(!)

Carlos_Mendoza's Avatar
Carlos_Mendoza Carlos_Mendoza is offline
Member with 115 posts.
THREAD STARTER
 
Join Date: Nov 2011
Experience: Larning VBA from Forum's
08-Mar-2012, 08:52 AM #1
VBS Script
I am very new to VBS and would like to be able to have some vbs code that will open a file, copy everything in that file, then open file2 and paste the copied data into it, then save file # 2.

Is this an easy feat?
Ent's Avatar
Ent   (Josiah) Ent is offline Ent is a Trusted Advisor with special permissions.
Computer Specs
Trusted Advisor with 5,234 posts.
 
Join Date: Apr 2009
Location: United Kingdom
Experience: Intermediate
09-Mar-2012, 06:29 AM #2
Yes, I would probably say it's a fairly easy task, although it does depend to some extent on what files you're talking about though. It's very much easier for text files than other kinds of file. If you're looking at text files, I suggest you begin your search with the filesystemobject.

Feel free to ask particular questions if you get stuck.
Carlos_Mendoza's Avatar
Carlos_Mendoza Carlos_Mendoza is offline
Member with 115 posts.
THREAD STARTER
 
Join Date: Nov 2011
Experience: Larning VBA from Forum's
09-Mar-2012, 10:23 AM #3
So how the code is written depends on the type of file that it is? I tried using the syntax of this code (of course modifying it for my needs) and when I run the code it errors out on Shell????
Code:
Shell "notepad.exe c:\foldername\filename.txt", vbMaximizedFocus
Ent's Avatar
Ent   (Josiah) Ent is offline Ent is a Trusted Advisor with special permissions.
Computer Specs
Trusted Advisor with 5,234 posts.
 
Join Date: Apr 2009
Location: United Kingdom
Experience: Intermediate
09-Mar-2012, 10:35 AM #4
The filesystemobject I referred to only really works with text files, which is why I said what I did.

If you want you script to call other programs to do what you want, it would be substantially easier to just use the copy program to get the contents of one file into another.
Carlos_Mendoza's Avatar
Carlos_Mendoza Carlos_Mendoza is offline
Member with 115 posts.
THREAD STARTER
 
Join Date: Nov 2011
Experience: Larning VBA from Forum's
09-Mar-2012, 10:47 AM #5
What is the copy program?
Ent's Avatar
Ent   (Josiah) Ent is offline Ent is a Trusted Advisor with special permissions.
Computer Specs
Trusted Advisor with 5,234 posts.
 
Join Date: Apr 2009
Location: United Kingdom
Experience: Intermediate
09-Mar-2012, 10:52 AM #6
It's what you would use in the command line or a batch script to copy files.

The syntax is
Copy Source Destination.

Since you want it in a VBS file, you'd need to include a shell.
Code:
set WSshell = createobject("wscript.shell")
WSshell.run("copy c:\foldername\filename.txt c:\folder2name\file2name.txt",1)
Carlos_Mendoza's Avatar
Carlos_Mendoza Carlos_Mendoza is offline
Member with 115 posts.
THREAD STARTER
 
Join Date: Nov 2011
Experience: Larning VBA from Forum's
09-Mar-2012, 11:04 AM #7
And that will copy what is inside of filename1 and paste into filename2?
Ent's Avatar
Ent   (Josiah) Ent is offline Ent is a Trusted Advisor with special permissions.
Computer Specs
Trusted Advisor with 5,234 posts.
 
Join Date: Apr 2009
Location: United Kingdom
Experience: Intermediate
09-Mar-2012, 11:06 AM #8
Exactly.
Carlos_Mendoza's Avatar
Carlos_Mendoza Carlos_Mendoza is offline
Member with 115 posts.
THREAD STARTER
 
Join Date: Nov 2011
Experience: Larning VBA from Forum's
09-Mar-2012, 11:11 AM #9
When I try to use Shell...I get this error:
Windows Script Host
Script --- shows location of the script
Line 1
Char 1
Error Type Mismatch: 'Shell'
Code 800A000D
Source Microsoft VBScript Runtime Error

And I am running the above code:
Code:
Shell "notepad.exe c:\foldername\filename.txt", vbMaximizedFocus
Ent's Avatar
Ent   (Josiah) Ent is offline Ent is a Trusted Advisor with special permissions.
Computer Specs
Trusted Advisor with 5,234 posts.
 
Join Date: Apr 2009
Location: United Kingdom
Experience: Intermediate
09-Mar-2012, 11:17 AM #10
That's because it's the wrong language. That syntax for shell is used in VBA (Visual Basic for Applications, the language you write macros in in Microsoft Office) and not vbs (Visual Basic Script)

If you wanted to use the same command in vbs it would be as follows:

Code:
set WSshell = createobject("wscript.shell")
WSshell.run("notepad.exe c:\foldername\filename.txt", 1)
Carlos_Mendoza's Avatar
Carlos_Mendoza Carlos_Mendoza is offline
Member with 115 posts.
THREAD STARTER
 
Join Date: Nov 2011
Experience: Larning VBA from Forum's
09-Mar-2012, 11:24 AM #11
Oh good gravy...so knowing that, you gave me the code a few posts up then. Really all I would have to do is run this code then. I'll test

Code:
set WSshell = createobject("wscript.shell")
WSshell.run("copy c:\foldername\filename.txt c:\folder2name\file2name.txt",1)
Carlos_Mendoza's Avatar
Carlos_Mendoza Carlos_Mendoza is offline
Member with 115 posts.
THREAD STARTER
 
Join Date: Nov 2011
Experience: Larning VBA from Forum's
09-Mar-2012, 11:27 AM #12
On the code above, if I save as a vbs file, I get an error of can not use parenthesis on a sub?
Ent's Avatar
Ent   (Josiah) Ent is offline Ent is a Trusted Advisor with special permissions.
Computer Specs
Trusted Advisor with 5,234 posts.
 
Join Date: Apr 2009
Location: United Kingdom
Experience: Intermediate
09-Mar-2012, 11:45 AM #13
Ah, I made a bit of a blunder there didn't I?

This would work.
Code:
set WSshell = createobject("wscript.shell")
WSshell.run "cmd /c copy File1.txt File2.txt",0
Carlos_Mendoza's Avatar
Carlos_Mendoza Carlos_Mendoza is offline
Member with 115 posts.
THREAD STARTER
 
Join Date: Nov 2011
Experience: Larning VBA from Forum's
09-Mar-2012, 11:53 AM #14
That code does not error out, but it isn't copying from file1 to file2 either?
Ent's Avatar
Ent   (Josiah) Ent is offline Ent is a Trusted Advisor with special permissions.
Computer Specs
Trusted Advisor with 5,234 posts.
 
Join Date: Apr 2009
Location: United Kingdom
Experience: Intermediate
09-Mar-2012, 12:12 PM #15
Very odd. It works for me.
Are you sure that you're getting the file extensions right? For example if File1 is a word document, you'd need file1.doc instead of file1.txt.

Last edited by Ent; 09-Mar-2012 at 12:18 PM..
As Seen On

BBC, Reader's Digest, PC Magazine, Today Show, Money Magazine
WELCOME TO TECH SUPPORT GUY!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.


(clock)
THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


WELCOME
You Are Using: Server ID
Trusted Website Back to the Top ↑