Det store af det lange er at jeg skal lave en copy funktion til binære filer, men jeg vil gerne kunne vise infomation om hvor lang den er nået efterhånden som den får overført data.
Vi taler mange gigabyte i hver fil, så den skal kunen holde til det, hvis du forstår
--- Dim path = "c:\temp.tmp" Dim binf As Stream = New FileStream(path, FileMode.Open) Dim c As Integer While (c = binf.ReadByte) >= 0 'Console.Write(c) Console.Write(binf.ReadByte) End While binf.Close()
Module Main Sub Copy(fnm1 As String, fnm2 As String) Dim f1 As Stream = New FileStream(fnm1, FileMode.Open, FileAccess.Read) Dim f2 As Stream = new FileStream(fnm2, FileMode.Create, FileAccess.Write) Dim c As Integer Dim n As Integer = 0 Dim more as boolean = True While (more) c = f1.ReadByte If c >= 0 then f2.WriteByte(CType(c, Byte)) n = n + 1 If (n Mod 10000) = 0 Then Console.WriteLine(n) End If Else more = False End If End While f1.Close f2.Close Console.WriteLine(n) End Sub Sub Main() Copy("C:\z1.dat", "C:\z2.dat") End Sub End Module
Module Main Sub Copy(fnm1 As String, fnm2 As String) Dim cr As String = Convert.ToChar(13) Dim f1 As Stream = New FileStream(fnm1, FileMode.Open, FileAccess.Read) Dim f2 As Stream = new FileStream(fnm2, FileMode.Create, FileAccess.Write) Dim c As Integer Dim n As Integer = 0 Dim more as boolean = True While (more) c = f1.ReadByte If c >= 0 then f2.WriteByte(CType(c, Byte)) n = n + 1 If (n Mod 100000) = 0 Then Console.Write(cr & n) End If Else more = False End If End While f1.Close f2.Close Console.WriteLine(cr & n) End Sub Sub Main() Copy("C:\z1.dat", "C:\z2.dat") End Sub End Module
1. Er det muligt at putte hele byte størrelsen ned af filen før vi kopier? 2. Hvordan læser jeg variabler/parametre på min exe fil? f.eks copy.exe c:\david.aaa c:\peder.aaa
Module Main Sub Copy(fnm1 As String, fnm2 As String) Dim cr As String = Convert.ToChar(13) Dim size As Integer = (new FileInfo(fnm1)).Length Dim f1 As Stream = New FileStream(fnm1, FileMode.Open, FileAccess.Read) Dim f2 As Stream = new FileStream(fnm2, FileMode.Create, FileAccess.Write) Dim c As Integer Dim n As Integer = 0 Dim more as boolean = True While (more) c = f1.ReadByte If c >= 0 then f2.WriteByte(CType(c, Byte)) n = n + 1 If (n Mod 100000) = 0 Then Console.Write(cr & n & " af " & size) End If Else more = False End If End While f1.Close f2.Close Console.WriteLine(cr & n & " af " & size) End Sub Sub Main() Copy("C:\z1.dat", "C:\z2.dat") End Sub End Module
Sub Main(ByVal args As String()) Dim files As String() = Directory.GetFiles(args(0)) For Each f As String In files Copy(f, args(1) & f.Substring(args(0).Length)) Next End Sub
hmmm, måske man så skulle lave et check på hvor stor filen er til at starte med og så derefter vælge buffer... Men det skalder vel også noget kode til..
Module Main Sub Copy(fnm1 As String, fnm2 As String) Dim cr As String = Convert.ToChar(13) Dim size As Integer = (new FileInfo(fnm1)).Length Dim f1 As Stream = New FileStream(fnm1, FileMode.Open, FileAccess.Read) Dim f2 As Stream = new FileStream(fnm2, FileMode.Create, FileAccess.Write) Dim b(100000) As Byte Dim n As Long = 0 Dim actual As Integer Dim more as boolean = True While (more) actual = f1.Read(b, 0, b.Length) If actual > 0 then f2.Write(b, 0, actual) n = n + actual Console.Write(cr & n & " af " & size) Else more = False End If End While f1.Close f2.Close Console.WriteLine(cr & n & " af " & size) End Sub Sub Main() Copy("C:\z1.dat", "C:\z2.dat") End Sub End Module
Sub CopyDir(dir1 As String, dir2 As String) Dim files As String() = Directory.GetFiles(dir1) For Each f As String In files Copy(f, dir2 & f.Substring(dir1.Length)) Next Dim dirs As String() = Directory.GetDirectories(dir1) For Each d As String In dirs CopyDir(d, dir2 & d.Substring(dir1.Length)) Next End Sub Sub Main(ByVal args As String()) CopyDir(args(0), args(1)) End Sub
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.