Moving file from folder to folder

user3560266
:20:S10734539/940

am reading swift messages from a folder, and at line :20: I perform some sort of validation to know if the file and reading is valid. In this case if line :20: has 940 in it, the windows service reads and moves it to a success folder when done. But an invalid file will not have 940 on line :20: , the windows service is meant to move the invalid file to an invalid file location. I have written a code to do that, but it not able to move the file. I get an error message 'file in use' find below snippets of my code.

    if (Directory.Exists(@CBN_INFLOW940))
DirectoryInfo dr = new DirectoryInfo(CBN_INFLOW940);
                        FileInfo[] fi = dr.GetFiles();
                        string[] files = Directory.GetFiles(CBN_INFLOW940);

int lengthchk = 0;

if (files.Length < 1)
    check = false;

while (files.Length > lengthchk)
{
    StringBuilder sb = new StringBuilder();
    logger.Info(fi[lengthchk].Name + ": read from folder");
    string narrationgen = "";
    bool isvalidrtgs = false;
    DateTime createddate = fi[lengthchk].CreationTime.Date;
    FileStream stream = null;
    try
    {
    stream = File.Open(files[lengthchk], FileMode.Open, FileAccess.ReadWrite, FileShare.None);
    StreamReader sf = new StreamReader(stream);

     }

and

if (line.Contains(":20:"))
{
        firstchk = 1;
        if (!line.Contains('/'))
        {
            string[] fnamesplit = fi[lengthchk].Name.Split('.');
            string newfname = fnamesplit[0] + "_bk" + ".txt";
            string destlocation = Invalidfilelocation940 + newfname;
            string sourcelocation = CBN_INFLOW940 + fi[lengthchk]; //  + "\\"
            File.Move(sourcelocation, destlocation);
            return;
        }
        string[] narr = line.Split('/');
        string filecode = narr[1];

        if (filecode.Trim() != "940")
        {
            string[] fnamesplit = fi[lengthchk].Name.Split('.');
            string newfname = fnamesplit[0] + "_bk" + ".txt";
            string destlocation = Invalidfilelocation940 + newfname;
            string sourcelocation = CBN_INFLOW940 + "\\" + fi[lengthchk];
            File.Move(sourcelocation, destlocation);

            return;
        }
}
Derek

Try to split out your check code from your move code. It will be a better design, plus you may be locking the file for reading while you are trying to move it.

Think:

if ( ShouldMoveFile( filename ) )
{
    File.Move...
}

And make sure you close the file you are reading.

If you keep the code the same:

add the lines:

sf.Close();
stream.Close();

before you go to move the files.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Make jar file from folder

From Dev

Automatically load file from a folder

From Dev

Finding File from a Folder

From Dev

Pick .class file from a folder

From Dev

Moving a file to different folder along with history in git

From Dev

Moving read items from a folder back to inbox

From Dev

How to remove a file from folder

From Dev

Moving Folder in a Directory

From Dev

Get file paths from a folder

From Dev

Moving the dropbox folder

From Dev

Moving Chrome cache folder?

From Dev

Moving all files from one folder to upper folder

From Dev

Moving a folder across the network

From Dev

Extracting a file from the WinSxS folder

From Dev

moving a file to a folder with root privileges in CentOS 7

From Dev

Overwrite file from old folder to new folder

From Dev

Finding File from a Folder

From Dev

Moving application to another folder

From Dev

Moving folder content to parent folder

From Dev

Moving zip folder from one location to another using batch file

From Dev

Finding a string in a file within a directory then moving that entire file to a new folder

From Dev

Moving Files in a Folder

From Dev

IOException while moving file from one folder to other

From Dev

Moving a file to different folder along with history in git

From Dev

File upload not moving file to tmp folder

From Dev

Prevent mv from moving target folder

From Dev

Error while moving the file from one folder to another using shutil

From Dev

Moving files in python based on file and folder name

From Dev

I need help simultaneously moving the same file type in multiple folder from one subdirectory to another