Tuesday, November 30, 2010

Renaming the folder using regular expressions

string filePath = General.GetUserDirectory(hidUserID.Value);

      //Create the query to be sent to the database
      string _strWhere = "where iduser = '" + hidUserID.Value + "' and location like '%" + hidFileOrFolderName.Value + "%'";

      DataSet ds = new DataSet();
      //Fill the dataset with the rows to be manipulated
      ds = FileFolderDB.GetMultipleRows(_strWhere);

      //Save the dataset in the session for the rollback operations if the database update fails
      Session["DataTableBackUp"] = ds;

      try
      {
          //Check if dataset 'ds' has any tables in it
          if (ds.Tables.Count > 0)
          {
              #region Loop through the data set and perform database update operations

              //Loop through the first table in dataset 'ds'
              for (int j = 0; ds.Tables[0].Rows.Count > 0 && j < ds.Tables[0].Rows.Count; j++)
              {
                  //Assign values from the Table
                  string FolderLocation = ds.Tables[0].Rows[j]["location"].ToString();
                  string _strFolderID = ds.Tables[0].Rows[j]["idFileFolder"].ToString();
                  string _strUserID = ds.Tables[0].Rows[j]["idUser"].ToString();
                  string  _isFolder = ds.Tables[0].Rows[j]["IsDirectory"].ToString();
                  string _filename = ds.Tables[0].Rows[j]["Name"].ToString();

                  //Check if the folderlocation has any match with the compare string


                  //Build first compare string with complete path of the selected folder location
                  string _strCompareString = hidFolder.Value;

                  //Escape any metacharacters in the string for absolute pattern match
                  _strCompareString = Regex.Escape(_strCompareString);

                  //Finally build our custom compare string to detect a specific pattern
                  _strCompareString = _strCompareString + ".";

                 


                  // Check if the file/folder path matches with our compare string
                  if (Regex.IsMatch(FolderLocation, _strCompareString))
                  {
                     


                      //Create another compare string to detect all the files and folder within the selected folder
                      string _strCompareString2 = hidFolder.Value + hidFileOrFolderName.Value;
                      _strCompareString2 = Regex.Escape(_strCompareString2);
                      _strCompareString2 = _strCompareString2 + ".";


                      //Build the replace string which has the full path of folder renamed
                      string _strReplaceString = hidFolder.Value + txtRenameFolder.Text.Trim();
                     
                      //Split the path after the selected folder name
                      string[] split = Regex.Split(FolderLocation, _strCompareString2);

                      //Attach the full path of new folder along with above retrieved content of string
                      string temp = _strReplaceString + @"\" + split[1].ToString();

                      //Create an object of file folder for database insertion
                      FileFolder ff = new FileFolder();

                      //Set the attributes of filefolder
                      ff.UserID = Convert.ToInt64(_strUserID);
                      ff.ID = Convert.ToInt64(_strFolderID);
                      ff.Location = temp;

                      //Checksum calculation only for description purpose
                      if (_isFolder == "False")
                      {
                          string checksum = General.CalculateFileCheckSum( filePath+ ff.Location + _filename);
                      }


                      Database db = new Database();

                      // Finally update the file/folder details with new attributes
                      FileFolderDB.renameFolder(db, ff);


                  }




              }
              #endregion

             
          }
      }
      catch (Exception ex)
      {
          //When an exception occurs rollback the database to the state it was before

          RollbackRenameUpdate(ds);

          //Finally log the error
          General.ExceptionOccured(ex);

      }

No comments:

Post a Comment