Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Permission Denied objFile.Copy

Status
Not open for further replies.

shatterstar6457

Programmer
Aug 6, 2006
4
US
Im getting the permission denied runtime error on the following line of code

strTargetFolder = objFSO.CreateFolder(strStartTargetFolder & "\" & objFile.Name)

and yes I am the admin on my machine.
Here is the rest of the code:

Option Explicit

Dim objFSO, objFile, objFolder, objSubFolder, objShell,_
colFiles, colFolders, colSubFolders,_
strStartDir, strExtension, strFolderPath, strSubFolderPath, strTargetFolder, strStartTargetFolder,_
intCount

strStartDir = InputBox("Full path of folder to check?")
strExtension = InputBox("Please enter the File Extension To Search For")
strStartTargetFolder = InputBox("Where would you like to Place the files? (Starting Directory)")


Set objShell = CreateObject("WScript.Shell")
Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strStartDir)
Set colFiles = objFolder.Files

strFolderPath = objFolder.Path

intCount = 0

For each objFile in colFiles
If lcase(Right(objFile.Name,3)) = strExtension Then
strTargetFolder = objFSO.CreateFolder(strStartTargetFolder & "\" & objFile.Name)
objFile.Copy strTargetFolder, True
intCount = intCount + 1
Else
WScript.Echo "The File " & objFile.Name & " Isn't a match according to your query"
end If
Next

ScanSubFolders(objFolder)
WScript.Echo "I Copied " & intCount & " " & strExtension & " Files"

Sub ScanSubFolders(objFolder)

Set colFolders = objFolder.SubFolders

For Each objSubFolder In colFolders
strSubFolderPath = objSubFolder.Path

Set colFiles = objSubFolder.Files

For Each objFile in colfiles
If lcase(Right(objFile.Name,3)) = strExtension Then
strTargetFolder = objFSO.CreateFolder(strStartTargetFolder & "\" & objFile.Name)
objFile.Copy strTargetFolder, True
intCount = intCount + 1
Else
WScript.Echo "The File " & objFile.Name & " Isn't a match according to your query"
end If

Next

ScanSubFolders(objSubFolder)
Next

End Sub
 
[1] You practically _must_ validate the inputs before processing further for this kind of functionality. Furthermore, you have to put more assurance as to fileexists() and folderexists() before doing create or copy (with or without overwrite boolean).

[2]
>strTargetFolder = objFSO.CreateFolder(strStartTargetFolder & "\" & objFile.Name)
>objFile.Copy strTargetFolder, True
[2.1]
The first line is wrong that strTargetFolder is a folder object, not a string anymore.
[tt] [red]set[/red] strTargetFolder = objFSO.CreateFolder(strStartTargetFolder & "\" & objFile.Name)[/tt]
[2.1.1] If the path contains either a file or a folder of the same name, it will be a runtime error.
[2.1.2] That means if you run the script the second time without deleting the source file, it will error out. This shows error control is material.
[2.2]
The .copy line is again wrong, even you have corrected per [2.1]. You create specifically a folder of that path (suppose that's what you mean strTargetFolder) and then you copy a file of whatever name to that folder. If you do not put a backslash there, it will be an error: the script engine in fact won't discern a file or a folder by that name (without backslash). If it finds one, it tries to overwrite it. But it would fail to overwrite a folder by a file. Hence, in all case, you are guarantee to have the "permission denied" error. Hence the proper line would be this (after taken into consideration of properly making strTargetFolder as folder object in [2.1]).
[tt] objFile.Copy strTargetFolder[red].path & "\"[/red], True[/tt]
 
Further notes
[2.1.3] Since you createfolder with objFile.Name, that means you are going to result in one folder one file only even the other errors are corrected.
[2.1.4] If you ever have by accident or not strStartTargetFolder the same as strStartDir, you won't ever be able to create the folder as the script engine will find the file of the same path (the source file) and refuse to make a folder of that path.
 
continue"? you must think continue is a universal language statement. So does continue help you? What language are you thinking of, continue?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top