Solved

Messed up my code

Posted on 2016-10-26
6
53 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 54

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 54

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
Technology Partners: 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 54

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

749 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