Reputation: 71
I have a folder that contains multiple text files and I would like to read them to get the contents of some lines and then move that file to a "backup" folder.
The path of my "backup" folder is :
\SRVWEB001\spool_interface\PVCHARGE\Backup\
The path that contains my text files :
\SRVWEB001\spool_interface\PVCHARGE\UTS600\
I managed to read the lines of a file and store them in a List but when I want to copy my text files in the "backup" folder I can not copy them because I have this error : "Unable to create an existing file." (System.IO.IO Exception) except that my file does not yet exist in the folder ..
This is my code :
[WebMethod]
public string enTete()
{
// Stocke une à une les lignes du fichier
List<string> allLines = new List<string>();
// Stocke l'en-tête
List<string> enTete = new List<string>();
// Chemin du dossier contenant les fichiers
string dirPath = @"\\SRVWEB001\spool_interface\PVCHARGE\UTS600\";
DirectoryInfo d = new DirectoryInfo(dirPath);
// Si il existe des fichiers txt dans le dossier
if (Directory.GetFiles(dirPath, "*.txt").Length != 0)
{
// Pour chaque fichier
foreach (var fichier in d.GetFiles("*.txt"))
{
// Lis le fichier
using (FileStream fs = fichier.OpenRead())
{
byte[] b = new byte[fs.Length];
int counter = 0;
string line;
StreamReader file = new StreamReader(fs, System.Text.Encoding.GetEncoding("iso-8859-1"));
// Récupération de chaque ligne dans la List<string> allLines
while ((line = file.ReadLine()) != null)
{
allLines.Add(line);
counter++;
}
// Fermeture du fichier
file.Close();
// Dossier "Backup"
string backupPath = @"\\SRVWEB001\spool_interface\PVCHARGE\Backup\";
DirectoryInfo d2 = new DirectoryInfo(backupPath);
// Fichier à déplacer
string filePath = dirPath + fichier.ToString();
if (Directory.Exists(dirPath))
{
string[] files = Directory.GetFiles(dirPath);
foreach (string s in files)
{
// Déplace le fichier dans le dossier "Backup"
File.Move(s, backupPath);
}
}
else
{
Console.WriteLine("Source path does not exist!");
}
// Récupération des colonnes de l'en-tête dans un tableau (correspond à la ligne 105)
char[] separator = new char[] { '\t' };
string[] colonnes = allLines[105].Split(separator, StringSplitOptions.None);
// Affichage des colonnes
return colonnes[0]+" "+colonnes[1]+" "+colonnes[2]+" "+colonnes[3]+" "+colonnes[4]+" "+colonnes[5]+" "+colonnes[6] + " " +colonnes[7] + " " +colonnes[8] + " " +colonnes[9]+" "+ colonnes[10] + " " + colonnes[11] + " " + colonnes[12] + " " + colonnes[13] + " " + colonnes[14];
}
}
return "Succès";
}
else
{
return "Pas de fichier texte";
}
}
Thanks in advance for your help
Upvotes: 0
Views: 481
Reputation: 6773
The second parameter of File.Move needs to be the path & filename that you want to move the file to. The reason you are getting the exception is that without including the filename you are trying to move the file & create it with the same name as your backup folder.
Upvotes: 0
Reputation: 9244
You need to provide a filename to File.Move - not a directoy name. Instead of:
File.Move(s, backupPath);
try
File.Move(s, Path.Combine(backupPath, Path.GetFilename(s)));
Upvotes: 3