# VBS Script



## Carlos_Mendoza (Nov 12, 2011)

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 (Apr 11, 2009)

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 (Nov 12, 2011)

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????

```
Shell "notepad.exe c:\foldername\filename.txt", vbMaximizedFocus
```


----------



## Ent (Apr 11, 2009)

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 (Nov 12, 2011)

What is the copy program?


----------



## Ent (Apr 11, 2009)

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.

```
set WSshell = createobject("wscript.shell")
WSshell.run("[B]copy c:\foldername\filename.txt c:\folder2name\file2name.txt[/B]",1)
```


----------



## Carlos_Mendoza (Nov 12, 2011)

And that will copy what is inside of filename1 and paste into filename2?


----------



## Ent (Apr 11, 2009)

Exactly.


----------



## Carlos_Mendoza (Nov 12, 2011)

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:

```
Shell "notepad.exe c:\foldername\filename.txt", vbMaximizedFocus
```


----------



## Ent (Apr 11, 2009)

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:


```
set WSshell = createobject("wscript.shell")
WSshell.run("notepad.exe c:\foldername\filename.txt", 1)
```


----------



## Carlos_Mendoza (Nov 12, 2011)

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 


```
set WSshell = createobject("wscript.shell")
WSshell.run("copy c:\foldername\filename.txt c:\folder2name\file2name.txt",1)
```


----------



## Carlos_Mendoza (Nov 12, 2011)

On the code above, if I save as a vbs file, I get an error of can not use parenthesis on a sub?


----------



## Ent (Apr 11, 2009)

Ah, I made a bit of a blunder there didn't I? 

This would work.

```
set WSshell = createobject("wscript.shell")
WSshell.run "cmd /c copy File1.txt File2.txt",0
```


----------



## Carlos_Mendoza (Nov 12, 2011)

That code does not error out, but it isn't copying from file1 to file2 either?


----------



## Ent (Apr 11, 2009)

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.


----------



## Carlos_Mendoza (Nov 12, 2011)

I am running this code:

```
set WSshell = createobject("wscript.shell")
WSshell.run "cmd /c copy C:\Test\Tips2.txt C:\Test\TIPS.txt",0
```
And it executes fine, but it isn't combined tips2 into tips like I was hoping for. I checked to see if it was by chance combining it into tips2 and that isn't happening either.


----------



## Ent (Apr 11, 2009)

Could you explain exactly what you were looking for? 

Do you want to develop this yourself, or would you prefer for me to write it for you?


----------



## Carlos_Mendoza (Nov 12, 2011)

If you could write it that would be tremendous!!

What I am wanting to do (on a much much much larger basis of course) is have a template (let's call it additions) and then I will have my normal documents. If an addition needs to be made, I want to be able to just add it into "additions" and then copy/paste from additions and cycle through each of my "normal" documents and paste it in. 

Basically it would save me a tremendous amount of time being able to do it this way as opposed to opening each document individually. Make sense?


----------



## Rollin_Again (Sep 4, 2003)

Are these true text files or another file format?

Rollin


----------



## Ent (Apr 11, 2009)

Sorry for the delay in responding. 
Please could you answer Rollin's question as to what kind of files these are. If they are text files I may be able to do it; if they're something like word documents it would take VBA programming which I can't do but someone around these parts probably could. 

Also, how does the contents of additions fit into the normal documents? Do they all go at the start, all go at the end, or are they spread out throughout the document?


----------



## Carlos_Mendoza (Nov 12, 2011)

No they are not text files. They are i believe they are called srt files (i am away from my computer right now). As far as tbe location....the end would be ideal.


----------



## Ent (Apr 11, 2009)

I have absolutely no idea whether my plan would work with .srt files.
I can have a go, but you'd need to send me a sample file of the attach and a document.


----------



## Carlos_Mendoza (Nov 12, 2011)

So how you would write the code depends on the type of file that it is?


----------



## Ent (Apr 11, 2009)

Yes, it would.


----------



## Rollin_Again (Sep 4, 2003)

So what type of data do these .srt files contain? What program creates them? Can these files be opened with Notepad if you right click and choose "open with" ? If so I believe you can still accomplish what you want.

Rollin


----------



## Carlos_Mendoza (Nov 12, 2011)

Rollin_Again said:


> What program creates them? Can these files be opened with Notepad if you right click and choose "open with" ?
> Rollin


The program that creates them is a program called Sales Response Team

If I try to open the file with notepad...it opens, but the data is very very jumbled.


----------

