File.Move/create just won't work!
-
Hello all, got myself a confusing mess here. I am going to provide as much information as possible, just let me know if something doesn't make sense. On load, I have a file check and backup that is executed. The backup, doesn't work at all (the Move) and the create, only creates for the first item in the cluster. Here is the code.
//This pulls the current selected item in combo box to the string
string instText = comboInst.GetItemText(comboInst.SelectedItem);//Items in the combobox have a leading 4 digit number so this
//string simply grabs those 4 digits into a string
string folder = instText.Substring(0, 4);//This is the path where each item's settings file is located
string path = @"C:\Nightly\Institutions\" + folder + "\\";//This names each email accordingly
string email = folder + "Email.txt";//This is the source path for the email file if it exists for
//the backup process
string sourceFile = @"C:\Nightly\Institutions\" + folder + "\\" + folder + "Email.txt";//this is where the backups go to... it's the above path
//with an archive directory
string destinationFile = @"C:\Nightly\Institutions\" + folder + @"\Archive\";//now I want to backup a file if it exists, if it doesn't create
//the file...foreach (string file in settingsFiles)
{
if (File.Exists(path + email))
{
File.Move(sourceFile, destinationFile);
}
if (!File.Exists(path + email))
{
Directory.CreateDirectory(path);
using (File.Create(path + email)) { /*Just creating a path*/ }
}What happens is, the file does NOT get moved if the file exists, and when it creates the file if a file doesn't exist, it only does it for the first item in the list, not each as you would expect a foreach statement to process. If anyone could offer some insight on the wrongdoings of my ways I would appreciate it. Thanks!
-
Hello all, got myself a confusing mess here. I am going to provide as much information as possible, just let me know if something doesn't make sense. On load, I have a file check and backup that is executed. The backup, doesn't work at all (the Move) and the create, only creates for the first item in the cluster. Here is the code.
//This pulls the current selected item in combo box to the string
string instText = comboInst.GetItemText(comboInst.SelectedItem);//Items in the combobox have a leading 4 digit number so this
//string simply grabs those 4 digits into a string
string folder = instText.Substring(0, 4);//This is the path where each item's settings file is located
string path = @"C:\Nightly\Institutions\" + folder + "\\";//This names each email accordingly
string email = folder + "Email.txt";//This is the source path for the email file if it exists for
//the backup process
string sourceFile = @"C:\Nightly\Institutions\" + folder + "\\" + folder + "Email.txt";//this is where the backups go to... it's the above path
//with an archive directory
string destinationFile = @"C:\Nightly\Institutions\" + folder + @"\Archive\";//now I want to backup a file if it exists, if it doesn't create
//the file...foreach (string file in settingsFiles)
{
if (File.Exists(path + email))
{
File.Move(sourceFile, destinationFile);
}
if (!File.Exists(path + email))
{
Directory.CreateDirectory(path);
using (File.Create(path + email)) { /*Just creating a path*/ }
}What happens is, the file does NOT get moved if the file exists, and when it creates the file if a file doesn't exist, it only does it for the first item in the list, not each as you would expect a foreach statement to process. If anyone could offer some insight on the wrongdoings of my ways I would appreciate it. Thanks!
If you run the code you've shown it will throw exceptions at the points where it is failing - examine those exceptions and you will see some of where you're going wrong. Other points - You need to check the archive directory exists and doesn't contain a file with that name before calling Move I would append the filename to your
destinationFile
string - not just the path Your loop is going to keep creating/moving the same fileDave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
Hello all, got myself a confusing mess here. I am going to provide as much information as possible, just let me know if something doesn't make sense. On load, I have a file check and backup that is executed. The backup, doesn't work at all (the Move) and the create, only creates for the first item in the cluster. Here is the code.
//This pulls the current selected item in combo box to the string
string instText = comboInst.GetItemText(comboInst.SelectedItem);//Items in the combobox have a leading 4 digit number so this
//string simply grabs those 4 digits into a string
string folder = instText.Substring(0, 4);//This is the path where each item's settings file is located
string path = @"C:\Nightly\Institutions\" + folder + "\\";//This names each email accordingly
string email = folder + "Email.txt";//This is the source path for the email file if it exists for
//the backup process
string sourceFile = @"C:\Nightly\Institutions\" + folder + "\\" + folder + "Email.txt";//this is where the backups go to... it's the above path
//with an archive directory
string destinationFile = @"C:\Nightly\Institutions\" + folder + @"\Archive\";//now I want to backup a file if it exists, if it doesn't create
//the file...foreach (string file in settingsFiles)
{
if (File.Exists(path + email))
{
File.Move(sourceFile, destinationFile);
}
if (!File.Exists(path + email))
{
Directory.CreateDirectory(path);
using (File.Create(path + email)) { /*Just creating a path*/ }
}What happens is, the file does NOT get moved if the file exists, and when it creates the file if a file doesn't exist, it only does it for the first item in the list, not each as you would expect a foreach statement to process. If anyone could offer some insight on the wrongdoings of my ways I would appreciate it. Thanks!
Put your code in a
try-catch
block or step through your code to find the exact location of your error. Once you fix it, your files should move from one location to the other.WP Apps - Color Search | Arctic | XKCD | Sound Meter | Speed Dial
-
Hello all, got myself a confusing mess here. I am going to provide as much information as possible, just let me know if something doesn't make sense. On load, I have a file check and backup that is executed. The backup, doesn't work at all (the Move) and the create, only creates for the first item in the cluster. Here is the code.
//This pulls the current selected item in combo box to the string
string instText = comboInst.GetItemText(comboInst.SelectedItem);//Items in the combobox have a leading 4 digit number so this
//string simply grabs those 4 digits into a string
string folder = instText.Substring(0, 4);//This is the path where each item's settings file is located
string path = @"C:\Nightly\Institutions\" + folder + "\\";//This names each email accordingly
string email = folder + "Email.txt";//This is the source path for the email file if it exists for
//the backup process
string sourceFile = @"C:\Nightly\Institutions\" + folder + "\\" + folder + "Email.txt";//this is where the backups go to... it's the above path
//with an archive directory
string destinationFile = @"C:\Nightly\Institutions\" + folder + @"\Archive\";//now I want to backup a file if it exists, if it doesn't create
//the file...foreach (string file in settingsFiles)
{
if (File.Exists(path + email))
{
File.Move(sourceFile, destinationFile);
}
if (!File.Exists(path + email))
{
Directory.CreateDirectory(path);
using (File.Create(path + email)) { /*Just creating a path*/ }
}What happens is, the file does NOT get moved if the file exists, and when it creates the file if a file doesn't exist, it only does it for the first item in the list, not each as you would expect a foreach statement to process. If anyone could offer some insight on the wrongdoings of my ways I would appreciate it. Thanks!
-
What is in
settingsFiles
and why do you have a loop which uses the same file name each time through? There is also a bit of duplication in your paths in the above:sourceFile
andpath + email
both evaluate to the same string.Use the best guess
settingsFile is a string of settings files, no way to know how many there will be so that is why it loops. Here is the string.
string[] settingsFiles = Directory.GetFiles(@"C:\Nightly\Institutions\", "settings.ini", SearchOption.AllDirectories);
-
settingsFile is a string of settings files, no way to know how many there will be so that is why it loops. Here is the string.
string[] settingsFiles = Directory.GetFiles(@"C:\Nightly\Institutions\", "settings.ini", SearchOption.AllDirectories);
-
Yes, but why are you using it to create a loop when it has nothing to do with the file that you are trying to move?
Use the best guess
The loop, I think, has everything to do with the file. How else would I know what email files are in each directory? For each *email.txt file in the directories specified do this { If the file exists in the directory, move it } { If the file does NOT exist, create it } ^ so for each file it sees in the string, it should be doing this. So if I have this structure: (Folder) (Email Exists?) 1 Y 2 Y 3 N 4 N 5 Y the intent is to move the file from folder 1 to folder 1\Archive, then create a new one, same for folder 2, then folder 3 doesn't have one so it just creates it, nothing to backup there. Repeat for 4. then backup the file in folder 5 to folder 5\Archive. Hope that helps with my logic.
-
The loop, I think, has everything to do with the file. How else would I know what email files are in each directory? For each *email.txt file in the directories specified do this { If the file exists in the directory, move it } { If the file does NOT exist, create it } ^ so for each file it sees in the string, it should be doing this. So if I have this structure: (Folder) (Email Exists?) 1 Y 2 Y 3 N 4 N 5 Y the intent is to move the file from folder 1 to folder 1\Archive, then create a new one, same for folder 2, then folder 3 doesn't have one so it just creates it, nothing to backup there. Repeat for 4. then backup the file in folder 5 to folder 5\Archive. Hope that helps with my logic.
-
Yes, but look at the code inside the loop. It changes nothing, but uses the same path and file names every time round, nor does it use the content of the item taken from the list.
Use the best guess
Richard I really appreciate you pointing me in the right direction, I took another look at my logic and you're right, it didn't make sense. It sounded good in my head, but I drew it out on paper to see what I was actually trying to accomplish and it didn't work. So here's what I did, and I am getting the looping result I am after, and I think I can go from here. Take a look. same strings as above, plus this, different for each
foreach (string file in settingsFiles)
{
IniFile ini = new IniFile(file);
string settingNum = ini.IniReadValue("info", "settingNumber");if (file.Contains(settingNum)) { MessageBox.Show("This is setting number: " + settingNum); continue; } }
I have a file call setting.ini and in each I have the category [info] and a generic setting called settingNumber. NOW on form load it does a message box that says "This is setting number: 1, then 2, then 3, then 4. Now to just play with it to do a check to see if the email file exists, if it does, move it, then it will create an email file for each direcotory that contains a settings file. Thanks! This is the best way to learn, being pointed in the right direction, by someone who isn't an asshole haha. I haven't really had any experiences myself, but sometimes when I use my good friend google, you see some of the responses people give and they are just straight up buttheads. Thanks again :P
-
Yes, but look at the code inside the loop. It changes nothing, but uses the same path and file names every time round, nor does it use the content of the item taken from the list.
Use the best guess
and the finished product! It needs some cleaning up, but for functionalities sake, I am happy with it. Thanks again.
foreach (string file in settingsFiles)
{
IniFile ini = new IniFile(file);
string settingNum = ini.IniReadValue("info", "settingNumber");if (file.Contains(settingNum)) { if (File.Exists(@"C:\\Nightly\\Institutions\\" + settingNum + "\\\\" + settingNum + email)) { MessageBox.Show("The file Exists for: " + settingNum); File.Move(@"C:\\Nightly\\Institutions\\" + settingNum + "\\\\" + settingNum + email, @"C:\\Nightly\\Institutions\\" + settingNum + "\\\\Archive\\\\" + Parameters.DateOrTime.fileDate.ToString(Parameters.DateOrTime.formatFileDate) + " - " + settingNum + email); } using (File.Create(@"C:\\Nightly\\Institutions\\" + settingNum + "\\\\" + settingNum + email)) { /\*Just creating a file\*/ } } }
so a little background on what this does - it checks to see if a file already exists, if it does it will move it to the archive folder in the directory the file is located, and renames it to today's date - email.txt. Then it recreates a blank email to be written to in the future.
-
and the finished product! It needs some cleaning up, but for functionalities sake, I am happy with it. Thanks again.
foreach (string file in settingsFiles)
{
IniFile ini = new IniFile(file);
string settingNum = ini.IniReadValue("info", "settingNumber");if (file.Contains(settingNum)) { if (File.Exists(@"C:\\Nightly\\Institutions\\" + settingNum + "\\\\" + settingNum + email)) { MessageBox.Show("The file Exists for: " + settingNum); File.Move(@"C:\\Nightly\\Institutions\\" + settingNum + "\\\\" + settingNum + email, @"C:\\Nightly\\Institutions\\" + settingNum + "\\\\Archive\\\\" + Parameters.DateOrTime.fileDate.ToString(Parameters.DateOrTime.formatFileDate) + " - " + settingNum + email); } using (File.Create(@"C:\\Nightly\\Institutions\\" + settingNum + "\\\\" + settingNum + email)) { /\*Just creating a file\*/ } } }
so a little background on what this does - it checks to see if a file already exists, if it does it will move it to the archive folder in the directory the file is located, and renames it to today's date - email.txt. Then it recreates a blank email to be written to in the future.