Note that there are some explanatory texts on larger screens.

plurals
  1. POChecking Value of array against array inside function C#
    text
    copied!<p>I am new to C# and I am sureI am writing a program that moves files if they meet a certain time criteria due to limitations of an existing 3rd party program. The program works in its current capacity however I want to have it check for files with the same name in the destination and change the name of the file that is being moved, so the program does not error out if there are already existing files with the same name. Also some fname and target are declared in the custom class GloDir and are used in other functions as well.</p> <p>This works but does not check for files in the target;</p> <pre><code>DirectoryInfo sourceInfo = new DirectoryInfo(GloDir.fname); FileInfo[] sourceFiles = sourceInfo.GetFiles("*.zip"); //creates array of all files ending in .zip DirectoryInfo destInfo = new DirectoryInfo(GloDir.target); FileInfo[] destFiles = destInfo.GetFiles("*.zip"); if (sourceFiles.Length == 0) // check to see if files are present. if not die. { return; } foreach (var sFileInfo in sourceFiles) { string sFip = sFileInfo.ToString(); //file info to string string fileName = System.IO.Path.GetFileName(sFip); //get file name string sourceFile = System.IO.Path.Combine(GloDir.fname, fileName); //Full filename and path string targetFile = System.IO.Path.Combine(GloDir.target, fileName); //New Target and Path DateTime createdOn = File.GetCreationTime(sourceFile); //get file creation time DateTime rightNow = DateTime.Now; //Variable for this moment var difference = rightNow - createdOn; //Different between now and creation var minutos = difference.TotalMinutes; //turn difference into minutes //If time between creation and now is greater than 120 minutes move file to new directory if (minutos &gt;= 1) { //Console.Write(minutos + " Old - Moving! -"); //debug console msg System.IO.File.Move(sourceFile, targetFile); //move file to new directory //System.Threading.Thread.Sleep(1555); //debug sleep } } </code></pre> <p>To check for files in the target i changed it to add a If statement that checks the directory for any files then a nested foreach loop that compares the values and that also works. The only issues is when trying to use the string declard within the loop or if statement I copied the code with the addition below. I also left in some of the console writes and sleeps that allow me to watch the program function those will not be in the final code;</p> <pre><code>DirectoryInfo sourceInfo = new DirectoryInfo(GloDir.fname); FileInfo[] sourceFiles = sourceInfo.GetFiles("*.zip"); //creates array of all files ending in .zip DirectoryInfo destInfo = new DirectoryInfo(GloDir.target); FileInfo[] destFiles = destInfo.GetFiles("*.zip"); if (sourceFiles.Length == 0) // check to see if files are present. if not die. { //Console.Write("no files present"); //debug console msg return; } foreach (var sFileInfo in sourceFiles) { string sFip = sFileInfo.ToString(); //file info to string string fileName = System.IO.Path.GetFileName(sFip); //get file name if (destFiles.Length != 0) { foreach (var dFileInfo in destFiles) { string dFip = dFileInfo.ToString(); //file info to string string dfileName = System.IO.Path.GetFileName(dFip); //get file name if (dfileName == fileName) { string newFilename = "Duplicate" + fileName; Console.Write(newFilename + "dup change name"); System.Threading.Thread.Sleep(1000); } else { string newFilename = fileName; Console.Write(newFilename + "dest files no duplicate"); System.Threading.Thread.Sleep(1000); } } } if (destFiles.Length == 0) { string newFilename = fileName; } string sourceFile = System.IO.Path.Combine(GloDir.fname, fileName); //Full filename and path string targetFile = System.IO.Path.Combine(GloDir.target, newFilename); //New Target and Path DateTime createdOn = File.GetCreationTime(sourceFile); //get file creation time DateTime rightNow = DateTime.Now; //Variable for this moment var difference = rightNow - createdOn; //Different between now and creation var minutos = difference.TotalMinutes; //turn difference into minutes //If time between creation and now is greater than 120 minutes move file to new directory if (minutos &gt;= 1) { System.IO.File.Move(sourceFile, targetFile); //move file to new directory } } </code></pre> <p>Every time it stops and says that the newFilename variable does not exist in the current context, even though it should be created based on the conditions of the destination directory. Do i need to delcare the string newFilename differently so that it can be seen outside of the if statement and loop? I am sure this is a simple mistake or one that I have not learned yet. This may also be an issue with variable scope but i would not think so considering the other variable is also a string. Once again I am new to C# and any help is appreciated.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload