Solved

dynamic paths - look for files, move files

Posted on 2006-06-26
15
355 Views
Last Modified: 2008-01-09
I need to search for text files, if the text files are older then 10 days, move files to a diffrent location else do nothing with files.
I am not sure how to dynamically set the destination folder on my app.config file, so instead of hard coding my destination folder i can just easilly change it on my config file?

When button clicked
search txt files in (dynamic directory)
if txt files are older than 10 days
move to (dynamice directory)
else
leave files at same location

??

0
Comment
Question by:Padre_Corleone
  • 6
  • 5
  • 4
15 Comments
 
LVL 5

Expert Comment

by:t_itanium
ID: 16990123
look at this ..it will help you in working with files and directories:
if it is not understood..copy the whole into a word doc.. or tell me so i can rearrange things..

this article contains the methods to manipulate files...like list files in adirectory...move...copy...delete file....etc...


Working with Files and Directories
 
Working with Files and Directories
The System.IO namespace contains the basic file I/O functions that enable you to read and write files, list directories, get file information, and manipulate files and directories. Most applications interact with a file in one way or another, and the System.IO namespace helps you do so.
In this section, you learn how to
•      List directories and files
•      Read and write simple text files
In JScript, VBScript, and Visual Basic, you use the FileSystemObject to work with files and directories. You can use the FileSystemObject in .NET, but because it's a COM object, there's additional resource overhead when calling the FileSystemObject's properties and methods. For this reason, you should always use the managed System.IO namespace for all file and directory code.
Inside the System.IO Namespace
The System.IO namespace contains static (or shared) classes and instance classes that enable you to interact with files, paths, directories, and streams. Streams can be from any type of resource, such as a file on the file system, a network stream, or even a URL from the Internet. You determine which classes in the System.IO namespace to use based on the type of stream you're accessing. If you look up the System.IO namespace in the SDK help file, you'll see something similar to Figure 9.12, which is a long list of the classes and namespaces that make up the System.IO namespace.
 
Figure 9.12 The System.IO namespace in the .NET Framework SDK.
Static and shared have the same meaning. In Visual Basic .NET, noninstance classes are called shared. In C#, noninstance classes are called static. All shared or static members are shared across all instances on a class, meaning that you can reference the member without using the New constructor for the member you're accessing.
Figure 9.13 is a compact representation of the hierarchy of the System.IO namespace. You can see the static (shared) classes and the instance classes in the figure.
 
Figure 9.13 Compact representation of the System.IO namespace.
As you can see in Figure 9.13, the System.IO namespace hierarchy contains many objects that interact with files and the file system. We'll discuss the File, Path, and Directory classes, and the StreamReader and StreamWriter classes. These are common classes for basic file I/O.
Understanding the File Class
The File class provides static methods that give you access to files. The instance class FileInfo gives you functionality similar to the static File class. But because the File class is static, it performs security checks each time you access a file object. If you're going to access the same file objects more than once, you should use the FileInfo class to avoid the overhead of performing security checks on the same objects each time. Table 9.3 lists the methods of the File class and their descriptions.
Table 9.3. Methods of the File Class
Method       Description
AppendText       Creates a StreamWriter that appends UTF-8–encoded text to an existing file.
Copy       Copies an existing file to a new file.
Create       Creates a file in the specified fully qualified path.
CreateText       Creates or opens a new file for writing UTF-8–encoded text.
Delete       Deletes the file specified by the fully qualified path. An exception isn't thrown if the specified file doesn't exist.
Exists       Determines whether the specified file exists.
GetAttributes       Gets the FileAttributes of the file on the fully qualified path.
GetCreationTime       Returns the creation date and time of the specified file or directory.
GetLastAccessTime       Returns the date and time the specified file or directory was last accessed.
GetLastWriteTime       Returns the date and time the specified file or directory was last written to.
Move       Moves a specified file to a new location, providing the option to specify a new file name.
Open       Overloaded. Opens a FileStream on the specified path.
OpenRead       Opens an existing file for reading.
OpenText       Opens an existing UTF-8–encoded text file for reading.
OpenWrite       Opens an existing file for writing.
SetAttributes       Sets the specified FileAttributes of the file on the specified path.
SetCreationTime       Sets the date and time the file was created.
SetLastAccessTime       Sets the date and time the specified file was last accessed.
SetLastWriteTime       Sets the date and time that the specified file was last written to.
Using the methods of the File class is straightforward. The following snippets use some of the methods to get you familiar with the syntax:
 
Imports System.IO

Public Class Class1

 Sub FileClassStuff()

  ' using Exists, Move, SetCreationTime,
  ' SetLastAccessTime, SetLastWriteTime

  If File.Exists("C:\Junk.txt") Then
   File.Move("C:\Junk.txt", "D:\Junk.txt")
   File.SetCreationTime("D:\Junk.txt", Date.Now)
   File.SetLastAccessTime("D:\Junk.txt", Date.Now)
   File.SetLastWriteTime("D:\Junk.txt", Date.Now)
  End If

  ' using Exists and Delete methods

  If File.Exists("C:\Junk.txt") Then
   File.Delete("C:\Junk.txt")
  End If

 End Sub
End Class

 
using System;
using System.IO;

namespace IO_FileClass_CS
{
 public class Class1
 {
  static void Main()
  {

  // using Exists, Move, SetCreationTime,
  // SetLastAccessTime, SetLastWriteTime

  if (File.Exists(@"C:\Junk.txt"))
  {
   File.Move(@"C:\Junk.txt", @"D:\Junk.txt");
   File.SetCreationTime(@"D:\Junk.txt", Date.Now);
   File.SetLastAccessTime(@"D:\Junk.txt", Date.Now);
   File.SetLastWriteTime(@"D:\Junk.txt", Date.Now);
  }

  // using Exists and Delete methods

  if (File.Exists(@"C:\Junk.txt"))
  {
   File.Delete(@"C:\Junk.txt");
  }
  }
 }
}
You should always use the File.Exists method to check for the existence of files instead of using a Try/Catch block to trap the FileNotFoundException if the file doesn't exists. This eliminates any of the overhead that might be introduced by using exception handling for actions that could be handled in the language, and not through the use for exceptions.
Understanding the Path Class
The Path class has methods that enable you to retrieve or modify paths on the operating system. When working with the Path class, the methods are smart enough to realize the rules of the operating system that your code is running on. For example, if an OS only allows 256-character pathnames with a 3-character extension, the Path class returns ArgumentException to let you know that you're doing something wrong. The same code might work perfectly on a system such as Windows XP, which has greater flexibility when working with file paths. Table 9.4 lists the more common available methods to you in the Path class.
Table 9.4. Methods of the Path Class
Method       Description
ChangeExtension       Changes the extension of a path string.
Combine       Combines two path strings.
GetDirectoryName       Returns the directory information for the specified path string.
GetExtension       Returns the extension of the specified path string.
GetFileName       Returns the filename and extension of the specified path string.
GetFileNameWithoutExtension       Returns the filename of the specified path string without the extension.
GetFullPath       Returns the absolute path for the specified path string.
GetPathRoot       Gets the root directory information of the specified path.
GetTempFileName       Returns a unique temporary filename and creates a zero-byte file by that name on disk.
GetTempPath       Returns the path of the current system's temporary folder.
HasExtension       Determines whether a path includes a filename extension.
IsPathRooted       Gets a value indicating whether the specified path string contains absolute or relative path information.
As is the File class, working with the Path class is very straightforward. All methods can be called directly without creating an instance of the Path class.
Understanding the Directory Class
The Directory class exposes methods that enable you to create, delete, move, rename, and enumerate directories and subdirectories. The Directory class is also a static class. The DirectoryInfo class is the instance class that enables you to create directory instances. Using the DirectoryInfo class avoids the overhead of security checks for each directory reference. Table 9.5 lists the common methods of the Directory class.
Table 9.5. Methods of the Directory Class
Method       Description
CreateDirectory       Creates all directories and subdirectories as specified by path.
Delete       Overloaded. Deletes a directory and its contents.
Exists       Determines whether the given path refers to an existing directory on disk.
GetCreationTime       Gets the creation date and time of a directory.
GetCurrentDirectory       Gets the current working directory of the application.
GetDirectories       Overloaded. Gets the names of subdirectories in the specified directory.
GetDirectoryRoot       Returns the volume information, root information, or both for the specified path.
GetFiles       Returns the names of files in the specified directory.
GetFileSystemEntries       Overloaded. Returns the names of all files and subdirectories in the specified directory.
GetLastAccessTime       Returns the date and time the specified file or directory was last accessed.
GetLastWriteTime       Returns the date and time the specified file or directory was last written to.
GetLogicalDrives       Retrieves the names of the logical drives on this computer in the form " <drive letter> :\".
GetParent       Retrieves the parent directory of the specified path, including both absolute and relative paths.
Move       Moves a file or a directory and its contents to a new location.
SetCreationTime       Sets the creation date and time for the specified file or directory.
SetCurrentDirectory       Sets the application's current working directory to the specified directory.
SetLastAccessTime       Sets the date and time the specified file or directory was last accessed.
SetLastWriteTime       Sets the date and time a directory was last written to.
You'll notice that some of these methods are similar to the methods of the Environment class (for example, GetLogicalDrives, which returns a string array of drives). Methods such as SetLastWriteTime and GetLastWriteTime are also similar to the methods of the File class. Because files and directories are similar object types, they share methods that perform similar operations.
In the next section, you create a sample application that uses some of the methods of the Directory class.
Creating an I/O Sample Application
To use some of the classes in the System.IO namespace, you're going to write a Windows Forms application that lists directories and files, writes a list of items from a list box to a text file, and then reads the text file back into a list box. To achieve this, you
•      Use the Directory class of the System.IO namespace
•      Use the StreamWriter class of the System.IO namespace to write to a file
•      Use the StreamReader class of the System.IO namespace to read from a file
•      Use arrays
•      Use the ListBox control
Follow these steps to set up the new application. But before you start, look at Figure 9.14 to see what the end result of your form should look like.
1.      Create a new Windows Forms application named IO_VB (if you're using Visual Basic .NET) or IO_CS (if you're using C#).
2.      On the default form1, change the following properties using the Properties window:
3.      Text = "Using the System.IO Namespace"
4.      
5.      StartupPosition = CenterScreen
6.      Next, drag a button from the Toolbox onto the form, and change these properties:
7.      Name = ListFolders
8.      
9.      Text = "List Folders"
10.      Drag another button from the Toolbox onto the form, and change these properties:
11.      Name = ListFiles
12.      
13.      Text = "List Files"
14.      Drag another button from the Toolbox onto the form, and change these properties:
15.      Name = WriteToFile
16.      
17.      Text = "Write To ListBox File"
18.      Drag another button from the Toolbox onto the form, and change these properties:
19.      Name = ReadFromFile
20.      
21.      Text = "Read ListBox From File"
22.      Drag a ListBox from the Toolbox onto the form, and change these properties:
23.      Anchor = Top, Bottom, Left, Right
 
Figure 9.14 Form1.vb from the IO application.
Your completed form should look like Figure 9.14.
Writing the IO Application Code
Now that you have the project set up, you can start writing the code to retrieve directory information. Double-click the List Folders button and add the code in Listing 9.4 to the code-behind for the ListFolders_Click event. This code uses the Directory.GetDirectories methods to return an array of directories.
Example 9.4. ListFolders_Click Event Code
 
Private Sub ListFolders_Click(ByVal sender As System.Object, _
   ByVal e As System.EventArgs) Handles ListFolders.Click

 ' Create a string variable array named
 ' dirs to hold the directory entries for the
 ' special folder Program Files
 Dim dirs As String() = _
   Directory.GetDirectories _
   (Environment.GetFolderPath _
   (Environment.SpecialFolder.ProgramFiles))

 ' Create a string variable for the For Each loop
 Dim dir As String

 ' Loop thru the array of folders and add them to the
 ' ListBox1 using the Items.Add method
 For Each dir In dirs
  With ListBox1.Items
   .Add(dir)
  End With
 Next

End Sub

 
private void listFolders_Click(object sender, System.EventArgs e)
 {

  /* Create a string variable array named
  dirs to hold the directory entries for the
  special folder Program Files */

  string[] dirs =
  Directory.GetDirectories
  (Environment.GetFolderPath
  (Environment.SpecialFolder.ProgramFiles));

  /* Loop thru the array of folders and add them to the
  ListBox1 using the Items.Add method */
  foreach (string dir in dirs)
  {
   listBox1.Items.Add(dir);
  }
 }
If you press the F5 key to run the application and click the Get List Folders button, you should see a listing of the subdirectories of your Program Files folder. My output looks like Figure 9.15.
 
Figure 9.15 Output from the ListFolders_Click event.
In Listing 9.4, you build on techniques you used in the Environment application you created earlier today. You use the GetFolderpath method of the Environment class to return the location of the Program Files folder. The Directory.GetDirectories method returns a string array of folders in the Program Files directory. You then loop through and add them to the list box using the Items.Add method. Using the GetFolderPath method of the Environment class ensures that you won't get an error if the user has a path for Program Files other than C:\Program Files.
To get folders and files, you use the GetFileSystemEntries method of the Directory class. Double-click the List Files button and add the code in Listing 9.5 to the ListFiles_Click event.
Example 9.5. Using the GetFileSystemEntries Method
 
Private Sub ListFiles_Click(ByVal sender As System.Object, _
   ByVal e As System.EventArgs) Handles ListFiles.Click

 ' Clear the ListBox
 ListBox1.Items.Clear()

 ' Create a string variable array named
 ' files to hold the directory and file entries for the
 ' special folder My Documents
 Dim files As String() = _
  Directory.GetFileSystemEntries _
  (Environment.GetFolderPath _
  (Environment.SpecialFolder.Personal))

 ' Create a string variable for the For Each loop
 Dim file As String

 ' Loop thru the array of files and folders and add
 ' them to the ListBox1 using the Items.Add method
 For Each file In files
  With ListBox1.Items
   .Add(file)
  End With
 Next

End Sub

 
private void listFiles_Click(object sender, System.EventArgs e)
 {
  // Clear the listBox
  listBox1.Items.Clear();

  /* Create a string variable array named
  files to hold the directory and file entries for the
  special folder My Documents */
  string[] files =
  Directory.GetFileSystemEntries
  (Environment.GetFolderPath
  (Environment.SpecialFolder.Personal));

  /* Loop thru the array of files and folders and add
  them to the ListBox1 using the Items.Add method */
  foreach (string file in files)
  {
   listBox1.Items.Add(file);
  }
 }
Now you can run the application by pressing F5 and observe the output. In Listing 9.5, you use the GetFileSystemEntries method to return an array of files and directories in the My Documents directory. The Environment.SpecialFolder.Personal enumeration returns the correct location of the My Documents folder to list the files and folders. You could also replace the GetFileSystemEntries method with the GetFiles method to return only the files in the My Documents folder.
Using the StreamWriter and StreamReader Classes
Up to now, you've used the classes of the System.IO namespace to access file system objects. But to read and write data, you must use the methods of the StreamReader and StreamWriter classes.
In Figure 9.13, you saw a representation of the System.IO namespaces and its members. There are classes that handle network streams, crypto streams, binary streams, text streams, and buffered streams. To accomplish basic reading and writing of files, you use the methods of the StreamReader and StreamWriter classes to complete the I/O project you started earlier. But before we write that code, examine Tables 9.6 and 9.7 to get an idea of what methods are available in the StreamReader and StreamWriter classes.
Table 9.6. Methods of the StreamReader Class
Method       Description
Close       Closes the StreamReader and releases any system resources associated with the reader.
DiscardBufferedData       Allows a StreamReader to discard its current data.
Peek       Returns the next available character but doesn't consume it.
Read       Reads the next character or next set of characters from the input stream.
ReadBlock       Reads a maximum of count characters from the current stream and writes the data to buffer, beginning at index .
ReadLine       Reads a line of characters from the current stream and returns the data as a string.
ReadToEnd       Reads the stream from the current position to the end of the stream.
Table 9.7. Methods of the StreamWriter Class
Method       Description
Close       Closes the current StringWriter and the underlying stream.
Flush       Clears all buffers for the current writer and causes any buffered data to be written to the underlying device.
GetStringBuilder       Returns the underlying StringBuilder.
ToString       Returns a string containing the characters written to the current StringWriter so far.
Write       Writes to this instance of the StringWriter.
WriteLine       Writes some data as specified by the overloaded parameters, followed by a line terminator.
The StreamReader class gives you all the read methods to accomplish reads on a stream after you've opened it. Read, Peek, ReadToEnd, ReadLine, and ReadBlock all read data from a stream. The StreamWriter methods perform the task of writing data to a stream.
Write and WriteLine are the methods to write data, with the only difference being that the WriteLine method adds a line terminator to the end of the line of data you're writing.
To implement the code to loop through the items in the ListBox control on your form, double-click the Write To File button, and add the code in Listing 9.6 to the WriteToFile_Click event.
Example 9.6. Using the StreamWriter Class
 
Private Sub WriteToFile_Click(ByVal sender As System.Object, _
   ByVal e As System.EventArgs) Handles WriteToFile.Click

 ' Create a new text file using the StreamWriter class
 Dim sw As New StreamWriter("C:\List.txt")

 ' Create a temp counting variable
 Dim intX As Short

 ' Loop thru the ListBox, grabbing the items and
 ' writing them to the stream using the WriteLine method
 Dim s As String
 For Each s In ListBox1.Items
  sw.WriteLine(s)
 Next

 ' Close the StreamWriter
 sw.Close()

End Sub

 
private void WriteToFile_Click(object sender, System.EventArgs e)
 {
  // Create a new text file using the StreamWriter class
  StreamWriter sw = new StreamWriter(@"C:\List.txt");

  /* Loop thru the ListBox, grabbing the items and
  writing them to the stream using the WriteLine method */
  foreach (string s in listBox1.Items)
  {
  sw.WriteLine(s);
  }

  // Close the StreamWriter
  sw.Close();
 }
After you run the application, you can check the List.txt file in the C drive. It lists the files that are also listed in the ListBox in a nice text format. Using the WriteLine method of the StreamWriter class adds the line terminator after each item it writes out in the List.txt file. Figure 9.16 shows the List.txt file from my machine.
 
Figure 9.16 The List.txt output from the StreamWriter.
The last step in the code for the I/O application is writing the code-behind for the Read From File button. Listing 9.7 has the code that uses the StreamReader class to create a new stream object based on the List.txt file you just created in Listing 9.6. Add the code in Listing 9.7 to the code-behind for the ReadFromFile_Click event to see this in action.
Example 9.7. Using the StreamReader Class
 
Private Sub ReadFromFile_Click(ByVal sender As System.Object, _
   ByVal e As System.EventArgs) Handles ReadFromFile.Click

 ' Clear the ListBox
 ListBox1.Items.Clear()

 ' Create a new StreamReader and pass the file
 ' created in the WriteToFile_Click event
 Dim sr As New StreamReader("C:\List.txt")

 ' Use the Peek method to move to the next character
 ' and the ReadLine method to get the next line
 While sr.Peek <> -1
  With ListBox1.Items
   .Add(sr.ReadLine)
  End With
 End While

 ' Close the StreamReader
 sr.Close()

End Sub

 
private void ReadFromFile_Click(object sender, System.EventArgs e)
 {
  // Clear the listBox
  listBox1.Items.Clear();

  /* Create a new StreamReader and pass the file
  created in the WriteToFile_Click event */
  StreamReader sr = new StreamReader(@"C:\List.txt");

  /* Use the Peek method to move to the next character
  and the ReadLine method to get the next line */
  do
  {
   listBox1.Items.Add(sr.ReadLine());
  }
  while (sr.Peek() != -1);

  // Close the StreamReader
  sr.Close();

 }
Now run the application to read the data from the file. The first line of code in Listing 9.7 clears the ListBox, and then the new StreamReader is created based on the file previously created. Using a loop, you're peeking into the file, one line at a time, using the Peek method. The ReadLine method reads the next line, and adds the string it reads into the ListBox. Because Peek doesn't consume the bytes of data that it reads, you can keep peeking into a stream while consuming its data with any of the read methods of the stream class. You finally call the Close method of the StreamReader to release the resource. You should always be diligent about using Close on resources that have a Close method. Not using Close could keep the stream handle open or waste unnecessary resources.
Summary | Next SectionPrevious Section








      











Simple Text File Operations in C#.
By Sriram Vaideeswaran
C-Sharp provides a File class which is used in manipulating text files. The File class is within the System namespace. Also we can use the StreamReader and StreamWriter classes, which are within the System.IO, namespace for reading from and writing to a text file. In this article we'll see examples of Creating a text file, reading contents of a text file and appending lines to a text file.

1.Creating a Text File
For creating text file we use the CreateText Method of the File Class. The CreateText methods takes in the path of the file to be created as an argument. It creates a file in the specified path and returns a StreamWriter object which can be used to write contents to the file.

Example

public class FileClass
{
    public static void Main()
    {
    WriteToFile();
    }
    static void WriteToFile()
    {
    StreamWriter SW;
    SW=File.CreateText("c:\\MyTextFile.txt");
    SW.WriteLine("God is greatest of them all");
    SW.WriteLine("This is second line");
    SW.Close();
    Console.WriteLine("File Created SucacessFully");
    }
}

2.Reading Contents Of A Text File
For reading the contents of a text file we use the OpenText Method of the File Class.The OpenText methods takes in the path of the file to be opened as an argument. It opens the specified file and returns a StreamReader object which can be used to read the contents of the file.

Example

public class FileClass
{
    public static void Main()
    {
    ReadFromFile("c:\\MyTextFile.txt");
    }
    static void ReadFromFile(string filename)
    {
    StreamReader SR;
    string S;
    SR=File.OpenText(filename);
    S=SR.ReadLine();
    while(S!=null)
    {
    Console.WriteLine(S);
    S=SR.ReadLine();
    }
    SR.Close();
    }
}

3.Appending Content To A Text File
For appending content to a text file we use the AppendText Method of the File Class. The AppendText methods takes in the path of the file to which the contents to be appended as an argument. It opens the file in the specified path and returns a StreamWriter object which can be used to append contents to the file.

Example

public class FileClass
{
    public static void Main()
    {
    AppendToFile();
    }
    static void AppendToFile()
    {
    StreamWriter SW;
    SW=File.AppendText("C:\\MyTextFile.txt");
    SW.WriteLine("This Line Is Appended");
    SW.Close();
    Console.WriteLine("Text Appended Successfully");
    }
}


cheers


0
 
LVL 19

Expert Comment

by:arif_eqbal
ID: 16990182
Suppose your Config file has an entry for the path....
The config file would look something like this

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
        <add key="Path" value="c:\MyFolder" />
    </appSettings>
</configuration>
In case you want to change the path anytime just open the app.config file say in Notepad and edit the entry for "Path"

Then this code would read the path and move files 10 days old to some other folder
imports System.Configuration

        Dim Path As String = ConfigurationSettings.AppSettings("Path");
        Dim Fls() As String = Directory.GetFiles(Path)
        For Each Fl As String In Fls
            If File.GetCreationTime(Fl) < Today.AddDays(-10) Then
                File.Move(Fl, "C:\SomeFolder" & Fl.Substring(Fl.LastIndexOf("\")))
            End If
        Next

Note that this is .Net 2003 code if you are using 2005 the code to access the AppSettings wouldchange to
ConfigurationManager.AppSettings("Path");


0
 
LVL 7

Author Comment

by:Padre_Corleone
ID: 16990237
'    File.Move(Fl, "C:\SomeFolder" & Fl.Substring(Fl.LastIndexOf("\")))
would c:\somefolder be the new destination to move file? and how can i make this dynamic also so i can place in my appsettings folder?


in this same code  If i am looking for *.txt files how wuold i be able to search for all text files?
0
 
LVL 19

Accepted Solution

by:
arif_eqbal earned 500 total points
ID: 16990266
OK
Now config file would be like this

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
        <add key="SourcePath" value="c:\MyFolder" />
        <add key="DestinationPath" value="c:\SomeFolder" />  <!-- Entry for Destination as well --->
    </appSettings>
</configuration>


and the code would modify to

        Dim SourcePath As String = ConfigurationSettings.AppSettings("SourcePath");
        Dim DestPath As String = ConfigurationSettings.AppSettings("DestinationPath");  '<-- Get Destination path also from config
        Dim Fls() As String = Directory.GetFiles(SourcePath, ".txt")  '<-- search for only TXT files
        For Each Fl As String In Fls
            If File.GetCreationTime(Fl) < Today.AddDays(-10) Then
                File.Move(Fl, DestPath & Fl.Substring(Fl.LastIndexOf("\"))) '<-- and move to destination folder
            End If
        Next

0
 
LVL 5

Expert Comment

by:t_itanium
ID: 16990322
string[] files =Directory.GetFileSystemEntries(filepath,"**.txt");
if (files.Length>0)
{
          foreach (string file in files)
           {
      if (File.Exists(@file))
      {
            //do what ever you want
      }

          }
}
0
 
LVL 7

Author Comment

by:Padre_Corleone
ID: 16990325
sorry to ask once again, but lets say

I want to move the txt file to a file folder that contains the name 06_26_2006
for example

move text file to file folder which has todays date folder 06_26_2006
if folder does not exist
create file folder name it with todays date 06_26_2006 and place txt file in there

I would like to inlcude this code in

       Dim SourcePath As String = ConfigurationSettings.AppSettings("SourcePath");
        Dim DestPath As String = ConfigurationSettings.AppSettings("DestinationPath");  '<-- Get Destination path also from config
        Dim Fls() As String = Directory.GetFiles(SourcePath, ".txt")  '<-- search for only TXT files
        For Each Fl As String In Fls
            If File.GetCreationTime(Fl) < Today.AddDays(-10) Then
                File.Move(Fl, DestPath & Fl.Substring(Fl.LastIndexOf("\"))) '<-- and move to destination folder
            End If
        Next
0
 
LVL 5

Expert Comment

by:t_itanium
ID: 16990346
sorry for providing the code in c# bbut i think it is easy to translate right?      

if(!Directory.Exists("C:\\"+DateTime.Now.Month.ToString()+"_"+DateTime.Now.Day.ToString()+"_"+DateTime.Now.Year.ToString())//! means not
      {
           Directory.CreateDirectory("C:\\"+DateTime.Now.Month.ToString()+"_"+DateTime.Now.Day.ToString()+"_"+DateTime.Now.Year.ToString());
      }
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 19

Expert Comment

by:arif_eqbal
ID: 16990387
Instead of the line
   Dim DestPath As String = ConfigurationSettings.AppSettings("DestinationPath");  
write
   Dim DestPath As String = "C:\" & Today.Day.ToString & "_" & Today.Month.ToString & "_" & Today.Year
   If Not Directory.Exists(DestPath) Then Directory.CreateDirectory(DestPath)

0
 
LVL 7

Author Comment

by:Padre_Corleone
ID: 16990427
I am not sure why but when I use the following code my form name doesn't show on the top, button name doesnt' show and also when i click by button nothing happens?
0
 
LVL 19

Expert Comment

by:arif_eqbal
ID: 16990437
what version are you using ?
test by removing the code and running...
0
 
LVL 5

Expert Comment

by:t_itanium
ID: 16990456
try to creat a new project....

and add code to it..

cheers
0
 
LVL 7

Author Comment

by:Padre_Corleone
ID: 16990484
An unhandled exception of type 'System.Configuration.ConfigurationException' occurred in system.dll

Additional information: This is an invalid comment syntax.  Expected '-->'. Line 5, position 102.

' this line is highlighted in green
  Dim SourcePath As String = AppSettings("SourcePath")
0
 
LVL 7

Author Comment

by:Padre_Corleone
ID: 16990491
the was an extra --> in my app config file but now when I run my appplication
I click btn start
and nothing happens,
I also adde code to new project that is strange
0
 
LVL 5

Expert Comment

by:t_itanium
ID: 16990498
use

ConfigurationSettings.AppSettings("SourcePath")

and not

AppSettings("SourcePath")
0
 
LVL 7

Author Comment

by:Padre_Corleone
ID: 16990499
ok it works now!

I changed the 10 days old < to > and it works!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now