471,066 Members | 1,585 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,066 software developers and data experts.

Copy Files

Hello Group.

I have a directory with 3000 ".jpg" named like this:

1234_01.jpg
1234_09.jpg
1234_11.jpg
1234_12.jpg
2341_01.jpg
2341_05.jpg
2341_06.jpg
2341_08.jpg
2341_09.jpg
(...)

Four figures (product reference) followed by "_"
I need to copy one file of each reference to another directory.

Thanks in advance.
Apr 3 '08 #1
4 1965
On Apr 3, 12:51*pm, Masta <mllo...@areagrup.comwrote:
Hello Group.

I have a directory with 3000 ".jpg" named like this:

1234_01.jpg
1234_09.jpg
1234_11.jpg
1234_12.jpg
2341_01.jpg
2341_05.jpg
2341_06.jpg
2341_08.jpg
2341_09.jpg
(...)

Four figures (product reference) followed by "_"
I need to copy one file of each reference to another directory.
I don't think you can do that with just one scan of the files in the
directory, that is by using a search string to return just one file
that has a unique value in the first four characters of the file name.

I would tackle it like this. I would generate a list of all possible
files with a Directory.GetFiles method call. Then I would loop through
the list an pick off the first four chacacters with a String.Substring
method call. I would add the four letter value to a generic List(Of
String), but only if it hasn't been added yet. Just use the
List.Contains method to check that. Next I would loop through the list
and do a Directory.GetFiles method call specifying a search string of
<first four letters>*.jpg. Pick one them to copy, either random, the
first one, the last one, whatever floats your boat, and copy it with
the File.Copy method.

Apr 3 '08 #2
Thanks a lot.. I have sucessfully done it with your help.
Apr 4 '08 #3
On Apr 3, 1:11 pm, za...@construction-imaging.com wrote:
On Apr 3, 12:51 pm, Masta <mllo...@areagrup.comwrote:
Hello Group.
I have a directory with 3000 ".jpg" named like this:
1234_01.jpg
1234_09.jpg
1234_11.jpg
1234_12.jpg
2341_01.jpg
2341_05.jpg
2341_06.jpg
2341_08.jpg
2341_09.jpg
(...)
Four figures (product reference) followed by "_"
I need to copy one file of each reference to another directory.

I don't think you can do that with just one scan of the files in the
directory, that is by using a search string to return just one file
that has a unique value in the first four characters of the file name.

I would tackle it like this. I would generate a list of all possible
files with a Directory.GetFiles method call. Then I would loop through
the list an pick off the first four chacacters with a String.Substring
method call. I would add the four letter value to a generic List(Of
String), but only if it hasn't been added yet. Just use the
List.Contains method to check that. Next I would loop through the list
and do a Directory.GetFiles method call specifying a search string of
<first four letters>*.jpg. Pick one them to copy, either random, the
first one, the last one, whatever floats your boat, and copy it with
the File.Copy method.
What about this using VB2008? This selects a random filename for each
product reference and copies it to a destination folder. It also
saves calling GetFiles more than once.

Private Sub Button_Click(...) Handles Button1.Click
Dim rnd As New Random

Dim files = From f In Directory.GetFiles("c:\test\filefind") _
Group f By key = Path.GetFileName(f).Substring(0,
4) Into Group _
Select Group(rnd.Next(0, Group.Count))

For Each f As String In files
File.Copy(f, Path.Combine("c:\destination",
Path.GetFileName(f)))
Next
End Sub

If you just want the first file in each product reference group,
change the Select line to

Select Group(0)

Or if you want the last file in each product reference group, change
the Select line to

Select Group(Group.Count - 1)

Chris
Apr 4 '08 #4
Thanks Chris. Very useful your post.
Apr 7 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Mike | last post: by
6 posts views Thread by Wayne Wengert | last post: by
2 posts views Thread by Steve Franks | last post: by
8 posts views Thread by luis molina Micasoft | last post: by
13 posts views Thread by jim | last post: by
1 post views Thread by =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post: by
1 post views Thread by =?Utf-8?B?UmFkZW5rb19aZWM=?= | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.