|
-
Feb 2nd, 2001, 09:18 AM
#1
Thread Starter
Junior Member
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
-
Feb 7th, 2001, 07:33 AM
#2
Addicted Member
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]
-
Feb 8th, 2001, 09:21 AM
#3
Thread Starter
Junior Member
hide option
whats the code for a hide option?
-
Feb 11th, 2001, 02:45 AM
#4
Addicted Member
-
Feb 11th, 2001, 09:07 AM
#5
Member
' 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
-
Feb 12th, 2001, 03:10 PM
#6
Thread Starter
Junior Member
Run time error
FileCopy SrcName, DestName
gives me a runtime error 53
-
Feb 12th, 2001, 04:47 PM
#7
Member
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.
-
Feb 12th, 2001, 04:55 PM
#8
Thread Starter
Junior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|