/build/static/layout/Breadcrumb_cap_w.png

Easy one I think! - Help with VBScript

Hi,

Can anybody give me a hand?

I need to write a VBSCript that moves a file from the INSTALLDIR to the users mydocuments folder that resides under their homedrive.

Basically the package I've create installs a config file to C:\Program Files\PGDS-US\PGDS Broadcast Ticker which is the INSTALLDIR. Within this directory is a file entitled "PGDS-NET-CLIENT.cfg".

I want my VBScript to move this file from the INSTALLDIR location to the mydocuments folder which is re-directed to; O:\Data\My Documents.

Can anybody give me a hand with creating a script? I've already created the sample one below but it doesn't appear to work;

Option Explicit
Dim oFSO, oShell, sDirectory
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oShell = CreateObject("WScript.Shell")
sDirectory = oShell.SpecialFolders("MyDocuments")
If not oFSO.FileExists("C:\Program Files\PGDS-US\PGDS Broadcaster\PGDS-NET-CLIENT.cfg") Then
oFSO.MoveFile "C:\Program Files\PGDS-US\PGDS Broadcaster\PGDS-NET-CLIENT.cfg", sDirectory, false
End If

Thanks in advance,
Cowley

0 Comments   [ + ] Show comments

Answers (16)

Posted by: pjgeutjens 14 years ago
Red Belt
0
If not oFSO.FileExists("C:\Program Files\PGDS-US\PGDS Broadcaster\PGDS-NET-CLIENT.cfg") Then

I think what you want is

If not oFSO.FileExists(sDirectory & "\PGDS-NET-CLIENT.cfg") Then

or something along those lines

PJ
Posted by: Jsaylor 14 years ago
Second Degree Blue Belt
0
Your first issue is that the "oFSO.MoveFile" will never execute, because you're telling your script to only execute that line if the file in your installdir is not found. Remove the "Not" as a first step, or change the file it's looking for to the my documents path.
Posted by: MSIPackager 14 years ago
3rd Degree Black Belt
0
Your first issue is that the "oFSO.MoveFile" will never execute, because you're telling your script to only execute that line if the file in your installdir is not found. Remove the "Not" as a first step, or change the file it's looking for to the my documents path.

Oh yeah lol well spotted [:D]
Posted by: cowley 14 years ago
Orange Belt
0
Thanks for the input, I've gone a little bit further with this and made life easy for myself, whilst it works, I want to find a mechanism for it to run more than once without error.

The below script works fine If I'm running it for the first time under an account where the file doesn't exist in the destination location, my problem occurs when I re-run the script, basically because the file already exists in the destination location it errors. What logic would I need to use to prevent it from displaying an error when I re-run the script?

I'm guessing it's an If filesys.FileExists("O:\Data\My Documents\PGDS-NET-CLIENT.cfg") then do nothing type statement?

dim filesys
set filesys=CreateObject("Scripting.FileSystemObject")
If filesys.FileExists("C:\Program Files\PGDS-US\PGDS Broadcast Ticker\PGDS-NET-CLIENT.cfg") Then
filesys.MoveFile "C:\Program Files\PGDS-US\PGDS Broadcast Ticker\PGDS-NET-CLIENT.cfg", "O:\Data\My Documents\"
End If
Posted by: Jsaylor 14 years ago
Second Degree Blue Belt
0
You can use AND statements nested inside of IF's. So try:

if filesys.fileexists("SOURCEFILE") AND NOT filesys.fileexists("Destination") Then.....
Posted by: cowley 14 years ago
Orange Belt
0
Thanks for your help on this Jsaylor, can I just clarify what you mean because I've tried what I think you mean and my script editor is unable to resolve it. Sorry for so many questions.

Here's my new script...the one that doesn't appear to work;

dim filesys
set filesys=CreateObject("Scripting.FileSystemObject")
if filesys.fileexists("C:\Program Files\PGDS-US\PGDS Broadcast Ticker\PGDS-NET-CLIENT.cfg") AND NOT filesys.fileexists ("O:\Data\My Documents\PGDS-NET-CLIENT.cfg")
Then
filesys.MoveFile "C:\Program Files\PGDS-US\PGDS Broadcast Ticker\PGDS-NET-CLIENT.cfg", "O:\Data\My Documents\"
End If
Posted by: pjgeutjens 14 years ago
Red Belt
0
Cowley,

perhaps, instead of using the MoveFile function, in which you cannot supply an overwrite parameter, you could use a combination of CopyFile (where you CAN specify Overwrite behaviour) and a DeleteFile

PJ
Posted by: cowley 14 years ago
Orange Belt
0
ORIGINAL: pjgeutjens

Cowley,

perhaps, instead of using the MoveFile function, in which you cannot supply an overwrite parameter, you could use a combination of CopyFile (where you CAN specify Overwrite behaviour) and a DeleteFile

PJ


Cheers for the heads up, I tried this method but my script editor doesn't like it :(

dim filesys
set filesys=CreateObject("Scripting.FileSystemObject")
if filesys.FileExists("C:\Program Files\PGDS-US\PGDS Broadcast Ticker\PGDS-NET-CLIENT.cfg") then
if not filesys.FileExists("O:\Data\My Documents\PGDS-NET-CLIENT.cfg") Then filesys.CopyFile "C:\Program Files\PGDS-US\PGDS Broadcast Ticker\PGDS-NET-CLIENT.cfg", "O:\Data\My Documents\PGDS-NET-CLIENT.cfg"
filesys.Deletefile "C:\Program Files\PGDS-US\PGDS Broadcast Ticker\PGDS-NET-CLIENT.cfg"
End if
Posted by: Jsaylor 14 years ago
Second Degree Blue Belt
0
ORIGINAL: cowley

Thanks for your help on this Jsaylor, can I just clarify what you mean because I've tried what I think you mean and my script editor is unable to resolve it.  Sorry for so many questions.

Here's my new script...the one that doesn't appear to work;

dim filesys
set filesys=CreateObject("Scripting.FileSystemObject")
if filesys.fileexists("C:\Program Files\PGDS-US\PGDS Broadcast Ticker\PGDS-NET-CLIENT.cfg") AND NOT filesys.fileexists ("O:\Data\My Documents\PGDS-NET-CLIENT.cfg")
Then
filesys.MoveFile "C:\Program Files\PGDS-US\PGDS Broadcast Ticker\PGDS-NET-CLIENT.cfg", "O:\Data\My Documents\"
End If


Are you by chance getting the "expected 'Then'" error when you run that script? Your "Then" appears to be on a separate line from your "if," which will confuse and explode a vbs.
Posted by: pjgeutjens 14 years ago
Red Belt
0
filesys.CopyFile "C:\Program Files\PGDS-US\PGDS Broadcast Ticker\PGDS-NET-CLIENT.cfg", "O:\Data\My Documents\PGDS-NET-CLIENT.cfg"

You'll want to specify the overwrite parameter,

CopyFile Source, Destination, False <- - the overwrite parameter

PJ
Posted by: anonymous_9363 14 years ago
Red Belt
0
Gents, was there some reason why you were all ignoring the MoveFile table? If persisting with script, PLEASE, PLEASE remove the continuous use of the same text and use variables! If the source/destination ever moves, it'll be somewhat easier to change the data once, rather than at every occurence.
Posted by: Jsaylor 14 years ago
Second Degree Blue Belt
0
Baby steps vbscab, baby steps.
Posted by: jmcfadyen 14 years ago
5th Degree Black Belt
0
probably better to use the current user healing techniques here as movefile table may only work for initial user and not successive users.

I do agree throwing away the vb script is the way forward.
Posted by: dnmech 14 years ago
Senior Purple Belt
0
What logic would I need to use to prevent it from displaying an error when I re-run the script?


cowley

u can use "On Error Resume Next" in ur script.

see if it helps.
Posted by: pjgeutjens 14 years ago
Red Belt
0
Gents, was there some reason why you were all ignoring the MoveFile table?

In my case it was lack of coffee, you are right ofcourse.
Unless the script is meant for some sort of Active Setup operation, to be run for each user.

PJ
Posted by: anonymous_9363 14 years ago
Red Belt
0
u can use "On Error Resume Next" in ur script. That step is only worthwhile if errors are subsequently trapped. As usual, the script is barren of a single line of error-trapping.

Also, I believe it may have been mentioned before that English may not necessarily be forum users' first language so please avoid the abomination of text-speak in posts. Thanks.

Lastly, @OP, there is a dedicated Scripting forum on AppDeploy.
Rating comments in this legacy AppDeploy message board thread won't reorder them,
so that the conversation will remain readable.
 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ