Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 1997-08-15
9
Medium Priority
?
268 Views
Last Modified: 2008-02-20
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
Comment
Question by:Wild_Bill
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 4

Expert Comment

by:mcoop
ID: 1431038
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
 
LVL 3

Expert Comment

by:vmpn
ID: 1431039
What are you asking?
0
 

Author Comment

by:Wild_Bill
ID: 1431040
Edited text of question
0
Industry Leaders: 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!

 
LVL 4

Expert Comment

by:mcoop
ID: 1431041
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
 
LVL 4

Expert Comment

by:mcoop
ID: 1431042
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
 

Author Comment

by:Wild_Bill
ID: 1431043
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
 
LVL 2

Accepted Solution

by:
msiman earned 50 total points
ID: 1431044
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
 
LVL 4

Expert Comment

by:mcoop
ID: 1431045
Hey!....

Does that mean we share the brownie points ?

Cheers !
MC
0
 
LVL 2

Expert Comment

by:msiman
ID: 1431046
a long a i concern, u can have these points...
i have enough of them...
just say the magic word....
:))
MSIMAN

0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question