Solved

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

Posted on 1997-08-15
9
261 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
  • 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

863 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now