Permission denied copying my own documents? (Win 7) - windows-7

I'm using the following vbscript to back up my documents:
If strMessage = vbYes Then
Dim FSO
Dim objShell
Dim strDesktop
Dim strDocuments
Set FSO = CreateObject("Scripting.FileSystemObject")
Set objShell = WScript.CreateObject("Wscript.Shell")
strDesktop = objShell.SpecialFolders("Desktop")
strDocuments = objShell.SpecialFolders("MyDocuments")
If FSO.DriveExists("H") Then
FSO.CopyFolder strDesktop, "H:\Desktop", True
FSO.CopyFolder strDocuments, "H:\Documents", True
Else
MsgBox("Your personal folder is not mapped. Please contact IT to have it mapped or created.")
End if
Else
MsgBox("You will be reminded again in 30 days")
End if
It works fine, except I run into a "permissions denied" error when copying My Documents. I cannot think of any reason why I wouldn't have access to copy my own documents....is it something to do with Windows Script Host?

Related

Trying to code for the first time, but failed with file location

The problem is that I tried to make a file on somebody else's desktop but I don't know what to write in the users.
I tried "C:\Users\" & strUser & "\desktop\Test"
Set fso = CreateObject("Scripting.filesystemobject")
fso.Createfolder "C:\Users\" & strUser & "\desktop\Test"
error: Can't find the file location.
Use the special folder Shell function to return the path to the desktop.
set objShell = Wscript.CreateObject("Wscript.Shell")
strDesktopPath = objShell.SpecialFolders("Desktop")
Set fso = CreateObject("Scripting.filesystemobject")
fso.Createfolder strDesktopPath & "\Test"
I think your code will work if you simply add a \ at the end of your path:
Set fso = CreateObject("Scripting.filesystemobject")
fso.Createfolder "C:\Users\" & strUser & "\desktop\Test\"

Downloading File on Google Drive to Run Macro

I am trying to get a .vbs to download files from google drive as I have had it working as a macro inside excel. It would just be overall easier without needing to go into excel to run the script. I have tried to Frankenstein the working code within the macro which downloads the file to downloads with some other but have got lost. I am still a rookie so if the code is less than ideal would appreciate some feedback.
As a note: The userprofile was attempted to be set as variable for multiple users with differently mapped download folders to still run it.
Ran macro in excel which achieved downloading the file.
Tried to create .vbs with similar code and failed
Option Explicit
Dim FileNum
Dim FileData
Dim MyFile
Dim WHTTP
Dim strDisplayName
Dim oShell
Dim strHomeFolder
Dim vbDirectory
Dim dir
Set oShell = CreateObject("WScript.Shell")
strHomeFolder = oShell.ExpandEnvironmentStrings("%USERPROFILE%")
'Set strDisplayName = CreateObject("Scripting.FileSystemObject")
'Set objAD = CreateObject("ADSystemInfo")
'Set objUser = GetObject("LDAP://" & objAD.UserName)
'strDisplayName = objUser.DisplayName
On Error Resume Next
Set WHTTP = CreateObject("WinHTTP.WinHTTPrequest.5")
If Err.Number <> 0 Then
Set WHTTP = CreateObject("WinHTTP.WinHTTPrequest.5.1")
End If
On Error GoTo 0
MyFile = "https://drive.google.com/open?id=1Hx5DymjrhaiM8Rb7NMP_Sde_JU2N2Rau"
WHTTP.Open "GET", MyFile, False
WHTTP.send
'On Error Resume Next
FileData = WHTTP.ResponseBody
Set WHTTP = Nothing
If Dir(strHomeFolder & "\Downloads", vbDirectory) = Empty Then MkDir strHomeFolder & "\Downloads"
Dim xlApp, xlBook
Set xlApp = CreateObject("Excel.Application")
xlApp.DisplayAlerts = False
Set xlBook = xlApp.Workbooks.Open(strHomeFolder & "\Downloads\External Linking.xlsm", 0, True)
'D:\Users\cdoyle\Desktop\External linking.xlsm
'https://drive.google.com/open?id=1Hx5DymjrhaiM8Rb7NMP_Sde_JU2N2Rau
'"C:\Users\ciara\OneDrive\BayswaterBridge.xlsm"'
xlApp.Run "BridgeHit"
'xlbook.Save False
xlBook.Close False
set xlBook = Nothing
xlApp.Quit
Set xlApp = Nothing
'WScript.Echo "Finished."
WScript.Quit
Keep getting some errors on If Dir(
line and seem to be chasing my tail.

CreateTextFile error

I am getting a 'Permission denied' error when trying to run this VBScript after dragging another file or folder icon onto the script's icon.
I have tried running it on two Windows 10 machines and if I run it with arguments if objects at the file creation line, otherwise the scripts completes normally.
Any ideas?
Option Explicit
Dim objFSO
Dim objShell
Dim fldTest
Dim fileTest
Dim strPath
Dim txsOutput
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set txsOutput = objFSO.CreateTextFile("Test.txt")
For Each strPath In WScript.Arguments
fldTest = objFSO.GetFolder(strPath)
For Each fleTest In fldTest.Files
...
Next
Next
txsOutput.Close()
Set txsOutput = Nothing
Set objFSO = Nothing
WScript.Quit

Copy file from script directory to All Users' desktop

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.

how to delete multiple folders,desktop and start menu shortcut using vbscript

I never did any vbscript before, so i don't know if my question is very easy one. Following is the flow of steps that has to be done :
Check if exist and delete a folder at c:\test1 if found and continue. If not found continue.
Check if exist and delete a folder at c:\programfiles\test2 if found and continue. If not found continue.
Check if a desktop shortcut and start menu shortcut exist and delete if found. If not exit.
I could delete 2 folders with the following code:
strPath1 = "C:\test1"
strPath1 = "C:\test1"
DeleteFolder strPath1
DeleteFolder strPath1
Function DeleteFolder(strFolderPath1)
Dim objFSO, objFolder
Set objFSO = CreateObject ("Scripting.FileSystemObject")
If objFSO.FolderExists(strFolderPath) Then
objFSO.DeleteFolder strFolderPath, True
End If
Set objFSO = Nothing
But i need to run one script to delete 2 folders in different paths, 2 shortcuts one in start menu and one on desktop.
I was experimenting with this code to delete the shortcut on my desktop:
Dim WSHShell, DesktopPath
Set WSHShell = WScript.CreateObject("WScript.Shell")
DesktopPath = WSHShell.SpecialFolders("Desktop")
on error resume next
Icon = DesktopPath & "\sample.txt"
Set fs = CreateObject("Scripting.FileSystemObject")
Set A = fs.GetFile(Icon)
A.Delete
WScript.Quit
It works fine for txt file on desktop, but how do i delete a shortcut for an application from desktop as well as start menu.
strPath1 = "C:\test1"
strPath2 = "C:\test2"
DeleteFolder strPath1
DeleteFolder strPath2
DeleteShortcut
'-------------------------------------------------------
Sub DeleteFolder(strFolderPath)
Set fso = CreateObject ("Scripting.FileSystemObject")
If fso.FolderExists(strFolderPath) Then
fso.DeleteFolder strFolderPath, True
End If
End Sub
'-------------------------------------------------------
Sub DeleteShortcut()
Set WSHShell = WScript.CreateObject("WScript.Shell")
DesktopPath = WSHShell.SpecialFolders("Desktop")
shortcutPath = DesktopPath & "\MyShortcut.lnk"
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(shortcutPath) Then
Set myFile = fso.GetFile(shortcutPath)
myFile.Delete
End If
End Sub

Resources