Solved

Messed up my code

Posted on 2016-10-26
6
19 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 51

Expert Comment

by:Bill Prew
Comment Utility
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
Comment Utility
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 51

Expert Comment

by:Bill Prew
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:Arion Sejdia
Comment Utility
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 51

Accepted Solution

by:
Bill Prew earned 500 total points
Comment Utility
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
Comment Utility
Best person ever!
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now