Solved

Messed up my code

Posted on 2016-10-26
6
42 Views
Last Modified: 2016-10-26
Ok, so this is what the code is supposed to do:
START
CHECK IF ITS LOCATION IS IN STARTUP
IF IT ISN'T IN STARTUP,
    FIND ITS OWN LOCATION
    COPY ITSELF TO STARTUP
    SAY "Copied to startup"
ELSE,
    SAY"Already copied".
'CONTINUE WITH CODE

I have been asking a few questions but as I edited to code, I now can't figure out if it if it will work or not:

(Remember, It needs to work from anyones computer. For Example someone downloads it, They open it from downloads and it copies itself to startup then starts the next piece of code. But if they download it and move it to the desktop, it will still know where it is and copy itself to startup)

Option Explicit

Dim objShell, strCmd, strTargetFolder, objFSO, strFile, strDirectory

Set objFSO = CreateObject("Scripting.FileSystemObject")

'Copy to startup section =====
strDirectory = "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup"

strFile = strDirectory & "\wippre-alpha.vbs"

If Not objFSO.FileExists(strFile) Then

strTargetFolder = "C:\Users\Arion\Documents\Environment\Test"

Set objShell = CreateObject("WScript.Shell")

strCmd = "%comspec% /c copy """ & WScript.ScriptFullName & """ """ & strTargetFolder & """ /Y"
objShell.Run strCmd
End if

Open in new window


P.S: If possible, could you clean up the code, for example: A=C B=C, What is C. Turn that into B=C, What is C. I think that is what has happened here:
strTargetFolder = "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup"
strDirectory = "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup"

Open in new window


Once again, thanks.
0
Comment
Question by:Arion Sejdia
  • 3
  • 3
6 Comments
 
LVL 53

Expert Comment

by:Bill Prew
ID: 41860356
Should be a simple as this:

Option Explicit

Dim objShell, objFSO
Dim strSourceFile, strTargetFile

Set objShell = WScript.CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

strSourceFile = WScript.ScriptFullName
strTargetFile = objShell.SpecialFolders("Startup") & "\" & WScript.ScriptName

If LCase(strSourceFile) <> LCase(strTargetFile) Then
   objFSO.CopyFile strSourceFile, strTargetFile, True
   Wscript.Echo "Copied to Startup folder."
   Wscript.Quit
End If

Wscript.Echo "Already copied."

' REST OF YOUR CODE HERE

Open in new window

~bp
1
 

Author Comment

by:Arion Sejdia
ID: 41860363
Should be a simple as this:

Option Explicit

Dim objShell, objFSO
Dim strSourceFile, strTargetFile

Set objShell = WScript.CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

strSourceFile = WScript.ScriptFullName
strTargetFile = objShell.SpecialFolders("Startup") & "\" & WScript.ScriptName

If LCase(strSourceFile) = LCase(strTargetFile) Then
   Wscript.Echo "Already copied."
Else
   objFSO.CopyFile strSourceFile, strTargetFile, True
   Wscript.Echo "Copied to Startup folder."
   Wscript.Quit
End If

' REST OF YOUR CODE HERE

So that will work from anywhere on any windows computer right?
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 41860368
Right, notice that it references WScript.ScriptFullName to determine the currently executing script file path.  This will always contain the full path with filename of the script that is executing.

I also updated the code just a smidge after posting to streamline the IF ... ELSE ... END IF.

~bp
1
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:Arion Sejdia
ID: 41860380
Thanks! One last thing and I will leave you alone, seeing you have answered every question about vbs i have ever asked  on this website, If I just removed
Wscript.Echo "Copied to Startup folder."
and
Wscript.Echo "Already copied."

Open in new window

From the code, will it still work the same?
0
 
LVL 53

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 41860396
Yes, those are just informational displays, the file copying will work fine.  And you are right, the code is better without those, good for testing, but not really useful when you deploy this.

~bp
1
 

Author Closing Comment

by:Arion Sejdia
ID: 41860399
Best person ever!
0

Featured Post

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question