Solved

Messed up my code

Posted on 2016-10-26
6
60 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 55

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 55

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 55

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

717 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