1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

VBS Script

Discussion in 'Software Development' started by Carlos_Mendoza, Mar 8, 2012.

Thread Status:
Not open for further replies.
Advertisement
  1. Carlos_Mendoza

    Carlos_Mendoza Thread Starter

    Joined:
    Nov 12, 2011
    Messages:
    124
    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?
     
  2. Ent

    Ent Josiah Trusted Advisor

    Joined:
    Apr 11, 2009
    Messages:
    5,407
    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.
     
  3. Carlos_Mendoza

    Carlos_Mendoza Thread Starter

    Joined:
    Nov 12, 2011
    Messages:
    124
    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 
    
     
  4. Ent

    Ent Josiah Trusted Advisor

    Joined:
    Apr 11, 2009
    Messages:
    5,407
    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.
     
  5. Carlos_Mendoza

    Carlos_Mendoza Thread Starter

    Joined:
    Nov 12, 2011
    Messages:
    124
    What is the copy program?
     
  6. Ent

    Ent Josiah Trusted Advisor

    Joined:
    Apr 11, 2009
    Messages:
    5,407
    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("[B]copy c:\foldername\filename.txt c:\folder2name\file2name.txt[/B]",1)
     
  7. Carlos_Mendoza

    Carlos_Mendoza Thread Starter

    Joined:
    Nov 12, 2011
    Messages:
    124
    And that will copy what is inside of filename1 and paste into filename2?
     
  8. Ent

    Ent Josiah Trusted Advisor

    Joined:
    Apr 11, 2009
    Messages:
    5,407
    Exactly.
     
  9. Carlos_Mendoza

    Carlos_Mendoza Thread Starter

    Joined:
    Nov 12, 2011
    Messages:
    124
    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
    
     
  10. Ent

    Ent Josiah Trusted Advisor

    Joined:
    Apr 11, 2009
    Messages:
    5,407
    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)
    
     
  11. Carlos_Mendoza

    Carlos_Mendoza Thread Starter

    Joined:
    Nov 12, 2011
    Messages:
    124
    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)
    
     
  12. Carlos_Mendoza

    Carlos_Mendoza Thread Starter

    Joined:
    Nov 12, 2011
    Messages:
    124
    On the code above, if I save as a vbs file, I get an error of can not use parenthesis on a sub?
     
  13. Ent

    Ent Josiah Trusted Advisor

    Joined:
    Apr 11, 2009
    Messages:
    5,407
    Ah, I made a bit of a blunder there didn't I? :eek:

    This would work.
    Code:
    set WSshell = createobject("wscript.shell")
    WSshell.run "cmd /c copy File1.txt File2.txt",0
    
     
  14. Carlos_Mendoza

    Carlos_Mendoza Thread Starter

    Joined:
    Nov 12, 2011
    Messages:
    124
    That code does not error out, but it isn't copying from file1 to file2 either?
     
  15. Ent

    Ent Josiah Trusted Advisor

    Joined:
    Apr 11, 2009
    Messages:
    5,407
    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.
     
  16. Sponsor

As Seen On
As Seen On...

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.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1044332