-1

Please I would like to know how I can copy files in windows using windows variable path with vbscript under elevated privillege. I have tried the codes below but it's not working.

dim filesys
set filesys=CreateObject("Scripting.FileSystemObject")
If filesys.FileExists("%AppData%\file.exe") Then
filesys.CopyFile "%AppData%\file.exe", "%AppData%\Microsoft\Windows\Start Menu\Programs\Startup\"
End If
Dim objFso, strSourcePath, strDestPath
strSourcePath = "%AppData%\file.exe"
strDestPath = "%AppData%\Microsoft\Windows\Start Menu\Programs\Startup\"
Set objFso = CreateObject("Scripting.FileSystemObject")
If objFso.FileExists(strSourcePath) then
    objFso.CopyFile strSourcePath, strDestPath, True
End If
Set objFso = Nothing
  • 1
    How do you know it doesn't work? –  Feb 09 '20 at 17:14
  • I check the file directory and I discovered the files weren't pesent after running the vbs script more so I use an ultrasearch software to search for the file by date, I didn't find it. – GitHub Frame Feb 10 '20 at 02:18

1 Answers1

-1

Try this:

dim filesys, WshShell
set filesys = CreateObject("Scripting.FileSystemObject")

Set WshShell = CreateObject("Wscript.Shell")
If filesys.FileExists(WshShell.ExpandEnvironmentStrings("%AppData%") & "\file.exe") Then
filesys.CopyFile WshShell.ExpandEnvironmentStrings("%AppData%") & "\file.exe", WshShell.ExpandEnvironmentStrings("%AppData%") & "\Microsoft\Windows\Start Menu\Programs\Startup\"
End If
Wasif
  • 14,755
  • 3
  • 14
  • 34
  • Thank you wasif, you code worked for me. What if I can to copy to drive and netwofk folder? What will the code look like? Can you please help me about this as well? – GitHub Frame Feb 10 '20 at 02:33
  • Just add a `, True` at the end of `filesys.copyfile` method and changes file paths to unc file path. Please accept the answer by clicking the big tick mark. Thank you for being helped by me! – Wasif Feb 10 '20 at 07:57
  • I am still confused, please an you provide me code on that can: Copy files ["*.exe","*.vbs"] and folders ["folder1", "folder2"] from any windows location to windows Variable path like; [%AppData%,%Tmp%], Drive path like ["c:\","d:\"] with hidden file attribute? – GitHub Frame Feb 13 '20 at 16:32
  • `filesys.copyfile "//server/share/source", "//server/share/destination", True` – Wasif Feb 13 '20 at 16:42
  • Did you mean I can make it this way for example to copy file: dim filesys, WshShell set filesys = CreateObject("Scripting.FileSystemObject") Set WshShell = CreateObject("Wscript.Shell") If filesys.FileExists(WshShell.ExpandEnvironmentStrings("%AppData%") & "\*.fileextensionA", "\*.fileextensionB","\*.fileextensionC") Then filesys.CopyFile WshShell.ExpandEnvironmentStrings("%AppData%") & "\*.fileextensionA", "\*.fileextensionB","\*.fileextensionC", WshShell.ExpandEnvironmentStrings("%AppData%") & "\Microsoft\Windows\Start Menu\Programs\Startup\" End If – GitHub Frame Feb 13 '20 at 17:22
  • You can't use multiple wildcards at a time. *Also this discussion is getting longer. As you are new here, you may not know questions cannot be extended after the original. Try asking a new question there more community members can help you.* – Wasif Feb 13 '20 at 17:26
  • And also I can make it this way for example to copy folder: dim filesys, WshShell set filesys = CreateObject("Scripting.FileSystemObject") Set WshShell = CreateObject("Wscript.Shell") If filesys.FileExists(WshShell.ExpandEnvironmentStrings("%AppData%") & "c:\source1","c:\source2") Then filesys.CopyFile WshShell.ExpandEnvironmentStrings("%AppData%") & "c:\source1","c:\source2", WshShell.ExpandEnvironmentStrings("%AppData%") & "\Microsoft\Windows\Start Menu\Programs\Startup\" End If – GitHub Frame Feb 13 '20 at 17:27
  • Yes that way works. – Wasif Feb 13 '20 at 17:28
  • Wasif, WHat if I would like to copy more than one fie type from more than one location? – GitHub Frame Feb 14 '20 at 12:24
  • I tried the code below but its not working: dim filesys, WshShell set filesys = CreateObject("Scripting.FileSystemObject") Set WshShell = CreateObject("Wscript.Shell") If filesys.FileExists(WshShell.ExpandEnvironmentStrings("%HomePath%") & "\Local\Temp\*.tmp") Then filesys.CopyFile WshShell.ExpandEnvironmentStrings("%HomePath%") & "\Local\Temp\*.tmp", WshShell.ExpandEnvironmentStrings("%AppData%") & "\Filebkp\" End If – GitHub Frame Feb 14 '20 at 12:38