Solved

Messed up my code

Posted on 2016-10-26
6
47 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
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.

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying 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

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…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

860 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