Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File move with wildcards 1

Status
Not open for further replies.

mmilan

Programmer
Jan 22, 2002
839
GB
This is really beginning to get on my nerves, so I thought I'd throw it open to everyone and see what you all came up with...

I am trying to write a function meeting the following criteria:

* Moves a specific file to another location (might be different drive or network), and then
* Moves any other files matching an expression containing a wildcard into the same destination.

The logic of the function, playing with the path strings etc isn't an issue - that's all done.

My first approach was to work with the movefile method of the file system object, which worked fine most of the time, but raised Automation errors on one specific box in a managers office. (This problem was also in evidence at other points in the code that made use of FileSystemObject).

I've now had instructions from the powers that be that I am to remove all FileSystemObjects from our project - which is causing me problems when I try to do a move with wildcards.

At the moment, I am considering writing the function so that it makes use of dir() to identify the files which need to be moved, using FileCopy to actually move them, and then killing the originals by using setattr to deal with any read only flag and then putting them though a Kill Statement.

Does anyone have any better ideas?

mmilan.

Ps. I'm prepared to consider API, but the calls must be supported in everything from win95 up.

Thanks in anticipation...
 
>File move with wildcards
>I'm prepared to consider API,
>but the calls must be supported in everything from win95 up.

You should certainly go for the SHFileOperation function which is supported on all versions of Windows since Win95. It allows you to Move/Copy/Delete/Rename files and also supports wild cards. It means that the function operate an all files that fit the wildcard criteria.

In addition to this, this function also has a variety of different options which allow you to control the file operation precisely if you want. See a simple demonstration of this function below.
___
[tt]
Private Declare Function SHFileOperation Lib "shell32" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
Private Type SHFILEOPSTRUCT
hWnd As Long
wFunc As Long
pFrom As String
pTo As String
fFlags As Integer
fAborted As Long
hNameMaps As Long
sProgress As String
End Type
Enum FileOperationVerbs
fovMove = 1
fovCopy = 2
fovDelete = 3
End Enum
Enum FileOperationFlags
fofSilent = &H4 'Do not display progress bar
fofNoConfirmation = &H10 'Say 'Yes' to all confirmations automatically
fofAllowUndo = &H40 'The operation can be undone from Windows Explorer
fofNoConfirmMakeDir = &H200 'Don't ask if the dest. dir. doesn't exist
fofNoErrorUI = &H400 'Do not display error messages (if any)
End Enum
Const FOF_FILESONLY = &H80
Const FOF_NORECURSION = &H1000

Sub FileOperation(Operation As FileOperationVerbs, SrcFile As String, DestDir As String, Optional Flags As FileOperationFlags)
On Error Resume Next
Dim shfos As SHFILEOPSTRUCT
With shfos
.fFlags = FOF_FILESONLY Or FOF_NORECURSION Or Flags
.hWnd = Screen.ActiveForm.hWnd
.pFrom = SrcFile & vbNullChar & vbNullChar
.pTo = DestDir & vbNullChar & vbNullChar
.wFunc = Operation
End With
SHFileOperation shfos
End Sub

Private Sub Command1_Click()
FileOperation fovCopy, "C:\Windows\*.exe", "C:\New", fofNoConfirmMakeDir
End Sub[/tt]
___

You can search google to find many other examples of this function.
See also thread222-710209 for another example of this function.

For more information, see the SHFileOperation function

and the SHFILEOPSTRUCT structure.
 
Cheers Hypetia,

I'll give you a star later - the script doesn't seem to work on my machine here...

mmilan
 
it really works for me here . . just want to know if it is possible to remove the copy progress?

Please pardon the grammar.
Not good in english.
 
COnsider the following Enumeration in Hypetia's code:
Code:
[COLOR=blue]
Enum FileOperationFlags
    [COLOR=red]fofSilent = &H4             'Do not display progress bar[/color]
    fofNoConfirmation = &H10    'Say 'Yes' to all confirmations automatically
    fofAllowUndo = &H40         'The operation can be undone from Windows Explorer
    fofNoConfirmMakeDir = &H200 'Don't ask if the dest. dir. doesn't exist
    fofNoErrorUI = &H400        'Do not display error messages (if any)
End Enum
[/color]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top