|
-
Jan 30th, 2001, 09:49 AM
#1
Thread Starter
Junior Member
I'm Having trouble with my code!
I am trying to use SHFileOperation API to copy and rename files.
I also could use some code to run batch files but that comes later.
The problem with the SHFileOperation is that, it copies but doesn't rename!
I appreciate all responses...
Here is the code:
--------------------------
Option Explicit
Private Type SHFILEOPSTRUCT
hwnd As Long
wFunc As Long
pFrom As String
pTo As String
fFlags As Integer
End Type
Private Type SHFILEOPSTRUCT2
hwnd2 As Long
wFunc2 As Long
pFrom2 As String
pTo2 As String
fFlags2 As Integer
End Type
Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" _
(lpFileOp As SHFILEOPSTRUCT) As Long
Private Declare Function SHFileOperation2 Lib "shell32.dll" Alias "SHFileOperationA" _
(lpFileOp2 As SHFILEOPSTRUCT2) As Long
' // 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_FILESONLY = &H80
Private Sub Command1_Click()
Dim lResult As Long, SHF As SHFILEOPSTRUCT
SHF.hwnd = hwnd
SHF.wFunc = FO_COPY
SHF.pFrom = "c:\test\*.bat"
SHF.pTo = "c:\"
lResult = SHFileOperation(SHF) 'first copy
If lResult Then
MsgBox "Error in Copying files", vbInformation, "Error 1"
End If
Dim lResult2 As Long, SHF2 As SHFILEOPSTRUCT2
SHF.wFunc = FO_RENAME
SHF.pFrom = "c:\*.bat"
SHF.pTo = "c:\*.txt"
SHF.fFlags = FOF_SILENT
lResult2 = SHFileOperation2(SHF2) 'then rename
If lResult2 Then
MsgBox "Error in renaming files", vbInformation, "Error 2"
End If
End Sub
Private Sub Form_Load()
Move (Screen.Width - Width) \ 2, (Screen.Height - Height) \ 2
End Sub
---------------
It works If I change these lines:
-----
SHF.pFrom = "c:\*.bat"
SHF.pTo = "c:\*.txt"
-----
To:
-----
SHF.pFrom = "c:\test.bat"
SHF.pTo = "c:\test.txt"
-----
WHY???????
I NEED TO RENAME MULTIPLE FILES!!!!
Also how do you end a command box so I don't have to x it out to end the program?
Thanks,
Stan
Last edited by [email protected]; Jan 30th, 2001 at 02:33 PM.
-
Jan 30th, 2001, 09:57 AM
#2
Frenzied Member
Hey you funny!
why did you make 2 api calls and 2 structures (ah **** too much into C++, I mean types ) for that?
One will do 
If it doesn't work, you may have to try looping trough each file and subdir, and kill it from there.
I have a function to loop trough all files in my file module too: http://www.geocities.com/despotez/file.bas.txt
then look for GetAllFiles.
At the point it's adding the file to a listbox, remove that and use Kill to delete the file.
Good luck!
Jop - validweb.nl
Alcohol doesn't solve any problems, but then again, neither does milk.
-
Jan 30th, 2001, 10:07 AM
#3
_______
<?>
Is there a reason you can't just use the VB FileCopy Function.
'Change a file's name and or directory
Dim sOldName as string, sNewName as string
sOldName = "mypath/mysubdirectory/myFile.txt"
sNewName = "mypath/mysubdirectory/myNewFile.txt"
FileCopy sOldName AS sNewName
With a for loop you can go through a dir or subdir
and do it in one shot.
"A myth is not the succession of individual images,
but an integerated meaningful entity,
reflecting a distinct aspect of the real world."
___ Adolf Jensen
-
Jan 30th, 2001, 10:32 AM
#4
Thread Starter
Junior Member
OK GUYS HERE'S NEW CODE:
STILL DOESN'T RENAME!
-------------------------------------------
Option Explicit
Private Type SHFILEOPSTRUCT
hwnd As Long
wFunc As Long
pFrom As String
pTo As String
fFlags As Integer
End Type
Private Type SHFILEOPSTRUCT2
hwnd2 As Long
wFunc2 As Long
pFrom2 As String
pTo2 As String
fFlags2 As Integer
End Type
Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" _
(lpFileOp As SHFILEOPSTRUCT) As Long
' // 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_FILESONLY = &H80
Private Sub Command1_Click()
Dim lResult As Long, SHF As SHFILEOPSTRUCT
SHF.hwnd = hwnd
SHF.wFunc = FO_COPY
SHF.pFrom = "c:\test\*.bat"
SHF.pTo = "c:\"
lResult = SHFileOperation(SHF) 'first copy
If lResult Then
MsgBox "Error in Copying files", vbInformation, "Error 1"
End If
SHF.wFunc = FO_RENAME
SHF.pFrom = "c:\*.bat"
SHF.pTo = "c:\*.txt"
SHF.fFlags = FOF_SILENT
If lResult Then
MsgBox "Error in renaming files", vbInformation, "Error 2"
End If
End Sub
Private Sub Form_Load()
Move (Screen.Width - Width) \ 2, (Screen.Height - Height) \ 2
End Sub
-
Jan 30th, 2001, 10:41 AM
#5
Thread Starter
Junior Member
VB FIleCOpy Function
Can you use this to copy multiple files at once, then rename at once? copy *.bat ...ren *.bat to *.txt ???
If so help me!!!!I'm new at this.
There has to be a way to run a simple batch using shell commands but I can't get anything to work!!!
Thanks,
Stan
-
Jan 30th, 2001, 10:58 AM
#6
Well bugger me, does this mean I'm a guru? 
Use the name command to rename the file afterward (save the use of complicating it using the API) -
Name "A_File.doc", "Another_File.xls"
easy peasy lemon squeezy
-
Jan 30th, 2001, 02:23 PM
#7
Thread Starter
Junior Member
name command?????
but I need to rename a lot of files not just one!
when I rename *.bat to *.txt it gives me a runtime error.
---------
Name "C:\*.bat" As "c:\*.txt"
Thanks,
Stan
-
Jan 31st, 2001, 03:33 AM
#8
Why not create a separate sub -
Code:
Public Sub RenameFile(Old as string, new as string, Path as string)
If right(path, 1) <> "\" then
Path = Path & "\"
End If
'Check if the path ends in a \. If not, put one in.
Name Path & old, Path & new
'This will rename the file.
End Sub
Private Sub command1_click()
Dim Path as string
Path = "C:\My_Folder"
RenameFile "File1.bat", "file1.txt" path
RenameFile "File2.bat", "file2.txt" path
RenameFile "File3.bat", "file3.txt" path
RenameFile "File4.bat", "file4.txt" path
RenameFile "File5.bat", "file5.txt" path
End Sub
As for your second part, the file will only not install if either you're using it (check in the ctrl+alt+del menu) or it's a system file, other than that this should work. What file are you trying to rename?
I've written an extension changer program I'll post here (when I find the #{@!* thing ). Give me a day or so.
-
Jan 31st, 2001, 08:43 AM
#9
Frenzied Member
You should loop trough each file (READ MY POSTS!)
then use the Name function or the FileOperation (from my module) to rename each single file!!!!!!
http://www.geocities.com/despotez/file.bas.txt
Jop - validweb.nl
Alcohol doesn't solve any problems, but then again, neither does milk.
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
|