Struggling to move files from one folder to another

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 1118 Views - Last Post: 14 November 2017 - 11:10 AM Rate Topic: -----

#16 Whateva_  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 28-August 16

Re: Struggling to move files from one folder to another

Posted 06 November 2017 - 10:14 PM

Final solution

DirectoryInfo diCopyFrom = new DirectoryInfo(textBox1.Text);
			DirectoryInfo diCopyTo = new DirectoryInfo(textBox2.Text);
			foreach (FileInfo xmlFile in diCopyTo.EnumerateFiles("*.xml", SearchOption.AllDirectories))
			{
				string tifDirectoryName = Path.GetFileNameWithoutExtension(xmlFile.Name);
				DirectoryInfo sourceDirectory = diCopyFrom.EnumerateDirectories(tifDirectoryName, SearchOption.AllDirectories).FirstOrDefault();
				if (sourceDirectory != null)
				{
					string destinationDirectoryName = xmlFile.DirectoryName;
					foreach (FileInfo tifFile in sourceDirectory.EnumerateFiles("*.tif"))
					{
						string destFileName = Path.Combine(destinationDirectoryName, tifFile.Name);
						tifFile.CopyTo(destFileName, true);
					}
				}
			}
			MessageBox.Show("Finished");

Was This Post Helpful? 0
  • +
  • -

#17 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1375
  • View blog
  • Posts: 4,309
  • Joined: 20-March 10

Re: Struggling to move files from one folder to another

Posted 06 November 2017 - 11:11 PM

Yes,

I ended up with something similar....

except I used .png's

Glad I could help.
Was This Post Helpful? 0
  • +
  • -

#18 Whateva_  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 28-August 16

Re: Struggling to move files from one folder to another

Posted 07 November 2017 - 12:34 AM

Hi, snoopy11

But is this the fastest way possible, the from_path is generally located on a different machine and I'm connected to it via LAN and if there are many folders to work with won't it take a long time to process?
Was This Post Helpful? 0
  • +
  • -

#19 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1375
  • View blog
  • Posts: 4,309
  • Joined: 20-March 10

Re: Struggling to move files from one folder to another

Posted 07 November 2017 - 01:26 AM

Yes I think it is pretty much unless instead of enumerating the .xml files you just enumerate the folder 'Assets' if that is where the .xml files will always be under.

You must remember that I don't have as much information to go as you do and ultimately its your program not mine.
Was This Post Helpful? 0
  • +
  • -

#20 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5889
  • View blog
  • Posts: 20,095
  • Joined: 05-May 12

Re: Struggling to move files from one folder to another

Posted 07 November 2017 - 06:18 AM

If from_path is on a different machine and needs to go across the LAN, then repeatedly hitting the disk to search for files and directories on line 6 and 10 is not doing you any favors.

Line 6 is particularly bad because it does a recursive search across the network. You are using EnumerateDirectories() which returns an IEnumerable<> and so the FirstOrDefault() will short circuit the search after the first match is found. If your probability of finding a match is less than 50%, though, then this doesn't save you anything because the entire tree is searched before it is determined that no match is available and FirstOrDefault() will return null.

My recommendation is get a full list of files and directories of the from_path just once and store it in memory as a tree a best, or a list at worse, and then do all searches against local memory rather than repeatedly hitting the disk across the network.
Was This Post Helpful? 0
  • +
  • -

#21 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1375
  • View blog
  • Posts: 4,309
  • Joined: 20-March 10

Re: Struggling to move files from one folder to another

Posted 07 November 2017 - 07:00 AM

Yes very true, I never used first or default in any of my code but the OP seems persistent on using it....
Was This Post Helpful? 0
  • +
  • -

#22 Whateva_  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 28-August 16

Re: Struggling to move files from one folder to another

Posted 08 November 2017 - 09:08 AM

View PostSkydiver, on 07 November 2017 - 06:18 AM, said:

My recommendation is get a full list of files and directories of the from_path just once and store it in memory as a tree a best, or a list at worse, and then do all searches against local memory rather than repeatedly hitting the disk across the network.

Can you show it with some code?
Was This Post Helpful? 0
  • +
  • -

#23 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13485
  • View blog
  • Posts: 53,843
  • Joined: 12-June 08

Re: Struggling to move files from one folder to another

Posted 08 November 2017 - 09:40 AM

FYI - someone did the work for the OP over here.
Was This Post Helpful? 2
  • +
  • -

#24 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1375
  • View blog
  • Posts: 4,309
  • Joined: 20-March 10

Re: Struggling to move files from one folder to another

Posted 09 November 2017 - 03:15 AM

Oh,

Lazy OP....

Bad OP.....

:)
Was This Post Helpful? 0
  • +
  • -

#25 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1233
  • View blog
  • Posts: 1,759
  • Joined: 24-August 11

Re: Struggling to move files from one folder to another

Posted 14 November 2017 - 11:10 AM

I know for a fact that there are at least two snippets in the library here that deal with copying files from a directory and its subdirectories (I wrote one of them)...
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2