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 Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File.Copy("c:\dirname\abc*.txt... Does Not Work! 1

Status
Not open for further replies.

talln6e

Programmer
Dec 6, 2001
6
US
I am trying to copy a block of files from one folder to
another. I would like to refer to these files by the first
few characters, using a wildcard.

The command
File.Copy("c:\sourcedir\abc*.","c:\destdir\abc*.",True)
does not work.

Does anyone know the proper way to do this or have a workaround?
It seems like there should be a straightforward method.

Thanks in Advance!
Howie
 
Hi
I am not sure
File.Copy commands allow you to use wildcards
but you can use the following routine to process a directory with specific files to process
hope it will help you out
Regards
Nouman
Private Sub ProcessDirectory(ByVal targetDirectory As String)
Try
Dim fileEntries As String() = Directory.GetFiles(targetDirectory)
' Process the list of files found in the directory
Dim fileName As String
Dim i As Integer
Dim strFileToProcess As String
Dim strFileOutBound As String

For Each fileName In fileEntries
if mid(fileName,1,3)="abc" then
strFileOutBound = "C:\SOURCEDIR\"& & fileName
strFileToProcess = "C:\TERGETDIR\ & fileName
File.Copy(strFileOutBound, strFileToProcess)
End If
Next fileName

Catch exp As Exception
MsgBox(exp.Message, MsgBoxStyle.Information, strSystemTitle)
End Try
End Sub 'ProcessDirectory

Nouman Zaheer
Software Engineer
MSR
 
Thanks Nouman

But I really want to have one copy command.
This utility I'm writing may be used to copy 1000 files at a time.

I also have a similar looping routine you may be interested in seeing:

Dim SourceDir As String() = Directory.GetFiles("c:\sourcedir\", "abc*.txt")
Dim SourceFile As String
Dim FilePart As String
Dim SplitName As Array
For Each SourceFile In SourceDir
SplitName = Split(SourceFile, "\")
FilePart = SplitName(UBound(SplitName))
File.Copy(SourceFile, "c:\newdir" & FilePart, True)
Next

I am still working on a non-looping solution and would welcome any efforts to help!

Howie
 
Hi
Howie
I don't think you can able to do what you want with the File.Copy command the thing which you want beacuse you need to somehow use wild-card for the files but may be i am wrong
good luck
Regards
Nouman

Nouman Zaheer
Software Engineer
MSR
 
You need to have an asterisk after the "." in your original post...

[tt]The command
File.Copy("c:\sourcedir\abc*.","c:\destdir\abc*.",True)
does not work.[/tt]

It should be ("c:\sourcdir\abc*.*", "c:\destdir\abc*.*", True)
 
Thank you for your help, but unfortunately, when I try

File.Copy("c:\sourcedir\abc*.jpg","c:\destdir\abc*.jpg",True)
or
File.Copy("c:\sourcedir\abc*.*","c:\destdir\abc*.*",True)

neither work.

Thanks again Nouman and Sypher for your time!
 
As stated earlier File.Copy doesn't support wild cards.

However the old FileSystemObject in the Scripting Runtime COM Object has a method called CopyFile that does support wildcards but it can be quite scary to use because if it fails it gives you no clues as to where it has gone wrong.

Read the documentation on CopyFile carefully as it goes into quite a lot of detail as to what is possible with wildcards. If it isn't suitable then use one of the iterative approaches suggested above as with these at least you know exactly what file has failed to copy and to be honest the performance will not be much worse if at all.



 
try reading the name of the file before moving it. then move it from the old location to the new one. not sure if this will work yet, i am going to work on it this evening when i get home.


Becca

Somtimes, the easy answer is the hardest to find. :)

Still under construction ...
 
You could probably use a FileInfo and DirectoryInfo object to interate through all the files in the directory. I'm not sure if this is what you want to do, though. It will just copy everything that matches the wildcards...

[tt]Imports System.IO

Dim diJpeg as New DirectoryInfo("C:\SourceDir\")
Dim fi as FileInfo

For Each fi In diJpeg.GetFiles("abc*.jpg")
fi.CopyTo("C:\DestDir\", True)
Next[/tt]
 
Hi
As i submit in my post the main thing is that you have to iterate to get what you want no matter if you are using file system object or file object but last post of syper2 suggestion is better if its do what you want
Regards
Nouman

Nouman Zaheer
Software Engineer
MSR
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top