473,385 Members | 1,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 2057
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Mike | last post by:
I want to be able to copy a directory of files (all .HTM files) from a network drive to a local drive on the machine c:\HTMFiles , How can i do that? I tried File.Copy(source, dest) but i need...
6
by: Wayne Wengert | last post by:
I am using VSNET 2003 to build an ASP.NET/VB set of pages. There are currently about a dozen aspx pages. When I make even a minor change to one page I currently rebuild the solution, copy the...
3
by: Johnny | last post by:
Hi, I have created an ASP.NET application (let's call it FooBar) with VS.NET on my local machine, residing in http://localhost/FooBar. Deploying it to another folder on my machine works well...
2
by: Steve Franks | last post by:
The Copy Web tool provided with VS.NET 2005 is very convenient. However every once in a while it seems to think the files on the remote server have changed, which they have not. Then when I use...
5
by: DraguVaso | last post by:
Hi, I'm looking for a way to Copy and Paste Files to the clipboard. I found a lot of articles to copy pieces of text and bitmaps etc, but nog whole files. Whay I need is like you have in...
8
by: luis molina Micasoft | last post by:
it seems that when i do file.copy the svchost.exe is hanged, i mean if i make 40 threads of file.copy , 40 copys of files at same time the system is going down and stop responding, this is when i'm...
1
by: dkmarni | last post by:
Hi, I am trying to do this perl script, but not able to complete it successfully. Here is the description what the script has to do.. Accept two and only two command line arguments. Again,...
13
by: jim | last post by:
Is there a way (using VB.Net or C#) to copy open or locked files? Thanks! jim
1
by: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post by:
Using .NET 2.0 is it more efficient to copy files to a single folder versus spreading them across multiple folders. For instance if we have 100,000 files to be copied, Do we copy all of them to...
1
by: =?Utf-8?B?UmFkZW5rb19aZWM=?= | last post by:
I am using standard File.Copy(source,dest,true) method in C# and I have problem with copying large number of files. Here is my code: foreach (FileInfo file in files) {...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.