Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 269
  • Last Modified:

Renaming a file .. exm auto.dwg to auto.pub

what I am trying to do is a drag and drop program which it all works ,but the backing up of the file...

when it copies the file. from 1 place to another  i want it to renames the original file to *.wrc

i can't get the name function to rename from auto.exe to auto.wrc..


var..
ren$ = Form3.dir1.path & "\" & Form3.File1.List(fn)
'' the line below is the one i am having problems with..
rento$ = Form3.dir1.path & "\" & "*.wrc"

''this section is straight from microsoft..
''it allows you to use windows 95's file operations.
With fileop
          .hwnd = Form1.hwnd

           .wFunc = FO_RENAME '' this is the rename function

           ' files rename FROM
           .pFrom = ren$
           ' files rename TO
           .pTo = rento$
           
           .fFlags = FOF_SIMPLEPROGRESS Or FOF_FILESONLY
       End With
       result = SHFileOperation(fileop)

       If result <> 0 Then  ' Operation failed
           MsgBox Err.LastDllError 'Msgbox the error that occurred in the
                                   ' API.
       Else
           If fileop.fAnyOperationsAborted <> 0 Then
               MsgBox "Operation Failed"
           End If
       End If

            DoEvents
       
    Form3.File1.Refresh

0
Wild_Bill
Asked:
Wild_Bill
  • 4
  • 2
  • 2
  • +1
1 Solution
 
mcoopCommented:
Apart from being VERY hard to read (your formatting is all over the place), what on earth are you trying to do ?

You have function calls to procedures that are not explained - hence it is difficult to decipher your intent with this code snippet.

If you are simply trying to rename a file...

How about...

Name ren$ As ren2$

Or, if you are trying to do some validation before the rename operatin, then that's another issue, and is highly dependent on the file state, contents and what you are trying to validate.

I hope this helps.
MC
0
 
vmpnCommented:
What are you asking?
0
 
Wild_BillAuthor Commented:
Edited text of question
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
mcoopCommented:
Aha !

I must admit to not having used the SHFileOperation() function...  Let me read a while, and come back with a suggestion. Or perhaps someone else has some experience.

I expect that you are using this call to get the Windows 'Flying folders' box during the copy operation.

If this isn't neccesary, just use the NAME function in VB until we can sort this other call out.
0
 
mcoopCommented:
Bill,

This is a bit fragile, but it does work.  The rename is similar, but I couldn't trap when the target file already existed... maybe you need to do that manually.

I don't have any reference to the SHFileOperation call, so I am just shooting in the dark for some of the params.

I couldn't get the 'raw' call to work, so used the ANSI version SHFileOperationA()

Put the first chunk in a 'global' module, then put the command button code in a form with a button.

- - - - - - - - - - - - -

Declare Function SHFileOperationA Lib "shell32.dll" (lpFileOp As SHFILEOPSTRUCT) As Long

Type SHFILEOPSTRUCT
        hwnd As Long
        wFunc As Long
        pFrom As String
        pTo As String
        fFlags As Integer
        fAnyOperationsAborted As Boolean
        hNameMappings As Long
        lpszProgressTitle As String '  only used if FOF_SIMPLEPROGRESS
End Type

Global FOresult As Long

Global Const FO_COPY = &H2
Global Const FO_DELETE = &H3
Global Const FO_MOVE = &H1
Global Const FO_RENAME = &H4

Global Const FOF_ALLOWUNDO = &H40
Global Const FOF_CONFIRMMOUSE = &H2
Global Const FOF_FILESONLY = &H80
Global Const FOF_MULTIDESTFILES = &H1
Global Const FOF_NOCONFIRMATION = &H10
Global Const FOF_NOCONFIRMMKDIR = &H200
Global Const FOF_RENAMEONCOLLISION = &H8
Global Const FOF_SILENT = &H4
Global Const FOF_SIMPLEPROGRESS = &H100
Global Const FOF_WANTMAPPINGHANDLE = &H20

Private Sub Command1_Click()

Dim opcode As SHFILEOPSTRUCT

  oldfile = "C:\AUTOEXEC.BAT"
  newfile = "C:\NEWFILE.TXT"
  namefile = "C:\RENAMED.TXT"
 
  With opcode
    .hwnd = Me.hwnd
    .wFunc = FO_COPY
    .pFrom = oldfile
    .pTo = newfile
    .fFlags = 0
    .hNameMappings = 0&
    .lpszProgressTitle = vbNull
  End With
 
  FOresult = SHFileOperationA(opcode)

  If opcode.fAnyOperationsAborted = False Then
    MsgBox oldfile & " copied to " & newfile
    Command2.Enabled = True
  Else
    MsgBox oldfile & " NOT COPIED"
  End If
 
End Sub


0
 
Wild_BillAuthor Commented:
answer is very good but the

oldfile="c:\autoexec.bat"
newfile="c:\newfile.txt"
namefile="c:\renamefile.txt"

i want to look like this
oldfile = file(selected)
newfile = file(selected)-old ext. + .wrc

like line 1 & 2 of my code



0
 
msimanCommented:
so what so baad with this?


Declare Function SHFileOperationA Lib "shell32.dll" (lpFileOp As SHFILEOPSTRUCT) As Long

                      Type SHFILEOPSTRUCT
                         hwnd As Long
                         wFunc As Long
                         pFrom As String
                         pTo As String
                         fFlags As Integer
                         fAnyOperationsAborted As Boolean
                         hNameMappings As Long
                         lpszProgressTitle As String ' only used if FOF_SIMPLEPROGRESS
                      End Type

                      Global FOresult As Long

                      Global Const FO_COPY = &H2
                      Global Const FO_DELETE = &H3
                      Global Const FO_MOVE = &H1
                      Global Const FO_RENAME = &H4

                      Global Const FOF_ALLOWUNDO = &H40
                      Global Const FOF_CONFIRMMOUSE = &H2
                      Global Const FOF_FILESONLY = &H80
                      Global Const FOF_MULTIDESTFILES = &H1
                      Global Const FOF_NOCONFIRMATION = &H10
                      Global Const FOF_NOCONFIRMMKDIR = &H200
                      Global Const FOF_RENAMEONCOLLISION = &H8
                      Global Const FOF_SILENT = &H4
                      Global Const FOF_SIMPLEPROGRESS = &H100
                      Global Const FOF_WANTMAPPINGHANDLE = &H20

                      Private Sub Command1_Click()

                      Dim opcode As SHFILEOPSTRUCT
' notice that
oldfile$ = Form3.dir1.path & "\" & Form3.File1.List(fn)
rento$ = left$(oldfile$,len(oldfile$)-3) & ".WRC"
                       With opcode
                         .hwnd = Me.hwnd
                         .wFunc = FO_COPY
                         .pFrom = oldfile
                         .pTo = newfile
                         .fFlags = 0
                         .hNameMappings = 0&
                         .lpszProgressTitle = vbNull
                       End With
                       
                       FOresult = SHFileOperationA(opcode)

                       If opcode.fAnyOperationsAborted = False Then
                         MsgBox oldfile & " copied to " & newfile
                         Command2.Enabled = True
                       Else
                         MsgBox oldfile & " NOT COPIED"
                       End If
                       
                      End Sub
0
 
mcoopCommented:
Hey!....

Does that mean we share the brownie points ?

Cheers !
MC
0
 
msimanCommented:
a long a i concern, u can have these points...
i have enough of them...
just say the magic word....
:))
MSIMAN

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now