Srcipt vbs, moveFolder return Permission denied - windows

Option Explicit
dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FolderExists("C:\Users\michal\Desktop\mv_files_backup") then
fso.MoveFolder "C:\Users\michal.glowacki\Desktop\mv_files_backup\*.*", "\\192.168.10.245\backup\servers\backup_server"
Else
wscript.echo "doesn't exist"
End If
When I try run this script I have error:
Permission denied 800A0046
but when I change MoveFolder to CopyFolder script works correctly. Why I can't use function MoveFolder ?

From MSDN:
If source contains wildcards or destination ends with a path separator
( \ ), it is assumed that destination specifies an existing folder in
which to move the matching files. Otherwise, destination is assumed to
be the name of a destination folder to create. In either case, three
things can happen when an individual folder is moved:
If destination does not exist, the folder gets moved. This is the
usual case.
If destination is an existing file, an error occurs.
If destination is a directory, an error occurs.
An error also occurs if a wildcard character that is used in source
doesn't match any folders. The MoveFolder method stops on the first
error it encounters. No attempt is made to roll back any changes made
before the error occurs.
I've marked in bold the parts that explain your problem. Because your destination doesn't end with a trailing backslash, your script is attempting to create the folder, and failing. Try it with a \ added to the end of the destination and report back if it still doesn't work.
Try this:
fso.MoveFolder "C:\Users\michal.glowacki\Desktop\mv_files_backup", "\\192.168.10.245\backup\servers\backup_server\"

Related

How to get the basename when it gets truncated with period in path?

Am I missing a trick here?
Dim fso
Set fso = WScript.CreateObject("Scripting.FileSystemObject")
WScript.Echo fso.GetBaseName("D:\temp\1. Some Folder")
WScript.Echo fso.GetBaseName("D:\temp\Some Other Folder Without A Dot")
WScript.Echo fso.GetAbsolutePathName("D:\temp\1. Some Folder")
The code above for the basename gets truncated at the dot/period.
D:\temp\1
I'm assuming that VBScript is thrown by the dot. Is there a trick to getting around this? Or do you have to modify the full path after the last index of slash?
I believe GetFileName will produce the result you're looking for.
From the docs:
Returns the last component of a specified path that is not part of the drive specification.
The GetBaseName function indeed sees everything after the last dot as extension so the folder name you are expecting is truncated.
Safer, but only for existing paths could be to use the GetFolder function to receive a Folder object and take the Name property from that:
WScript.Echo fso.GetFolder("D:\temp\1. Some Folder").Name
returns
Some Folder

FileSystemObject.CopyFile is not able to copy file from one folder to another

I have a piece of VBS code where a file(C:\test.txt) should be copied to a newly created temporary folder. But it fails to copy the file. Weird thing is that the same function works fine when I have wild characters in the source parameter (C:\ *est.txt). Any advice would be really helpful.
set fso = createobject("Scripting.FileSystemObject")
if fso.FileExists(src_temp) then
'src_temp contains the file path.
'Path of the temporary folder
Set tmp_fld = fso.GetSpecialFolder(TemporaryFolder)
tmp_fld = tmp_fld & "\OldFiles_tmp"
'Create the temporary folder if does not exist
If Not fso.FolderExists(tmp_fld) Then
fso.CreateFolder(tmp_fld)
End If
'Copy the files to temporary path
On Error Resume Next
fso.CopyFile src_temp, tmp_fld, True 'last parameter is set as true for overwriting the existing
On Error Goto 0
End If
I have verified if destination temporary folder is created and also the path and other stuffs. How can a wild character in the path makes the CopyFile work and same does not work for complete file name. Also how to solve this issue?
Put a backslash (\) at the end of the destiny folder:
tmp_fld = tmp_fld & "\OldFiles_tmp\" 'note the \
I tested and it worked for me. But, strangely, the wildcard thing was not working for me in the original source code.

'Permission denied' error when counting files

I'm trying to count (recursivly) the number of files in a folder using VBS.
I have derived the following function but whenever I run it I get an error -
800A0046 - Permission denied
This happens on all of the folders I wish to know the file count of, with all folders either being local or on a mapped drive to which I have full access.
I have passed the folder paths both with and without a trailing slash and the error still occurs.
Can anybody tell me how I can fix this problem?
Function CountFiles(ByVal folder)
Dim parentFolder
Dim subFolder
Dim count
'** Grab an instance of the current parent folder *'
Set parentFolder = FSO.GetFolder(folder)
'** Count the files in the parent folder *'
count = parentFolder.Files.Count
'** Count all files in each subfolder - recursion point *'
For Each subFolder In parentFolder.SubFolders
count = count + CountFiles(subFolder.Path)
Next
'** Return a count of the files *'
CountFiles = count
End Function
By removing the dangerous fat from David's code (lavishly introducing variables (like IntCount), spurious back (folder object) and forth (folder.Path)) you get a clean function:
Option Explicit
Const csFolder = ".\"
Dim goFS : Set goFS = CreateObject("Scripting.FileSystemObject")
Function CountFiles(oFolder)
CountFiles = oFolder.Files.Count
Dim oSubFolder
For Each oSubFolder In oFolder.SubFolders
CountFiles = CountFiles + CountFiles(oSubFolder)
Next
End Function
WScript.Echo "Files in", csFolder, CountFiles(goFS.GetFolder(csFolder))
This can be tested (e.g. on "\"):
cscript 30263200.vbs
Files in .\ 146
and checked vs the Properties of the folder shown by the Explorer (trust me, the 146 files are correct).
If you get a "Permission denied" for some other folder, then there is at least one folder in the tree you don't have permission to access. If all
of the starting folders you are interested in cause this error, you have no permissions for any of them.
As there are reasons for the granting of permissions, this probably is a good thing.
Consider to shell out to dir /s /b, which would give you a list of all the folders/files you are entitled to know about without breaking if there are any forbidden ones.

File Path When Including Nested Files in a Script

I have a script file which opens a text file located in the same directory. Let's call it SubScript.
SubScript.vbs
Function DoSomething(foo)
...
Dim Key
With CreateObject("Scripting.FileSystemObject")
Key = .OpenTextFile("key.txt", 1).ReadAll
End With
...
End Function
No problem here when the script is run on its own. However, I want to use the script above in another script file, "MainScript". The SubScript is located in a subfolder in the MainScript directory.
MainScript.vbs
With CreateObject("Scripting.FileSystemObject")
ExecuteGlobal .OpenTextFile(".\SubDir\SubScript.vbs", 1).ReadAll
End With
When I try and use the DoSomething function in the SubScript, I get a file not found error. I see what is happening, the subscript is trying to find the text file in the MainScript directory, where it doesn't exist.
Is there a way, without using an absolute file path, to make sure the SubScript loads the text file from the SubDir?
A relative path is resolved wrt the current directory of the process. Sometimes you can use the script's folder to get more flexibility. But in your case (.ExecuteGlobal), the SubScript's current directory is the current directory of the MainScript.
You should pass a path to DoSomething(), unless you can live with hardcoding ".\SubDir\key.txt".

Permission denied on CopyFile in VBS

I'm trying to automate pushing a file into my users' home directories, but am stuck on a "Permission Denied" error — is thrown on line 6 here, with the CopyFile call.
There are other parts of the script (not shown) that create and copy folder contents using the same source and destination directories, and they work perfectly. It's only when I use CopyFile that it fails.
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FileExists("H:\Minecraft\.minecraft\options.txt") Then
fso.CopyFile "C:\Minecraft\options.txt", "H:\Minecraft\.minecraft\"
End If
Set fso = Nothing
H: is a network home directory, to which the current user has full read/write privs.
I've tried adding/removing trailing slashes from the paths, adding "options.txt" to the destination path, removing the false argument... Not sure what else to try. Any thoughts?
FYI, this chunk of code, which comes immediately before the error-prone bit above, executes perfectly every time:
If Not fso.FolderExists("H:\Minecraft\.minecraft\bin\") Then
If Not fso.FolderExists("H:\Minecraft\.minecraft\") Then
fso.CreateFolder("H:\Minecraft\.minecraft\")
End If
fso.GetFolder("C:\Minecraft\bin\").Copy "H:\Minecraft\.minecraft\"
End If
I've only ever seen CopyFile fail with a "permission denied" error in one of these 3 scenarios:
An actual permission problem with either source or destination.
Destination path is a folder, but does not have a trailing backslash.
Source file is locked by an application.
for me adding / worked at the end of location of folder.
Hence, if you are copying into folder, don't forget to put /
Based upon your source variable (sourcePath = "C:\Minecraft\bin\") I suspect your hard code is pointing at the wrong place
fso.CopyFile "C:\Minecraft\options.txt", destinationPath, false
should be
fso.CopyFile "C:\Minecraft\bin\options.txt", destinationPath
or
fso.CopyFile sourcePath & "options.txt", destinationPath
Another thing to check is if any applications still have a hold on the file.
Had some issues with MoveFile. Part of my permissions problem was that my script opens the file (in this case in Excel), makes a modification, closes it, then moves it to a "processed" folder.
In debugging a couple things, the script crashed a few times. Digging into the permission denied error I found that I had 4 instances of Excel running in the background because the script was never able to properly terminate the application due to said crashes. Apparently one of them still had a hold on the file and, thusly, "permission denied."
I have read your problem, And i had the same problem. But af ter i changed some, my problem "Permission Denied" is solved.
Private Sub Addi_Click()
'On Error Resume Next
'call ds
browsers ("false")
Call makeAdir
ffgg = "C:\Users\Backups\user\" & User & "1\data\"
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.Getfolder("c:\users\Backups\user\" & User & "1\data")
f.Attributes = 0
Set fso = VBA.CreateObject("Scripting.FileSystemObject")
Call fso.Copyfile(filetarget, ffgg, True)
Look at ffgg = "C:\Users\Backups\user\" & User & "1\data\", Before I changed it was ffgg = "C:\Users\Backups\user\" & User & "1\data" When i add backslash after "\data\", my problem is solved. Try to add back slash. Maybe solved your problem. Good luck.
You can do this:
fso.CopyFile "C:\Minecraft\options.txt", "H:\Minecraft\.minecraft\options.txt"
Include the filename in the folder that you copy to.
It's worth checking task manager for any stray wscript.exe tasks that are stuck.
It could be one of those that's blocking access to the file.
It is possible that your Antivirus software may be preventing the activity of the script. I encountered this with AVG Antivirus running in silent mode (which means it does not alert you to every protection step it takes, so you get a permission error without realising that AVG is preventing the action).
In my case, I invoked verbose mode (switched off silent mode), executed the script and AVG came up with an interception warning, allowing me to train AVG to permit this script to run.

Resources