theperfectcucumber
theperfectcucumber

Reputation: 43

copy file error - object required: quotes

I am trying to create a script that would copy a file from a USB onto my computer. It would search for a USB, and use that as the drive letter. I got some third-party software that would allow me to eject the USB once it was done. I think that everything was fine except for the copy line. I have looked at numerous StackOverflow posts, but none seemed to have the answer.

The error was:

Object Required: "

My code:

Option Explicit
Dim objFSO, objDrive, scriptBaseName
Dim WshShell, strUserName, fso, x, v, y
Set objFSO     = CreateObject("Scripting.FileSystemObject")
scriptBaseName = objFSO.GetBaseName(Wscript.ScriptFullName)
For Each objDrive In objFSO.Drives
  If objDrive.DriveType = 1 And objDrive.IsReady Then
    MsgBox objDrive.DriveLetter & ":\ = " & objDrive.VolumeName, vbInformation, scriptBaseName
  End If
Next

Set WshShell = CreateObject("WScript.Shell")
strUserName = WshShell.ExpandEnvironmentStrings("%USERNAME%")

Set fso = CreateObject("Scripting.FileSystemObject")

x = objDrive.DriveLetter & ":\Inspirational\Inspirational_Quotes.VBS"
v = "C:\Users\" & strUserName & "\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\"

'Below was the problem
fso.CopyFile x, v, True

I tried this:

y = "Inspirational\Inspirational_Quotes.VBS"
WScript.Echo objDrive.DriveLetter & ":\" & y

I am not sure what I have to do to fix the error though.

Then, I just had to remove the USB

'Remove the USB'(s)
WshShell.Run "D:\Inspirational\removedrive\x64\RemoveDrive_D"
WshShell.Run "D:\Inspirational\removedrive\x64\RemoveDrive_E"

I experimented with this:

fso.CopyFile """" & x & """", """" & v & """", True

But it still didn't work.

Upvotes: 1

Views: 1637

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200273

objDrive is your loop variable. It doesn't retain a value/object after the loop finishes. You need to assign the value or object to another variable to make it accessible outside the loop.

Set fso = CreateObject("Scripting.FileSystemObject")

For Each objDrive In fso.Drives
  If objDrive.DriveType = 1 And objDrive.IsReady Then
    path = objDrive.RootFolder.Path
  End If
Next

x = fso.BuildPath(path, "Inspirational\Inspirational_Quotes.VBS")

Quotes don't have anything to do with the problem. FileSystemObject methods can handle paths with spaces just fine without additional quotes.

You don't need to create multiple FileSystemObject objects, BTW. Just create the object once at the beginning of your script.

Upvotes: 1

Related Questions