DOS-ey VBScript File and Folder Copies
Remember the simpler days? Days when you could copy files by just typing something like "COPY C:\MyFiles\MyDoc.Txt C:\MoreFiles" at the command prompt and things just worked? Well, you can still do that in a command window but things aren't quite that easy in VBScript. Although VBScript does work to do file and folder copies, it adds a level of complexity that just wasn't there in DOS days.First you have to instantiate the file system object, then determined if the file, or folder you are trying to copy exists, determine if the destination exists, include error handling in case something goes wrong, and on and on. It's just a lot harder to do something simple like copy a file or folder from one place to another.
Well, I took some time to remedy this situation to an extent. The following function simplifies the built in methods CopyFolder and CopyFile to one more simple "Copy" function. This function will work fine for both types of copies. Just specify a source file or folder and a destination and this function will copy just like DOS. You can even copy multiple files using "*.*".
The only problem I can see with this script is that it will do multiple file copies, but not multiple file and folder copies. So using the wildcard characters *.* will result in copying of all files in a directory, but not all the folders too. To copy folders you'd have to loop through the folders in the directory and call the copy command on each one. A pain, but I'll post another XCOPY function to facilitate that at some point in the near future.
Alright, without further ado:
| Function Copy(sSource, sDestination, vOverWriteYN) 'This function will copy a file from a "sSource" to a "sDestination" 'include a 1 to enable "vOverWriteYN" or a 0 to disable. 'This script is provided under the Creative Commons license located 'at http://creativecommons.org/licenses/by-nc/2.5/ . It may not 'be used for commercial purposes with out the expressed written consent 'of NateRice.com Set oFSO = CreateObject("Scripting.FileSystemObject") If Not oFSO.FileExists(sSource) AND Not oFSO.FolderExists(sSource) Then Copy = "Error: Source File Not Found." Set oFSO = Nothing Exit Function Elseif oFSO.FolderExists(sSource) Then On Error Resume Next oFSO.CopyFolder sSource, sDestination, vOverWriteYN If Err.Number <> 0 Then If Err.Number = 58 Then Copy = "Error: Folder Exists and OverWrite = No" ElseIf Err.Number = 70 Then Copy = "Error: Permission Denied." Else Copy = "Error: Folder Copy Failed. " & Err.Number End If Err.Clear() Else Copy = True End If On Error Goto 0 Set oFSO = Nothing Exit Function End If If Not oFSO.FolderExists(sDestination) Then If Not Trim(Right(sDestination,1)) = "\" Then aFolders = Split(sDestination,"\") If Not oFSO.FolderExists(Left(sDestination,len(sDestination)-_ len(aFolders(UBound(aFolders))))) Then Copy = "Error: Destination Not Found: " & _ Left(sDestination,len(sDestination)-len(aFolders(UBound(aFolders)))) Set oFSO = Nothing Exit Function End If Else Copy = "Error: Destination Not Found: " & sDestination Set oFSO = Nothing Exit Function End If ElseIf Not Trim(Right(sDestination,1)) = "\" And _ oFSO.FolderExists(sDestination) Then sDestination = sDestination & "\" End If aFileName = Split(sSource,"\") sDestinationWFilename = sDestination & aFileName(UBound(aFileName)) If ( oFSO.FileExists(sDestination) Or oFSO.FileExists(sDestinationWFilename) ) _ And vOverWriteYN = False Then Copy = "Error: Destination File Exists and OverWrite = No" Set oFSO = Nothing Exit Function End If On Error Resume Next oFSO.CopyFile sSource, sDestination, vOverWriteYN If Err.Number <> 0 Then If Err.Number = 70 Then Copy = "Error: Permission Denied." Else Copy = "Error: File Copy Failed. " & Err.Number End If Err.Clear() Set oFSO = Nothing Exit Function End If On Error Goto 0 Copy = True Set oFSO = Nothing End Function |
| Send this to: |

Comments
hack said...
good job but i still want to copy a lot of folders in diferent destinations and some files.it's a project at school:((
7/3/2007 10:16:20 PM