Solved How to start VB Script & Create Move, Copy & Delete command

November 20, 2012 at 00:26:55
Specs: Windows XP

I'm new with this programming, & I need to know how to start the project for VB script & can you give me a command for Copy, Move & Delete in VB Script?

Then how to save to ".vbs"?

Actually I've run the VB 6.0 & try some samples from the web that I've found but it cause me error when try to run(compile).

"Invalid Outside Procedure"

Here is my only coding:

Option Explicit
Dim objFSO
Const OverwriteExisting = True

Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile "C:\SCRIPTING\input\*.txt", "C:\SCRIPTING\move\*.txt", OverwriteExisting

I've just create in new "Standard EXE" in VB & written these code.

Can someone help me please?


See More: How to start VB Script & Create Move, Copy & Delete command

Report •

November 20, 2012 at 16:30:13
✔ Best Answer
This code worked on my 32-bit system, both vbscript and vis.basic:
'===== begin vis basic:
Attribute VB_Name = "Module1"
Sub main()
const overwrite=True
Set fso = CreateObject("scripting.filesystemobject")
fso.copyfile "C:\SCRIPTING\input\*.txt", "C:\SCRIPTING\move", Overwrite
End Sub
'====== end vis basic
which compiled and ran ok. vbscript is same, but without the "Attribute" line, and the sub/end sub are optional, for this snippet anyway. The main thing I did was to remove the "*.txt" from your destination, which fouls things up.

Report •

November 20, 2012 at 17:20:26
Hi nbrane,

Thanks for your replied. And it was helps. Then 1 more thing, how to save to ".vbs" file when you developed using visual basic design?

After that, how to execute the ".vbs" file? Is it using a batch files or just double click that ".vbs" file


Report •

November 20, 2012 at 19:42:38
for vbs, it is simple text (ascii). Just use notepad or (i prefer) EDIT to create the script. (same as batch script.) Save it with .vbs extension. That is critical. To run, on older systems: cscript /nologo xxxx.vbs On newer systems, they will run straight from commandline: xxxx.vbs BUT, this method can cause failure, so if it fails, fall back to the cscript method.
If you want to compile it (vis.basic): vb32 xxxx.BAS
then do the "make exe" option from the alt-F menu. This will (should) make it into executable form. Like i said, my system had no errors, but 64-bit systems might.
vbscript is a "scripting" language, ie: not compiled, but interpreted 'on the fly'. So it runs "as is".
Vis.basic code, unless it is compiled, will only run inside the "developement" box
("vb32.exe" or something like), but once compiled, will run straight from commandline (on compatible platforms/op.sys). Hope this makes sense.

Report •

Related Solutions

November 20, 2012 at 20:05:38
Thanks mate.

Now it is working without error. The thing is, the file did not copied to the "Move" folder as per the coding. Here is my coding that follow your samples.

Sub main()
Const overwrite = True
Set fso = CreateObject("scripting.filesystemobject")
fso.CopyFile "C:\SCRIPTING\input\*.txt", "C:\SCRIPTING\move", overwrite
End Sub

I've just created using a notepad & save as ".vbs". Maybe got something that I missing?


Report •

November 20, 2012 at 20:12:04
I run using a SolFusion modules under Solimar Product which is used to watch a folder "C:\SCRIPTING\input\" and calling the ".vbs" script whenever see the files with extension ".txt".

So it was no error but it didn't copy the file to folder "C:\SCRIPTING\move".


Report •

November 20, 2012 at 20:41:09
First, i guess, how does it call the vbs script?
1) cscript xxx.vbs
2) cscript xxx (implicit extension, avoid that, be explicit.)
3) xxx.vbs
4) xxx

All of these can work, but you need to see what rendition is being invoked by your main process.

But, since you got no error,
is the directory already established? if not, create it.

try the scipt outside the Sol, running independantly, with a test set of files, and examine all output. Debugging 101 ;-)

Report •

November 20, 2012 at 20:51:18
Actually the SolFusion modules calling the vbs script means execute the vbs script. My vbs script name is "exercise1.vbs", so the Sol modules call that file & execute it. Same like you call a batch file & execute it.

Means Double click the vbs file, I guess.

The folders directory are ready.

Can you explained how to try the script running independantly? How to execute the vbs script file?


Report •

November 20, 2012 at 21:08:32
Well, go to command-prompt. Either CD or use path to run the .vbs script where it is located. (f/e): CD \xx\yy\myvbs
cscript excercise1.vbs
cscript c:\xx\yy\myvbs\excercise1.vbs

either way works. CSCRIPT.EXE interprets and runs the script.

Like I said, my tests had no problems on this end: win-xp 32-bit.

Oh, and you did honor the script with a name? I noticed you might have called it ".vbs", but you must honor it with a name: xxx.vbs, yyy.vbs, anything.vbs

Report •

November 20, 2012 at 22:19:20
I run the command promt & type this:

C:\>cd c:\scripting\

C:\SCRIPTING>cscript exercise1.vbs

and then this promt:

Microsoft <R> Windows Script Host Version 5.7
Copyright <C> Microsoft Corporation. All rights reserved.


Looks like no error but the files is still not copied to the folder "move".

Actually my PC also Windows XP 32bit.


Report •

November 21, 2012 at 07:45:58
fso.copyfile "C:\SCRIPTING\input\*.txt", "C:\SCRIPTING\move\", Overwrite

Suggested reading: MSDN

How To Ask Questions The Smart Way

Report •

November 21, 2012 at 18:17:38
Thank you all for your helps. Now it was working already. Here I share the coding that I used:

Dim args, fso
Set args = wscript.Arguments
Set fso = CreateObject("scripting.filesystemobject")

Dim StateServer
Const overwrite = True

Set StateServer = CreateObject("StateServerClient.StateServer")
StateServer.Connect "http://localhost:8001/Solfusion/StateServer", args(0)
wscript.Echo args(0)

fso.CopyFile args(0),"C:\SCRIPTING\move\", overwrite

These coding applied for all ".pdf" extension copies that already setup in the SolFusion Module. The arguments must set in the SolFusion Module as "InputFile[0]"

I have 1 more question. How about if I have to filter certain files only to copy out?

eg: I have 3 files that named:
1) Motor.pdf
2) Car.pdf
3) House.pdf

I want to copy the files that contain "Motor" OR "Car" in the filename to the folder "move" while "House" copy to folder "transfer".

Thats mean I gonna use IF command right? Anybody please.


Report •

November 21, 2012 at 19:02:27

if lcase(args(0))="house.pdf" then dest="transfer\" else dest="move\"
fso.CopyFile args(0),"C:\SCRIPTING\"+dest, overwrite

If you need more extensive filtering, then an associative array or a dictionary might be called for.

Report •

November 21, 2012 at 20:21:40
Actually it didnt working because the "args(0)" contains including the path as well. So what I do, I use GetFileName(args(0)) to get the file name & extension. However if the Input files have a long name but contains the wordings house/motor/car? May I use a wild card?

eg: If lcase(args(0)) = "*house*.pdf" Then


Report •

November 21, 2012 at 21:35:59
Sorry, I should have seen that. Here is VERY simple fix:
if left(lcase(args(0)),5)="house" then dest="transfer\" else dest="move\"

or, if it can be anywhere in the name:
if instr(lcase(args(0)),"house")>0 then dest="transfer\" else dest="move\"

Report •

November 21, 2012 at 22:17:50
Thanks mate, now it is working wonderful..


Report •

Ask Question