Struggling to move files from one folder to another

• (2 Pages)
• 1
• 2

24 Replies - 1318 Views - Last Post: 14 November 2017 - 11:10 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=407370&amp;s=8faab35d2d844b1f31752e0674f769a3&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 Whateva_

Reputation: 0
• Posts: 56
• Joined: 28-August 16

Struggling to move files from one folder to another

Posted 03 November 2017 - 08:35 AM

I want copy all *.tif files from a location (from_path) to another location (to_path) using the following conditions.
The path from_path has folders in it and each folder can contain multiple tif files.
The path to_path also has multiple folders in it and each folder has a subfolder named xml and inside it is a xml file.
The program should first get the name of the xml file in to_path, then search for a folder with that same name in from_path and if there is a folder of that same name then copy all tif files from that folder and paste it inside folder named xml in to_path, where the xml file is.
I've tried something like
string to_path=textBox1.Text;
string from_path=textBox2.Text;
DirectoryInfo diCopyFrom=new DirectoryInfo(from_path);
DirectoryInfo diCopyTo=new DirectoryInfo(to_path);
FileInfo[] tifFiles = diCopyFrom.GetFiles("*.tif");
FileInfo[] xmlFiles = diCopyFrom.GetFiles("*.xml");
foreach (FileInfo xmlFile in xmlFiles)
{
var xmlFilePath=Directory.GetDirectories(from_path, xmlFile.Name, SearchOption.AllDirectories).First();
foreach (FileInfo tifFile in tifFiles) {
string loc = Path.Combine(to_path, xmlFile.Name);
string f_name = Path.Combine(xmlFilePath, xmlFile.Name);
File.Copy(loc, f_name, true);
}
}
MessageBox.Show("Finished");


I did not get any compile errors, but it does not work.
Where did I do wrong?

This post has been edited by Whateva_: 03 November 2017 - 08:35 AM

Is This A Good Question/Topic? 0

Replies To: Struggling to move files from one folder to another

#2 andrewsw

• blow up my boots

Reputation: 6492
• Posts: 26,267
• Joined: 12-December 12

Re: Struggling to move files from one folder to another

Posted 03 November 2017 - 09:10 AM

Do you get the message "Finished" indicating that the code ran?

If there is no error you need to be able to step through your code and see what is happening. There is a debugging tutorial linked in my signature below. Knowing how to debug and step through your code is essential knowledge.

#3 Whateva_

Reputation: 0
• Posts: 56
• Joined: 28-August 16

Re: Struggling to move files from one folder to another

Posted 03 November 2017 - 09:17 AM

andrewsw, on 03 November 2017 - 09:10 AM, said:

Do you get the message "Finished" indicating that the code ran?

Yes I did get it.
I think there is some logic problem in the code, maybe its not getting the right files(from the desired folder) or something like that, I could not find out...
BTW, I'm using sharpdevelop IDE..

This post has been edited by Whateva_: 03 November 2017 - 09:18 AM

#4 Skydiver

• Code herder

Reputation: 6009
• Posts: 20,649
• Joined: 05-May 12

Re: Struggling to move files from one folder to another

Posted 03 November 2017 - 09:41 AM

From what I can see doing a quick Google search, SharpDevelop also lets you set breakpoints and debug your code like any good modern IDE should allow you to do.

#5 Skydiver

• Code herder

Reputation: 6009
• Posts: 20,649
• Joined: 05-May 12

Re: Struggling to move files from one folder to another

Posted 03 November 2017 - 09:52 AM

Anyway this statement:

Whateva_, on 03 November 2017 - 11:35 AM, said:

The path from_path has folders in it and each folder can contain multiple tif files.
:
... folder with that same name in from_path and if there is a folder of that same name then copy all tif files from that folder ...

Conflicts with what you implemented. You are collecting all the .TIF files found in the from_path folder, not the .TIF files in the folder with the same name as the xml file.

I suspect that tifFiles is an empty array and so the inner foreach does nothing.

#6 Whateva_

Reputation: 0
• Posts: 56
• Joined: 28-August 16

Re: Struggling to move files from one folder to another

Posted 03 November 2017 - 10:20 AM

I just realized that there is no need for the array tifFiles as I dont want all tif files from from_path to be copied but the folder with the same name as the xml file, only the tif files from that folder instead.
But now I'm even more confused...Does anyone have a better procedure to do this? />

This post has been edited by Whateva_: 03 November 2017 - 10:21 AM

#7 Dankwansere

• D.I.C Regular

Reputation: 82
• Posts: 256
• Joined: 09-November 09

Re: Struggling to move files from one folder to another

Posted 03 November 2017 - 11:54 AM

You can achieve it by updating your logic,

1. Get the xml file in the "to_path" that you require

2. search the the "from_path" for that specific folder
- If no folder exist - No copy needed
- If folder exist but it's empty - No copy needed

3. if folder found and contains .tiff files, you can use the "Directory" class API and call the "GetFiles()" Method. It will return all the files based on your specified criteria, and store them into an array.
Directory Class API

4. Iterate through the array and use the "File" Class to move or copy the files to the "to_path" specified folder
File Class API

I hope this helps

This post has been edited by Dankwansere: 03 November 2017 - 11:55 AM

#8 tlhIntoq

• Xamarin Cert. Dev.

Reputation: 6517
• Posts: 14,405
• Joined: 02-June 10

Re: Struggling to move files from one folder to another

Posted 03 November 2017 - 03:18 PM

> I think there is some logic problem in the code, maybe its not getting the right files(from the desired folder) or something like that, I could not find out...

This tells me that you don't know how to debug... or how to place breakpoints and look at the run-time values. If you didn't, you wouldn't be guessing about whether or not its getting valid file names.

I urge you to step aside for a couple hours and do some debugging tutorials. Then return. 2 hours learning this vital skill will save you 20 hours of guessing on this problem alone.

Q: I do x and y happens which I didn't expect but I don't know how to figure out why. How do I debug and find my problem?
A:

See FAQ # 2 "How do I debug my program?"

What does this error message mean?
FAQ 2: How do I debug
FAQ 6: How do I make Class1/Form1 talk to Class2/Form2

FAQ (Frequently Asked Questions - Updated July 2016
Spoiler

#9 snoopy11

• Engineering ● Software

Reputation: 1437
• Posts: 4,625
• Joined: 20-March 10

Re: Struggling to move files from one folder to another

Posted 04 November 2017 - 06:51 AM

Well following your English Logic rather than your program Logic you need at least two if statements and

a call to CreateDirectory at some point... in your code I would try something like this I was using png files hence the refs to png.

DirectoryInfo diCopyFrom = new DirectoryInfo(from_path);
DirectoryInfo diCopyTo = new DirectoryInfo(to_path);
FileInfo[] pngFiles = diCopyFrom.GetFiles("*.png");

FileInfo[] xmlFiles = diCopyTo.GetFiles("*.xml");

foreach (FileInfo xmlFile in xmlFiles)

{
var xmlDir = Path.GetFileNameWithoutExtension(xmlFile.Name);

var xmlFilePath = Directory.GetDirectories(from_path,xmlDir);
if (Directory.Exists(xmlFilePath.ToString()))
{
foreach (FileInfo pngFile in pngFiles)
{

string loc = Path.Combine(from_path, pngFile.Name);
string f_name = Path.Combine(to_path + xmlDir, pngFile.Name);

if (!Directory.Exists(to_path + xmlDir))
Directory.CreateDirectory(to_path + xmlDir);

File.Copy(loc, f_name, true);

}
}

}



#10 Whateva_

Reputation: 0
• Posts: 56
• Joined: 28-August 16

Re: Struggling to move files from one folder to another

Posted 04 November 2017 - 08:49 AM

Hi snoopy11,
Did the code you posted work on your computer?

In my case, the contents of from_path looks like below
"E:\from_path\133456\133456-fig-1.tif, \133456-fig-2.tif ..."
"E:\from_path\136406\136406-table-1.tif, 136406-fig-1.tif ..."

... ... ....

etc.


and the contents of to_path look like this
D:\to_path\PL132_SPIEV9066_3_62\00062_psisdg9066_90661R\Assets\133456.xml

D:\to_path\PL132_SPIEV9066_4_69\00069_psisdg9069_90660R\Assets\136406.xml

... ...

etc.


How do I use the Path.Combine() method for my case?
I'm trying to copy the tif files to the Assets folder in to_path...

This post has been edited by Whateva_: 04 November 2017 - 08:53 AM

#11 snoopy11

• Engineering ● Software

Reputation: 1437
• Posts: 4,625
• Joined: 20-March 10

Re: Struggling to move files from one folder to another

Posted 04 November 2017 - 09:40 AM

Yes it worked it took png's in subfolders marked by the names of .xml files in another location and transferred from png location to xml location creating directory for the transfer if required.

This post has been edited by snoopy11: 04 November 2017 - 09:40 AM

#12 Whateva_

Reputation: 0
• Posts: 56
• Joined: 28-August 16

Re: Struggling to move files from one folder to another

Posted 04 November 2017 - 10:21 PM

I tried to revise the code and came up with this(the folders in from_path only contain tif files and no other files)
string to_path=textBox1.Text;
string from_path=textBox2.Text;
DirectoryInfo diCopyFrom = new DirectoryInfo(from_path);
DirectoryInfo diCopyTo = new DirectoryInfo(to_path);
FileInfo[] xmlFiles = diCopyTo.GetFiles("*.xml",SearchOption.AllDirectories);
foreach (FileInfo xmlFile in xmlFiles)
{
var xmlDir = Path.GetFileNameWithoutExtension(xmlFile.Name);
var targetLoc=Path.GetFullPath(xmlFile.Name);
var tifLoc = Directory.GetDirectories(from_path,xmlDir,,SearchOption.AllDirectories).FirstOrDefault();
if (Directory.Exists(tifLoc.ToString()))
File.Copy(tifLoc, targetLoc, true);
}
MessageBox.Show("Finished");


Also the targetLoc is showing path of the file in my programs bin directory.

This post has been edited by Whateva_: 04 November 2017 - 10:26 PM

#13 snoopy11

• Engineering ● Software

Reputation: 1437
• Posts: 4,625
• Joined: 20-March 10

Re: Struggling to move files from one folder to another

Posted 05 November 2017 - 12:23 AM

Well,

You cant change the code that much and expect it to work and if you want to search all sub folders you would need to do something like this..

 DirectoryInfo diCopyTo = new DirectoryInfo(to_path);

FileInfo[] xmlFiles = diCopyTo.GetFiles("*.xml");

foreach (FileInfo xmlFile in xmlFiles)

{
var xmlDir = Path.GetFileNameWithoutExtension(xmlFile.Name);

var xmlFilePath = Directory.EnumerateDirectories(from_path, xmlDir,SearchOption.AllDirectories);
foreach (var filePath in xmlFilePath)
{
DirectoryInfo diCopyFrom = new DirectoryInfo(filePath);

FileInfo[] pngFiles = diCopyFrom.GetFiles("*.png");
foreach (FileInfo pngFile in pngFiles)
{

string loc = Path.Combine(filePath, pngFile.Name);
string f_name = Path.Combine(to_path + xmlDir, pngFile.Name);

if (!Directory.Exists(to_path + xmlDir))
Directory.CreateDirectory(to_path + xmlDir);

File.Copy(loc, f_name, true);

}
}
}



If you change the "*.png" bit to "*.tif" it will work as is no need to change anything else.

#14 Whateva_

Reputation: 0
• Posts: 56
• Joined: 28-August 16

Re: Struggling to move files from one folder to another

Posted 05 November 2017 - 01:02 AM

Hi snoopy11,

You still don't seem to understand where I'm trying to put the TIF files
I need to copy the TIF files to the folder which contains the xml in to_path
Each folder in to_path are like
PL132_SPIEV9066_3_62\00062_psisdg9066_90661R\Assets\133456.xml
PL132_SPIEV9066_1_65\00065_psisdg9016_90662R\Assets\133457.xml

I need to copy the TIF files inside the Asstes sub-folder of each main folder as it is the folder that contains the xml file.
Also I don't need to create a folder to put the TIF files in and I need to check whether the directory exist in the from_path not in to_path bcoz I'm copying the TIF files from the folder (in from_path) which has the same names as the xml file in to_path

This post has been edited by Whateva_: 05 November 2017 - 01:04 AM

#15 snoopy11

• Engineering ● Software

Reputation: 1437
• Posts: 4,625
• Joined: 20-March 10

Re: Struggling to move files from one folder to another

Posted 05 November 2017 - 01:26 AM

Well,

The principle is still the same and your condition as you explain it is a bit complex...

so you have a from path that looks like \...\...\13456\mytiff.tif
and you have a to path that looks like \...\...\Assets\

Yes.. So...

DirectoryInfo diCopyTo = new DirectoryInfo(to_path);

FileInfo[] xmlFiles = diCopyTo.GetFiles("*.xml");

foreach (FileInfo xmlFile in xmlFiles)

{
var xmlDir = Path.GetFileNameWithoutExtension(xmlFile.Name);

var xmlFilePath = Directory.EnumerateDirectories(from_path, xmlDir,SearchOption.AllDirectories);
foreach (var filePath in xmlFilePath)
{
DirectoryInfo diCopyFrom = new DirectoryInfo(filePath);

FileInfo[] pngFiles = diCopyFrom.GetFiles("*.png");
foreach (FileInfo pngFile in pngFiles)
{

string loc = Path.Combine(filePath, pngFile.Name);
string f_name = Path.Combine(to_path, pngFile.Name);

if (Directory.Exists(to_path))
File.Copy(loc, f_name, true);

}
}
}



to get the to_path you need to enumerate all the directories in the target folder searching for "Assets"

similar to what we do here

var xmltargetPath = Directory.EnumerateDirectories(target_path, "Assets",SearchOption.AllDirectories);


then using a foreach search all enumerations. As I did...

like

foreach (var targetfilePath in xmltargetPath)
{
`

This post has been edited by snoopy11: 05 November 2017 - 06:33 AM

• (2 Pages)
• 1
• 2

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }