Copy file from script directory to All Users' desktop - vbscript

I've written the script below but when I run it (using PrimalScript for troubleshooting) I get the error 'Permission denied'.
I'm admin on this device and I get the same error when I run the script elevated.
Here is the script:
Dim WshShell, strCurDir, File, strDesktop
Set WshShell = CreateObject("WScript.Shell")
strDesktop = WshShell.SpecialFolders("AllUsersDesktop")
Set ofso = CreateObject("Scripting.FileSystemObject")
strPath = ofso.GetParentFolderName(WScript.ScriptFullName)
File = "pwsafe.psafe3"
strCurDir = ofso.BuildPath(strPath, File)
ofso.CopyFile strCurDir , strDesktop , OverwriteExisting
What am I doing wrong?

You must set a process:
Dim ofso, oshell, oproc, strDesktop, overwrite
Set ofso = CreateObject("Scripting.FileSystemObject")
Set oshell = CreateObject("WScript.Shell")
Set oproc = oshell.Environment("Process")
strDesktop = oproc.Item("UserProfile") & "\Desktop\"
overwrite = True
ofso.CopyFile "pwsafe.psafe3" , strDesktop , overwrite

while my last comment showed the script that doesn't work, the script below is combination of Sergio's and Lankymart's work. Wanted to mention them both since they deserve the credit. Here is the working script and I hope someone else makes use of it.
Dim ofso, oshell, oproc, strDesktop, overwrite
Set ofso = CreateObject("Scripting.FileSystemObject")
Set oshell = CreateObject("WScript.Shell")
Set oproc = oshell.Environment("Process")
strDesktop = oproc.Item("ALLUSERSPROFILE") & "\Desktop\"
overwrite = True
ofso.CopyFile "pwsafe.psafe3" , strDesktop , overwrite
Thank you both.

Related

VBScript command to wait for files to be extracted before launching EXE to install program

I'm looking at having a script that decompresses a file (PDMsetup.zip) and then launch the executable that it extracts.
ZipFile="PDMsetup.zip"
ExtractTo=".\"
Set fso = CreateObject("Scripting.FileSystemObject")
sourceFile = fso.GetAbsolutePathName(ZipFile)
destFolder = fso.GetAbsolutePathName(ExtractTo)
Set objShell = CreateObject("Shell.Application")
Set FilesInZip=objShell.NameSpace(sourceFile).Items()
objShell.NameSpace(destFolder).copyHere FilesInZip, 16
Set fso = Nothing
Set objShell = Nothing
Set FilesInZip = Nothing
wscript.sleep 480000
Set objShell = CreateObject("Wscript.Shell")
strPath = Wscript.ScriptFullName
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(strPath)
strFolder = objFSO.GetParentFolderName(objFile)
strPath = strFolder & "\Startwinstall.exe"
objShell.Run strPath
I want to get rid of;
wscript.sleep 480000
and replace it with a command that tells the script wait until the extraction is done before launching startwinstall.exe
I've kept adjusting the wait time to make up for differences in PC performance with the extraction, but a command to just 'wait' until it's done would be preferential.
Delete any previous copy of the installer exe in the target folder and then wait for that file to be created. Create your objects once at the top of the script. And there's no need to set the objects to Nothing. That will happen automatically when the script ends. The edited script is below:
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oWSH = CreateObject("Wscript.Shell")
Set oApp = CreateObject("Shell.Application")
MyFolder = oFSO.GetParentFolderName(WScript.ScriptFullName)
ExtractTo = ".\"
ZipFile = "PDMsetup.zip"
StartApp = ExtractTo & "Startwinstall.exe"
On Error Resume Next
oFSO.DeleteFile StartApp
On Error Goto 0
sourceFile = oFSO.GetAbsolutePathName(ZipFile)
destFolder = oFSO.GetAbsolutePathName(ExtractTo)
Set FilesInZip = oApp.NameSpace(sourceFile).Items()
oApp.NameSpace(destFolder).copyHere FilesInZip, 16
Do Until oFSO.FileExists(StartApp)
WScript.Sleep 1000
Loop
oWSH.Run StartApp
Note: I assigned a MyFolder variable, but it's not currently being used. ExtractTo = ".\" could be changed to ExtractTo = MyFolder. You could also eliminate the GetAbsolutePathName lines if you are using MyFolder with the ZipFile name. There are always many ways to do the same thing.
Note: I think the above can be done with a much briefer (probably two line) PowerShell script. Let me know if you're interested in that solution.

Create multiple folders by merging two VBS codes

'I have 2 scripts but could not combine them
'create multiple folders script:
Dim objFSO, objFolder, strDirectory, i
strDirectory = "C:\Users\test\Desktop\"
Set objFSO = CreateObject("Scripting.FileSystemObject")
i = 1 ''
While i < 150
Set objFolder = objFSO.CreateFolder(strDirectory & i)
i = i+1
''WScript.Quit ''
Wend
'desktop path script
set WshShell = WScript.CreateObject("WScript.Shell")
strDesktop = WshShell.SpecialFolders("Desktop")
wscript.echo(strDesktop)
I want the code to automatically find the desktop path and then create the folders, some one help me please ?
To get the desktop folder path string and create a sub directory you can do this:
Set objShell = Wscript.CreateObject("Wscript.Shell")
strPath = objShell.SpecialFolders("Desktop")
Dim objFso
Set objFso = WScript.CreateObject("Scripting.FileSystemObject")
If Not objFso.FolderExists(strPath + "\NewFolder") Then
objFso.CreateFolder strPath + "\NewFolder"

The process cannot access the file because it is being used by another process. Code: 80070020, VBScript

When I run my vbscript, it says(In Windows Script Host):
C:\Users\admin\Desktop\Test.vbs
Line: 34
Char:1
Error: The process cannot access the file because it is being used by another process.
Code: 80070020
Source: (null)
How would I be able to fix this? Also here's the script...
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objFSO, objFolder, objShell, objFile
Dim strDirectory, strFile
strDirectory = "c:\Folder"
strFile = "\Hidden.bat"
If objFSO.FolderExists(strDirectory) Then
Set objFolder = objFSO.GetFolder(strDirectory)
Else
Set objFolder = objFSO.CreateFolder(strDirectory)
End If
If objFSO.FileExists(strDirectory & strFile) Then
Set objFolder = objFSO.GetFolder(strDirectory)
Else
Set objFile = objFSO.CreateTextFile(strDirectory & strFile)
End If
set objFolder = nothing
set objFile = nothing
Const fsoForAppend = 8
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objTextStream
Set objTextStream = objFSO.OpenTextFile("C:\Folder\Hidden.bat", fsoForAppend)
objTextStream.WriteLine "attrib ""Folder"" +s +h"
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run """C:\Folder\Hidden.bat"""
Set objShell = Nothing
Without creating any batch file to hide your folder :
Option Explicit
Dim objFSO,objFolder,strDirectory,Command,Result,objShell
Set objFSO = CreateObject("Scripting.FileSystemObject")
strDirectory = "C:\Folder"
If objFSO.FolderExists(strDirectory) Then
Set objFolder = objFSO.GetFolder(strDirectory)
Else
Set objFolder = objFSO.CreateFolder(strDirectory)
End If
set objFolder = nothing
Command = "Cmd /c Attrib +s +h "& DblQuote(strDirectory) &""
Set objShell = CreateObject("WScript.Shell")
Result = objShell.Run(Command,0,True)
Set objShell = Nothing
'****************************************************************
Function DblQuote(str)
DblQuote = Chr(34) & Str & Chr(34)
End Function
'****************************************************************
Closes an open TextStream file.
object.Close
From Help.
You need to close it after writing to it before using it.

vbscript with bginfo using objfso

May I ask how I can make the username as a variable (ex. %userprofile%):
Set objFile = objFSO.OpenTextFile("c:\users\username\AppData\Roaming\Samplefolder\sampletext.txt", 1)
I suppose it's because it's a string with the " ".
Thanks in advance!
You need to use Shell object to fetch the userprofile. Sample script would be
dim objShell, strPath, objFSO, objFile
Set objShell = WScript.CreateObject("WScript.Shell")
strPath = objShell.ExpandEnvironmentStrings("%UserProfile%")
strPath = objFSO.BuildPath(strPath, "AppData\Roaming\Samplefolder\sampletext.txt")
set objFSO = CreateObject("Scripting.FileSystemObject")
set objFile = objFSO.OpenTextFile(strPath,1)

geting special folders with shell.application

I need in a script to return the path to the current user desktop. Now I know you can do it with WScript.
var WshShell = WScript.CreateObject("WScript.Shell");
strDesktop = WshShell.SpecialFolders("Desktop");
But for my script this will not work as I cant use WScript. but I can use the shell.application object as below.
dim objShell
dim ssfWINDOWS
dim objFolder
ssfWINDOWS = 0
set objShell = CreateObject("shell.application")
set objFolder = objshell.BrowseForFolder(0, "Example", 0, ssfWINDOWS)
if (not objFolder is nothing) then
Set objFolderItem = objFolder.Self
g_objIE.Document.All("logdir").Value = objFolderItem.path
end if
set objFolder = nothing
set objShell = nothing
what is the syntax so the rather than "BrowseForFolder" i can simple return the path of the current users desktop?
IE replace the line
set objFolder = objshell.BrowseForFolder(0, "Example", 0, ssfWINDOWS)
with the equilivent of.
strDesktop = WshShell.SpecialFolders("Desktop");
Cheers
Aaron
You need to use Shell.Namespace(...).Self.Path:
Const ssfDESKTOPDIRECTORY = &h10
Set oShell = CreateObject("Shell.Application")
strDesktop = oShell.NameSpace(ssfDESKTOPDIRECTORY).Self.Path
WScript.Echo strDesktop
But for my script this will not work as I cant use WScript.
Do you mean you can't use WScript.CreateObject(...) because WScript is undefined? If so, you can simply use CreateObject("WScript.Shell").SpecialFolders("Desktop") instead. See What is the difference between CreateObject and Wscript.CreateObject?.
Try the namespace method:
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(&H10&)
Where &H10& is a special folder constant for the desktop. See technet for a list of all special folder constants.

Resources