You are not logged in.

Dear visitor, welcome to Jabaco - Community. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

HardDrive

Beginner

  • "HardDrive" is male
  • "HardDrive" started this thread

Posts: 12

Date of registration: Sep 22nd 2009

Location: London

Occupation: IT Consultant

Hobbies: inline skating, chess

  • Send private message

1

Thursday, September 24th 2009, 12:46am

WinAPI vs ???

I had used this two WinAPI functions



Public WinAPI Function CreateDirectory Lib "kernel32" Alias "CreateDirectoryA" (ByVal lpPathName As String, ByVal lpSecurityAttributes As Long) As Long
Public WinAPI Function CopyFile Lib "kernel32.dll" Alias "CopyFileA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal bFailIfExists As Long) As Long


And I was wondering if I could avoid using them and replace it with some kind of "internal" functions.



Thank you

OlimilO

Intermediate

  • "OlimilO" is male

Posts: 277

Date of registration: Jan 18th 2009

Location: Germany

Occupation: software engineer

  • Send private message

2

Thursday, September 24th 2009, 10:34am

Hi,

i have updated the FileSystem-modul in the Framework, now we have the function FileCopy.

The Framework is not compiled until now due to other problems, but you could copy the function to your project.

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




greetings



OlimilO

HardDrive

Beginner

  • "HardDrive" is male
  • "HardDrive" started this thread

Posts: 12

Date of registration: Sep 22nd 2009

Location: London

Occupation: IT Consultant

Hobbies: inline skating, chess

  • Send private message

3

Friday, September 25th 2009, 12:12am

FileCopy *New Version*

Thank you very much for that .
But, there is always a but somewhere along the way.
With the WinAPI CopyFile I could copy say "SourceFile" into a "NewDirectory" and end up with "NewDirectory\SourceFile"
Therefore I did some tweaks and the it is a *new version" of FileCopy where I also replaced all the Exit Function statements with Exit Sub (don't know if that was because it will be part of library???)
-----------------------------------------------------------------------

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)


HardDrive

Beginner

  • "HardDrive" is male
  • "HardDrive" started this thread

Posts: 12

Date of registration: Sep 22nd 2009

Location: London

Occupation: IT Consultant

Hobbies: inline skating, chess

  • Send private message

4

Friday, September 25th 2009, 1:49am

Revision

A.I.



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




replaced by



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

HardDrive

Beginner

  • "HardDrive" is male
  • "HardDrive" started this thread

Posts: 12

Date of registration: Sep 22nd 2009

Location: London

Occupation: IT Consultant

Hobbies: inline skating, chess

  • Send private message

5

Friday, September 25th 2009, 3:38am

A Fuction FileCopy() that works like a treat

This function will return True/False if operation succeeds/fails

Will take an existing file (otherwise it fails) and will copy it to:

An existing folder/file (prompts to overwrite file)

A different filename and/or to a new folder (will create the folder)

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

OlimilO

Intermediate

  • "OlimilO" is male

Posts: 277

Date of registration: Jan 18th 2009

Location: Germany

Occupation: software engineer

  • Send private message

6

Friday, September 25th 2009, 12:53pm

file extension

Hi,

yes you were right, "Exit Function" should be "Exit Sub" but to return the function success should even be better :)



please let's think about this piece:

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

could you please tell what lead you to this assumption?

* a directory also can have a point, and

* not every file must have an extension, and

* not every file extension is 3 characters long

* unfortunately on MAC OS file extension do not play a role



greetings



OlimilO

HardDrive

Beginner

  • "HardDrive" is male
  • "HardDrive" started this thread

Posts: 12

Date of registration: Sep 22nd 2009

Location: London

Occupation: IT Consultant

Hobbies: inline skating, chess

  • Send private message

7

Friday, September 25th 2009, 4:18pm

File estension

Hi

Yes, the last version of FileCopy does return the status.
About the extension

Perhaps I am old school, but its useful to indentify the file type with an extension

3 caracters, again old school, comes from the days of dirty DOS

The dot after a directory/folder name will not do anything for identification and can cause some problems since for the OS it represents de Parent directory/folder.
Said that, I realy apreciated your help and comments
Regards
HD

HardDrive

Beginner

  • "HardDrive" is male
  • "HardDrive" started this thread

Posts: 12

Date of registration: Sep 22nd 2009

Location: London

Occupation: IT Consultant

Hobbies: inline skating, chess

  • Send private message

8

Friday, September 25th 2009, 9:29pm

No extension? Must be a folder

Ok

I admite, lots of files can have 1,2 3 or more caracteres on the extension

Said that, with this change its acommodated

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


Its all good

OlimilO

Intermediate

  • "OlimilO" is male

Posts: 277

Date of registration: Jan 18th 2009

Location: Germany

Occupation: software engineer

  • Send private message

9

Friday, September 25th 2009, 10:25pm

Hi,

String-functions can be found in the Strings-module. For Left/Right you could also use the Mid-function

You do not need the For-Next-loop, use the InStrRev-function instead, that returns the position of a string from the right.

greetings

OlimilO

HardDrive

Beginner

  • "HardDrive" is male
  • "HardDrive" started this thread

Posts: 12

Date of registration: Sep 22nd 2009

Location: London

Occupation: IT Consultant

Hobbies: inline skating, chess

  • Send private message

10

Friday, September 25th 2009, 11:25pm

InStrRev - Point taken

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

Rate this thread
WoltLab Burning Board