Results 1 to 13 of 13

Thread: Files

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 1999
    Posts
    308
    What would be an equivalent in VB for a simple DOS command?

    copy A:*.txt c:\temp.txt
    del A:*.txt

    Or maybe someone can give me a hand to run such BAT file with help of Shell. the problem with Shell is that I have to wait untill Shell finished its job. But in W95/98 OS without .pif ("close on exit" properti of BAT file)file shell never finished its process without user's respond.



  2. #2
    Guest
    Use the FileCopy and Kill for Copy and Delete.

    Code:
    MyFile = "C:\MyFile"
    DestFile = "C:\Windows\NewFile"
    
    FileCopy MyFile, DestFile ' Copy MyFile
    Kill "C:\MyFile" ' Delete MyFile

  3. #3
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    Cool ...this is = 2


    'copy or save a file to a different directory

    Private Sub Command1_Click()

    Dim SourcePath As String, DestPath As String
    SourcePath = "c:\my documents\try.txt"
    DestPath = "c:\download\whodoneit.txt"

    FileCopy SourcePath, DestPath

    End



    'using fso kill a file
    'before killing check for existence to avoid error on kill
    '
    Dim sFile$
    sFile = "Path & Name Of Your File.ext"

    Dim FSO As Object
    Set FSO = CreateObject("Scripting.FileSystemObject")

    If FSO.FileExists(sFile$) = True Then
    Kill sFile
    Else
    MsgBox "Sorry, the specified file does not exist!"
    End If
    Set FSO = Nothing
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 1999
    Posts
    308
    Thank you , but you are missing my point: CopyFile does not take a "wild card". Otherwise I would not have a this question.

  5. #5
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    contents of a folder

    'bas module code for copying a file from
    'one directory to another
    'in this case from c:\a vb tips to a:\a vb tips
    'CAUTION....WILL OVERWRITE EXISTING FILES WITHOUT PROMPT

    Option Explicit
    '
    Declare Function CreateDirectory Lib "kernel32" Alias "CreateDirectoryA" _
    (ByVal lpPathName As String, _
    lpSecurityAttributes As SECURITY_ATTRIBUTES) As Long

    Declare Function CopyFile Lib "kernel32" Alias "CopyFileA" _
    (ByVal lpExistingFileName As String, _
    ByVal lpNewFileName As String, _
    ByVal bFailIfExists As Long) As Long

    Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" _
    (ByVal lpFileName As String, _
    lpFindFileData As WIN32_FIND_DATA) As Long

    Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" _
    (ByVal hFindFile As Long, _
    lpFindFileData As WIN32_FIND_DATA) As Long

    Declare Function FindClose Lib "kernel32" _
    (ByVal hFindFile As Long) As Long
    '
    Public Const INVALID_HANDLE_VALUE = -1
    Public Const MAX_PATH = 260

    Type FILETIME
    dwLowDateTime As Long
    dwHighDateTime As Long
    End Type

    Type WIN32_FIND_DATA
    dwFileAttributes As Long
    ftCreationTime As FILETIME
    ftLastAccessTime As FILETIME
    ftLastWriteTime As FILETIME
    nFileSizeHigh As Long
    nFileSizeLow As Long
    dwReserved0 As Long
    dwReserved1 As Long
    cFileName As String * MAX_PATH
    cAlternate As String * 14
    End Type

    Type SECURITY_ATTRIBUTES
    nLength As Long
    lpSecurityDescriptor As Long
    bInheritHandle As Long
    End Type

    Public Function rgbCopyFiles(sSourcePath As String, _
    sDestination As String, _
    sFiles As String) As Long

    Dim WFD As WIN32_FIND_DATA
    Dim SA As SECURITY_ATTRIBUTES

    Dim r As Long
    Dim hFile As Long
    Dim bNext As Long
    Dim copied As Long
    Dim currFile As String

    'Create the target directory if it doesn't exist
    Call CreateDirectory(sDestination, SA)

    'Start searching for files in the Target directory.
    hFile = FindFirstFile(sSourcePath & sFiles, WFD)

    If (hFile = INVALID_HANDLE_VALUE) Then

    'nothing to do, so bail out
    MsgBox "No " & sFiles & " files found."
    Exit Function

    End If

    'Copy each file to the new directory
    If hFile Then

    Do

    'trim trailing nulls, leaving one to terminate the string
    currFile = Left$(WFD.cFileName, InStr(WFD.cFileName, Chr$(0)))

    'copy the file to the destination directory & increment the count
    Call CopyFile(sSourcePath & currFile, sDestination & currFile, False)
    copied = copied + 1

    'find the next file matching the initial file spec
    bNext = FindNextFile(hFile, WFD)

    Loop Until bNext = 0

    End If

    'Close the search handle
    Call FindClose(hFile)

    'and return the number of files copied
    rgbCopyFiles = copied

    End Function

    '>>>> CODE FOR FORM <<<<

    Dim sSourcePath As String
    Dim sDestination As String
    Dim sFiles As String
    Dim numCopied As Long

    'set the appropriate initializing values
    sSourcePath = "c:\A VB Tips\"
    sDestination = "a:\A VB Tips\"
    sFiles = "*.*"

    'perform the copy and return the copied file count; end application
    numCopied = rgbCopyFiles(sSourcePath, sDestination, sFiles)

    MsgBox numCopied & " files copied to " & sDestination

    Unload Me
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 1999
    Posts
    308
    Thanks.

    I got to this point myself but the problem still remains.
    The result of DOS commant copy A:*.txt c:\temp.txt is the only file, so maybe the question should be "How to append files"

  7. #7
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946
    copy A:*.txt c:\temp.txt
    guess I missed again..
    now I see that what you are saying
    is that command copyies all text files form A:\
    into the file temp.txt on the c:\ drive...

    Here I thought it just was going from folder to folder..
    that's a horse of a different color...
    needs a little thought...
    you will have to open and read each file
    stick the reading of each file into an array
    then when all reading is done you will have
    to load the array into the temp.txt file
    and save it...

    am I right in assuming this?
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  8. #8
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Opening in Binary is fast and easy:
    Code:
    Property Get File(Filename As String) As String
        Dim fnum As Byte
        fnum = FreeFile
        Open Filename For Binary As fnum
            File = Space(LOF(fnum))
            Get #fnum, , File
        Close fnum
    End Property
    Property Let File(Filename As String, textstring As String)
        Dim fnum As Byte
        fnum = FreeFile
        If Dir(Filename) <> "" Then Open Filename For Output As fnum: Close fnum
        Open Filename For Binary As fnum
            Put #fnum, , textstring
        Close fnum
    End Property
    You can use it like this:
    Code:
    file("c:\temp.txt")=file("a:\blabla1")+file("a:\blabla2")
    Hope you can suit it for your purpose
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 1999
    Posts
    308
    Yes HeSaidJoe, now you got to the problem.

    You see. The dos command is idial, the only thing is that it (shell) runs it asychronicaly. The next action in the application is to read from appendend file, wich as a matter of fact does not exist yet.
    To solve that I wrote a rutine (OpenProces ...) to wait for DOS to finish its job. And it works without a problem on my work station (NT).
    When I installed it on clients computer (win98), i realized that shell never finises the job. It looks like a difference between OS. It seems that WIN98 ignores Exit command in the BAT file untill you create a shortcut (.pif) file with option "close on exit". Maybe there is a way to create such a file from VB or intallation package? I dont now.
    Running a DOS .bat would be better for me because it is unnown how many files are on A: drive, but as soon as it is a problem with WIN98, I am looking for a solution.

    kedaman, your advice is OK but I would prefer some other thing, just because I dont know either names and numbers of files on A: ( I am given only extentions), I still have to write a painfull procedure to lookup for files names and ....

    Such a small DOS Command is turning to be the VB project, painful ......

    [Edited by LG on 06-22-2000 at 08:55 PM]

  10. #10
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    I wouldnt say there's a problem:
    Code:
    Path = "A:\*.txt"
    a = Dir(Path)
    Do While Len(a)
        b = b & file(a)
        a = Dir
    Loop
    file(destination) = b
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 1999
    Posts
    308
    Looks promissing, I'll give it a tray.

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 1999
    Posts
    308
    kedaman


    Open Filename For Binary As fnum
    File = Space(LOF(fnum))
    Get #fnum, , File
    Close fnum
    For some reason File(a) returns empty string all the time.
    What am I doing wrong?

  13. #13
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    No idea, put a break at
    b = b & file(a)
    and chech out what a is.
    Also you should declare all vars as string.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width