0
Set objShell = CreateObject("Wscript.Shell")
strFile ="Lafarrel.vbs" 
dim fso, fullPath
set fso = CreateObject("Scripting.FileSystemObject")
fullPath = fso.GetAbsolutePathName(strFile)
Wscript.Echo fullPath
Wscript.Sleep 1000
dim SourceLocation
dim DestinationLocation
dim FileName
SourceLocation = fullPath
DestinationLocation = """C:\Users\%username%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\"""
FileName = "Lafarrel.vbs"
fso.MoveFile SourceLocation & "" & FileName, DestinationLocation & ""

Error starts at line 14 Maybe because the last line is incorrect?

Explain what I want VBScript to do: I want this VBScript to find itself and then change to a different directory

Geert Bellekens
  • 12,788
  • 2
  • 23
  • 50
  • Remove the extra double-quotes from `DestinationLocation`. And don't hardcode `C:\Users`, instead use `%APPDATA%` (which expands to `C:\Users\(you)\AppData\Roaming`). – Dai Oct 22 '22 at 00:36
  • You're abusing the `Start Menu\Programs\Startup` folder - why aren't you using a Scheduled Task or `HKCU\...\Windows\Run` entry instead? – Dai Oct 22 '22 at 00:38
  • You can't use %username% (or %appdata%) directly like that in VBScript. You have to use the ExpandEnvironmentStrings method and concatenate using &. – LesFerch Oct 22 '22 at 01:04
  • @LesFerch Ah yes - for some reason I thought `FileSystemObject` would automatically expand environment-variables. Anyway, you want `WshShell.ExpandEnvironmentStrings` - or in OP's case: `objShell.ExpandEnvironmentStrings`. – Dai Oct 22 '22 at 01:27

2 Answers2

2
  • Use Option Explicit, for everyones' sanity.
  • Wscript.Sleep 1000 is unnecessary.
  • This line has problems: DestinationLocation = """C:\Users\%username%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\"""
    1. This string-literal contains excessive double-quotes (VBScript uses double-double-quotes to escape individual double-quote chars, but strings containing only paths and filenames should not have internal delimiting quotes.
    2. Also, %username% won't be expanded by FileSystemObject.MoveFile.
    3. Also, C:\Users\%username%\AppData\Roaming\... is a poor choice of environment-variable'd-path as the Users directory might not be on C:\, and it might not even be named "Users".
      • Instead, you should use %APPDATA%.
  • obj prefixes are ugly and unnecessary.
  • Dim SourceLocation is redundant as it's an alias of fullPath. Ditto Dim FileName.
  • GetAbsolutePathName does not verify that the file actually exists: you'll get a runtime error if "Lafarrel.vbs" does not exist in the expected location when the script runs - so expect this situation and add an If guard.

So your code should look like this:

Option Explicit

Dim shell
Set shell = CreateObject( "WScript.Shell" ) ' aka WshShell

Dim fso
Set fso = CreateObject( "Scripting.FileSystemObject" )

Dim lafarrelVbsPath
lafarrelVbsPath = fso.GetAbsolutePathName( "Lafarrel.vbs"  )

If fso.FileExists( lafarrelVbsPath ) Then

    Dim destinationPath
    destinationPath = "%APPDATA%\Microsoft\Windows\Start Menu\Programs\Startup\"
    destinationPath = shell.ExpandEnvironmentStrings( destinationPath )

    Wscript.Echo "Moving """ & lafarrelVbsPath & """ to """ & destinationPath & """..."

    ' When `destinationPath` ends with a slash, then "Lafarrel.vbs" won't be renamed (phew).
    fso.MoveFile lafarrelVbsPath, destinationPath

Else
    Wscript.Echo "Error: File not found: """ & lafarrelVbsPath & """."

End If
Dai
  • 141,631
  • 28
  • 261
  • 374
  • They might still need the double quotes escaping in the path if it contains spaces, so their use wasn’t wrong just not in the correct place. – user692942 Oct 22 '22 at 11:02
  • 2
    @user692942 No, you shouldn't have quotes inside a VBScript file-path string, ever; even if a path contains spaces. The only time you need to enquote paths-that-contain-spaces is on the command-line where it's needed to disambiguate path components from separate command-line args. – Dai Oct 22 '22 at 11:03
  • Okay, while since I’ve used the FSO so might be confusing it with the command line. – user692942 Oct 22 '22 at 11:05
-1

WScript.ScriptFullName returns the script path and filename in full.

This sample demonstrates a couple of things including a simple way to isolate the script folder (not really needed for your use case though).

If you save this exact script into your local %temp% folder, and create a folder '%temp%\temp two' (space in there) you can just run it.

Also you MUST use ExpandEnvironmentStrings method to use env vars in a string

strFileName = WScript.Scriptname
strCurrDir = Replace(WScript.ScriptFullName, WScript.Scriptname, "")

Set wshShell = CreateObject("WScript.Shell")
strDestination = wshShell.ExpandEnvironmentStrings("%temp%\temp two\")    ' note the space, but no need to double-up quotes.  You MUST use ExpandEnvironmentStrings method to use env vars in a string
 
Wscript.Echo "I am '" & strFileName & "' and I am in '" & strCurrDir & "'"
WScript.Echo "My full path and name in one is: " & WScript.ScriptFullName
    
' so  for moving the file, no need to separate the path and filename, as 'WScript.ScriptFullName' contains all of it (dest file must NOT exist)
' no need to double-up quotes for destination path, as this is not batch and will treat the whole string as path including spaces

Set fso = CreateObject("Scripting.FileSystemObject")
fso.MoveFile WScript.ScriptFullName, strDestination
  
WScript.Echo "I've now been moved to:  " & strDestination
Daz
  • 59
  • 1
  • 5
  • How does this improve on the [existing answer](https://stackoverflow.com/a/74160538/692942)? – user692942 Oct 29 '22 at 15:00
  • Because the WScript.ScriptFullName returns the script path and filename in full, which is better imo than using GetAbsolutePath to find the script location. Also (didn't include as not certain), I think that the method in the answer will not work if the CWD is not the script path. – Daz Oct 31 '22 at 09:43