Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I'm trying to create a little program that will rename all the *.MSG file to *.EDI file and then copy those *.EDI file to a archive folder and then move all the .EDI to a specific folder.

for now I only tried to rename and the move but it's not working and I don't know why.

I would really appreciate your help.

thanks

Marc.

What I have tried:

static void Main(string[] args)
        {

            string souceFile = @"\\PATH TO FOLDER\";
            string targetFile = @"\\PATH TO FOLDER\";

            DirectoryInfo d = new DirectoryInfo(@"\\PATH TO FOLDER\");
            FileInfo[] infos = d.GetFiles();
            foreach (string filename in Directory.GetFiles(@"\\PATH TO FOLDER\", "*.MSG", SearchOption.TopDirectoryOnly))
            {
                Path.ChangeExtension(filename, ".EDI");
            }

                        
            foreach (string filename in Directory.GetFiles(@"\\PATH TO FOLDER\", "*.EDI", SearchOption.TopDirectoryOnly))
            {
                    System.IO.File.Move(souceFile, targetFile);
            }

        }
Posted
Updated 11-Sep-20 21:34pm
Comments
Wendelius 11-Sep-20 14:11pm    
What is the error you get?
Marc_Moi 11-Sep-20 14:14pm    
Hi,
thanks for your reply.

it's just not working I don't get any error. the file stay there without any modification and it's not moved.

thanks
Marc
PIEBALDconsult 11-Sep-20 16:27pm    
System.IO.File.Move(souceFile, targetFile);
I think the problem is that you don't want souceFile there.

If executing the code doesn't give any errors, you should utilize the debugger to investigate what goes wrong. Perhaps the path is invalid and the loops don't find anything etc.

To use the debugger, execute the code line-by-line and investigate the variables, have a look at Tutorial: Debug C# code - Visual Studio | Microsoft Docs[^]

ADDDITION
Looking at the examples, in case you use verbatim strings (@) ensure that you don't escape folder separators. In other words either use
string souceFile = @"C:\TEMP\";

or
string souceFile = "C:\\TEMP\\";
 
Share this answer
 
v2
Comments
Marc_Moi 11-Sep-20 14:26pm    
Hi Thanks for that I found this in my code when debugging.

System.IO.IOException: 'Logon failure: unknown user name or bad password.
when I get to the line : FileInfo[] infos = d.GetFiles();

but I don't understand why on the machine where's I'm running the program I do have access to this folder.
Wendelius 11-Sep-20 14:48pm    
A common reason for that exception is that the user running the application does not have access to the folder specified.

The important thing is the account that is running the code. For example if the code is run from a service under local system account then it has no access to network resources even though you may have those privileges when logged into the same computer using a different account. The same could happen also for example with ASP.NET applications if the service is run using an account without proper privileges or for example when using remote debugging.

So using appropriate account is one option and another option could be to impersonate user in code, see c# - Logon failure: unknown user name or bad password.error while accessing other server - Stack Overflow[^]
Marc_Moi 11-Sep-20 14:38pm    
Yes where you see @"\\Path to folder\" there's \\ at the beginning cause it,s a network folder so @"\\1.1.1.1\abc\" this is why.
BillWoodruff 12-Sep-20 6:42am    
+5 a good example of teaching skills that are needed, rather than doing the OP's work "for them" !
Wendelius 16-Sep-20 0:12am    
Thanks!
Add code to check if file and directory paths are valid, and/or already exist:

in System.IO:

File.Exists: [^]

Directory.Exists: [^]

You can use a call to Path.ChangeExtension as the second argument in the call to File.Move: File.Move(src, Path.ChangeExtension(src, ".xxx"));

for discussion of checking on a network:
Quote:
When you use System.IO.Directory.Exists, it only lets you know that it couldn't find the directory, but this could be because the directory doesn't actually exist or because the user doesn't have sufficient access rights to the directory.
[^]
 
Share this answer
 
v2
Comments
Wendelius 16-Sep-20 0:13am    
Very good point.
BillWoodruff 16-Sep-20 6:06am    
Thank you !
C#
static void Main(string[] args)
        {

        string souceFile = @"\\PATH TO FOLDER\";
        string targetFile = @"\\PATH TO FOLDER\";

...

        foreach (string filename in Directory.GetFiles(@"\\PATH TO FOLDER\", "*.EDI", SearchOption.TopDirectoryOnly))
        {
                // First problem is that souceFile and targetFile have same value.
                // This means that you try to move a file to same place.
                System.IO.File.Move(souceFile, targetFile);
                // Second problem is that the file name is not in the parameters of move.
        }

    }

Are you sure that @"\\PATH TO FOLDER\" is real path ?
 
Share this answer
 
v2
Comments
Marc_Moi 12-Sep-20 7:39am    
I wrote path to folder has reference there’s an actual path to a network path.
Patrice T 12-Sep-20 7:49am    
Advice: Show some realistic code to get useful answers.
Marc_Moi 12-Sep-20 7:59am    
What would be the difference even if I put \\1.1.1.1\testfolder\in you can imagine a path to a folder. All the other person did understand it was has exemple but the code was the same except the path.
Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900