Results 1 to 8 of 8

Thread: I need help with code...

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2001
    Posts
    29

    Talking

    My problem is I can't get this program to rename multiple files."BinRename("C:\*.bat", "C:\*.txt")"
    It will only rename one file. Can any one suggest how I could do this?

    Thanks,
    Stan

    Code:
    -------------

    Option Explicit

    Const FO_MOVE = &H1
    Const FO_COPY = &H2
    Const FO_DELETE = &H3
    Const FO_RENAME = &H4
    Const FOF_SILENT = &H4
    Const FOF_NOCONFIRMATION = &H10
    Const FOF_FILESONLY = &H80
    Const FOF_MULTIDESTFILES = &H1


    Private Type SHFILEOPSTRUCT
    hwnd As Long
    wFunc As Long
    pFrom As String
    pTo As String
    fFlags As Integer
    End Type
    ' Functions
    Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" _
    (lpFileOp As SHFILEOPSTRUCT) As Long

    Private Function BinRename(FromFileName As String, ToFileName As String) As Boolean

    Dim FileOperation As SHFILEOPSTRUCT
    Dim lReturn As Long

    With FileOperation
    .wFunc = FO_RENAME
    .pFrom = FromFileName
    .pTo = ToFileName
    .fFlags = FOF_MULTIDESTFILES
    End With

    lReturn = SHFileOperation(FileOperation)
    BinRename = (lReturn = 0)

    End Function
    'Sub Click
    Private Sub Command1_Click()

    Dim lResult As Long, bRetVal As Long, SHF As SHFILEOPSTRUCT, FileOperation As SHFILEOPSTRUCT

    SHF.hwnd = hwnd
    SHF.wFunc = FO_COPY ' copy
    SHF.pFrom = "c:\test\*.bat"
    SHF.pTo = "c:\"
    lResult = SHFileOperation(SHF)
    If lResult Then
    MsgBox "Error in Copying files", vbInformation, "Error 1"
    End If
    bRetVal = BinRename("C:\*.bat", "C:\*.txt")
    Unload Form1
    End Sub
    'Sub Form
    Private Sub Form_Load()
    Move (Screen.Width - Width) \ 2, (Screen.Height - Height) \ 2
    End Sub

  2. #2
    Addicted Member KrishnaSantosh's Avatar
    Join Date
    Feb 2001
    Location
    Coimbatore
    Posts
    210
    I Dont Know The Exact API Calls.

    But You can do onething.

    Create a Batch (.bat) File From Within VB (using Standard File Functions Like Open,Close And Print#) And Write To File The Exact Dos Command For That Rename.

    Then Close The File and Run That File Using Standard Shell Command with the Hide Option. I Think This Should Work.

    My EMail Is : [email protected]

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jan 2001
    Posts
    29

    hide option

    whats the code for a hide option?

  4. #4
    Addicted Member KrishnaSantosh's Avatar
    Join Date
    Feb 2001
    Location
    Coimbatore
    Posts
    210
    Shell "Prg",vbHide

  5. #5
    Member
    Join Date
    Jul 1999
    Posts
    42
    ' A real VB solution, be carefull with the Kill statement
    Option Explicit

    Private Sub Command1_Click()
    RenameFromTo "C:\", "TXT", "BAK"
    End Sub



    Sub RenameFromTo(Path As String, FromExt As String, ToExt As String)
    Dim SrcName As String, DestName As String

    If Right$(Path, 1) <> "\" Then Path = Path + "\"
    SrcName = Dir(Path & "*." & FromExt, vbNormal + vbSystem + vbReadOnly + vbHidden)

    Do While Len(SrcName) <> 0
    DestName = Left$(SrcName, Len(SrcName) - Len(FromExt)) & ToExt
    FileCopy SrcName, DestName
    'Kill SrcName
    SrcName = Dir
    Loop

    End Sub

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jan 2001
    Posts
    29

    Angry Run time error

    FileCopy SrcName, DestName

    gives me a runtime error 53

  7. #7
    Member
    Join Date
    Jul 1999
    Posts
    42
    Runtime error 53: file not found.

    Maybe something wrong with the extensions FromExt, ToExt or with the periode in the file name.
    Place some message boxes or debug.print statements in the Do while ... loop to examine the
    source and destination file names. If the extension FromExt = "" (empty) the source file name
    may cause an runtime error if the periode isn't removed from the end of the source file name?

    What extensions did you use?
    Last edited by Alfred; Feb 12th, 2001 at 04:51 PM.

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Jan 2001
    Posts
    29

    CODE:

    Here is the code:
    --------------------------

    Option Explicit

    ' // Shell File Operations

    Const FO_MOVE = &H1
    Const FO_COPY = &H2
    Const FO_DELETE = &H3
    Const FO_RENAME = &H4
    Const FOF_SILENT = &H4
    Const FOF_NOCONFIRMATION = &H10
    Const FOF_FILESONLY = &H80
    Const FOF_MULTIDESTFILES = &H1


    Private Type SHFILEOPSTRUCT
    hwnd As Long
    wFunc As Long
    pFrom As String
    pTo As String
    fFlags As Integer
    End Type

    ' Functions
    Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" _
    (lpFileOp As SHFILEOPSTRUCT) As Long
    Dim FileOperation As SHFILEOPSTRUCT
    Dim lReturn As Long

    'Sub Click
    Private Sub Command1_Click()
    Dim lResult As Long, bRetVal As Long, SHF As SHFILEOPSTRUCT, FileOperation As SHFILEOPSTRUCT
    SHF.hwnd = hwnd
    SHF.wFunc = FO_COPY ' copy
    SHF.pFrom = "c:\test\*.bat"
    SHF.pTo = "c:\"
    lResult = SHFileOperation(SHF)
    If lResult Then
    MsgBox "Error in Copying files", vbInformation, "Error 1"
    End If
    RenameFromTo "C:\", "BAT", "TXT"
    Unload Form1
    End Sub

    'Sub Rename
    Sub RenameFromTo(Path As String, FromExt As String, ToExt As String)
    Dim SrcName As String, DestName As String

    If Right$(Path, 1) <> "\" Then Path = Path + "\"
    SrcName = Dir(Path & "*." & FromExt, vbNormal + vbSystem + vbReadOnly + vbHidden)

    Do While Len(SrcName) <> 0
    DestName = Left$(SrcName, Len(SrcName) - Len(FromExt)) & ToExt
    FileCopy SrcName, DestName

    'Kill SrcName
    SrcName = Dir
    Loop

    End Sub

    'Sub Form
    Private Sub Form_Load()
    Move (Screen.Width - Width) \ 2, (Screen.Height - Height) \ 2
    End Sub

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