PDA

Click to See Complete Forum and Search --> : I need help with code...


stan@croftmetals.com
Feb 2nd, 2001, 08:18 AM
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

KrishnaSantosh
Feb 7th, 2001, 06:33 AM
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 : santoshkrishna@santoshsmail.com

stan@croftmetals.com
Feb 8th, 2001, 08:21 AM
whats the code for a hide option?

KrishnaSantosh
Feb 11th, 2001, 01:45 AM
Shell "Prg",vbHide

Alfred
Feb 11th, 2001, 08:07 AM
' 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

stan@croftmetals.com
Feb 12th, 2001, 02:10 PM
FileCopy SrcName, DestName

gives me a runtime error 53

Alfred
Feb 12th, 2001, 03:47 PM
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?

stan@croftmetals.com
Feb 12th, 2001, 03:55 PM
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