Beginner
Date of registration: Sep 22nd 2009
Location: London
Occupation: IT Consultant
Hobbies: inline skating, chess
Jabaco Source |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
Public Sub FileCopy(Source As String, Destination As String) ' 'http://www.java2s.com/Code/Java/File-Input-Output/CopyfilesusingJavaIOAPI.htm 'OlimilO: 'VBFile-Class? ' Dim fromFile As New java#io#File(Source) Dim toFile As New java#io#File(Destination) If (Not fromFile.exists) Then MsgBox("FileCopy: " & "no such source file: " & Source) Exit Function End If If (Not fromFile.isFile) Then MsgBox("FileCopy: " & "can't copy directory: " & Source) Exit Function End If If (Not fromFile.canRead) Then MsgBox("FileCopy: " & "source file is unreadable: " & Source) Exit Function End If If (toFile.isDirectory) Then toFile = New File(toFile, fromFile.getName) End If If (toFile.exists) Then If (Not toFile.canWrite()) Then MsgBox("FileCopy: " & "destination file is unwriteable: " & Destination) End If Dim ms As String = "Overwrite existing file?" & vbCrLf & toFile.getName If MsgBox(ms, vbOKCancel) <> vbOK Then MsgBox("FileCopy: " & "existing file was not overwritten.") Exit Function End If Else Dim Parent As java#lang#String = toFile.getParent If (Parent = Nothing) Then Parent = System.getProperty("user.dir") End If Dim dir As New File(Parent) If (Not dir.exists) Then If MsgBox("FileCopy: " & "destination directory does not exist create it?: " & Parent) = vbOK Then dir.mkdir Else Exit Function End If End If If (dir.isFile) Then MsgBox("FileCopy: " & "destination is not a directory: " & Parent) Exit Function End If If (Not dir.canWrite) Then MsgBox("FileCopy: " & "destination directory is unwriteable: " & Parent) Exit Function End If End If Dim fisFrom As New java#io#FileInputStream(fromFile) Dim fosTo As New java#io#FileOutputStream(toFile) Dim buffer() As Byte Redim buffer(4096) Dim bytesRead As Integer bytesRead = fisFrom.read(buffer) While (bytesRead <> -1) fosTo.write(buffer, 0, bytesRead) bytesRead = fisFrom.read(buffer) Wend End Sub |
Beginner
Date of registration: Sep 22nd 2009
Location: London
Occupation: IT Consultant
Hobbies: inline skating, chess
Jabaco Source |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
Public Sub FileCopy(Source As String, Destination As String) ' 'http://www.java2s.com/Code/Java/File-Input-Output/CopyfilesusingJavaIOAPI.htm 'OlimilO: 'VBFile-Class? ' Dim fromFile As New java#io#File(Source) Dim toFile As New java#io#File(Destination) ' More elegant to do the checks over Source and Destination ' and allows new directory to be created to copy the source file into it ' using the original file name i.e. FileCopy("SourceFile","C:\NewDirectory") ' resuts a copy like "C:\NewDirectory\SourceFile" With fromFile Select Case False Case .exists MsgBox("FileCopy: no such source file: " & Source) Exit Sub Case .isFile MsgBox("FileCopy: can't copy directory: " & Source) Exit Sub Case .canRead MsgBox("FileCopy: source file Is unreadable: " & Source) Exit Sub End Select End With With toFile If (.isDirectory) Then toFile = New File(toFile, fromFile.getName) End If If (.exists) Then If (Not .canWrite()) Then 'If Destination was a folder then the file name was missing using the Destination reference MsgBox("FileCopy: destination file Is unwriteable: " & toFile) End If Dim ms As String = "FileCopy: overwrite existing file?" & vbCrLf & .getName If MsgBox(ms, vbOKCancel) <> vbOK Then MsgBox("FileCopy: existing file was Not overwritten.") Exit Sub End If Else Dim Parent As String = .getParent If (Parent = Nothing) Then Parent = System.getProperty("user.dir") End If 'Directory? or File? If MsgBox("FileCopy: destination is a directory?", vbYesNo) = vbYes Then Parent = toFile toFile = New File(toFile, fromFile.getName) End If Dim dir As New File(Parent) ' Directory checks If (Not dir.exists) Then If MsgBox("FileCopy: " & "destination directory does not exist create it? " & Parent) = vbOK Then dir.mkdir Else Exit Sub End If End If If (dir.isFile) Then MsgBox("FileCopy: " & "destination is not a directory: " & Parent) Exit Sub End If If (Not dir.canWrite) Then MsgBox("FileCopy: " & "destination directory is unwriteable: " & Parent) Exit Sub End If End If End With Dim fisFrom As New java#io#FileInputStream(fromFile) Dim fosTo As New java#io#FileOutputStream(toFile) Dim buffer(4096) As Byte 'Assigned dimenson during declaration 'Redim buffer(4096) Dim bytesRead As Integer bytesRead = fisFrom.read(buffer) While (bytesRead <> -1) fosTo.write(buffer, 0, bytesRead) bytesRead = fisFrom.read(buffer) Wend End Sub |
This post has been edited 3 times, last edit by "HardDrive" (Sep 25th 2009, 12:39am)
Beginner
Date of registration: Sep 22nd 2009
Location: London
Occupation: IT Consultant
Hobbies: inline skating, chess
Jabaco Source |
|
1 2 3 4 5 |
'Directory? or File? If MsgBox("FileCopy: destination is a directory?", vbYesNo) = vbYes Then Parent = toFile toFile = New File(toFile, fromFile.getName) End If |
Jabaco Source |
|
1 2 3 4 5 6 7 |
'Is a Directory or File? - No extension assumes a Directory If (Len(toFile)>4 And Left(Right(toFile,4),1) = ".") Or (Len(toFile)>5 And Left(Right(toFile,5),1) = ".") Then ' Due to existing extension it assumes to be a file Else Parent = toFile toFile = New File(toFile, fromFile.getName) End If |
Beginner
Date of registration: Sep 22nd 2009
Location: London
Occupation: IT Consultant
Hobbies: inline skating, chess
Jabaco Source |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
Public Function FileCopy(Source As String, Destination As String) As Boolean ' 'http://www.java2s.com/Code/Java/File-Input-Output/CopyfilesusingJavaIOAPI.htm 'OlimilO:, HD 'VBFile-Class? ' Dim fromFile As New java#io#File(Source) Dim toFile As New java#io#File(Destination) FileCopy = False ' More elegant to do the checks over Source and Destination ' and allows new directory to be created to copy the source file into it ' using the original file name i.e. FileCopy("SourceFile","C:\NewDirectory") ' resuts a copy like "C:\NewDirectory\SourceFile" With fromFile Select Case False Case .exists MsgBox("FileCopy: no such source file: " & Source) Exit Function Case .isFile MsgBox("FileCopy: can't copy directory: " & Source) Exit Function Case .canRead MsgBox("FileCopy: source file Is unreadable: " & Source) Exit Function End Select End With With toFile If (.isDirectory) Then toFile = New File(toFile, fromFile.getName) End If If (.exists) Then If (Not .canWrite()) Then 'If Destination was a folder then the file name was missing using the Destination reference MsgBox("FileCopy: destination file Is unwriteable: " & toFile) End If Dim ms As String = "FileCopy: overwrite existing file?" & vbCrLf & .getName If MsgBox(ms,vbYesNo) = vbNo Then MsgBox("FileCopy: existing file was Not overwritten.") Exit Function End If Else Dim Parent As String = .getParent If (Parent = Nothing) Then Parent = System.getProperty("user.dir") End If 'Is a Directory or File? - No extension assumes a Directory If (Len(toFile)>4 And Left(Right(toFile,4),1) = ".") Or (Len(toFile)>5 And Left(Right(toFile,5),1) = ".") Then ' Due to existing extension it assumes to be a file Else Parent = toFile toFile = New File(toFile, fromFile.getName) End If Dim dir As New File(Parent) ' Directory checks If (Not dir.exists) Then dir.mkdir End If If (dir.isFile) Then MsgBox("FileCopy: " & "destination is not a directory: " & Parent) Exit Function End If If (Not dir.canWrite) Then MsgBox("FileCopy: " & "destination directory is unwriteable: " & Parent) Exit Function End If End If End With Dim fisFrom As New java#io#FileInputStream(fromFile) Dim fosTo As New java#io#FileOutputStream(toFile) Dim buffer(4096) As Byte 'Assigned dimenson during declaration 'Redim buffer(4096) Dim bytesRead As Integer bytesRead = fisFrom.read(buffer) While (bytesRead <> -1) fosTo.write(buffer, 0, bytesRead) bytesRead = fisFrom.read(buffer) Wend FileCopy = True End Function |
Jabaco Source |
|
1 2 3 |
'Is a Directory or File? - No extension assumes a Directory If (Len(toFile)>4 And Left(Right(toFile,4),1) = ".") Or (Len(toFile)>5 And Left(Right(toFile,5),1) = ".") Then ' Due to existing extension it assumes to be a file |
Beginner
Date of registration: Sep 22nd 2009
Location: London
Occupation: IT Consultant
Hobbies: inline skating, chess
Beginner
Date of registration: Sep 22nd 2009
Location: London
Occupation: IT Consultant
Hobbies: inline skating, chess
Jabaco Source |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
'Is a Directory or File? - No extension assumes a Directory y = Len(toFile) d = True For x = 2 To y - 1 If Left(Right(toFile,x),1) = "." Then d = False x = y End If Next If d Then Parent = toFile toFile = New File(toFile, fromFile.getName) End If '+++++++++++++ Old code ' If (Len(toFile)>4 And Left(Right(toFile,4),1) = ".") Or (Len(toFile)>5 And Left(Right(toFile,5),1) = ".") Then ' ' Due to existing extension it assumes to be a file ' Else ' Parent = toFile ' toFile = New File(toFile, fromFile.getName) ' End If |
Beginner
Date of registration: Sep 22nd 2009
Location: London
Occupation: IT Consultant
Hobbies: inline skating, chess
Jabaco Source |
|
1 2 3 4 5 |
'Is a Directory or File? - No extension assumes a Directory If InStrRev(toFile,".") = 0 Then Parent = toFile toFile = New File(toFile, fromFile.getName) End If |