Advertisement
SMASIF

Generate Date Wise ID for Uploaded FIles

Nov 28th, 2020
1,549
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.61 KB | None | 0 0
  1. public string GetName(string path, string fileType)
  2.         {
  3.             string[] sourcePath = Directory.GetFiles(path);
  4.             Int64 yearMonth = (DateTime.Now.Year * 10000) + (DateTime.Now.Month * 100) + DateTime.Now.Day;
  5.             string name = yearMonth + "-001";
  6.             int pathNo2 = 0;
  7.  
  8.             string objt = db.FileCopyFromServers.Where(x => x.YearMonth == yearMonth && x.FileType == fileType).OrderByDescending(x => x.YearMonth).Select(x => x.FileName).FirstOrDefault();
  9.             if (objt != null)
  10.             {
  11.                 objt = objt.Substring(objt.Length - 7).Substring(0, 3);
  12.                 pathNo2 = Convert.ToInt32(objt) + 1;
  13.                 name = CovertName(pathNo2, yearMonth);
  14.             }
  15.  
  16.             if (sourcePath.Length != 0)
  17.             {
  18.                 string[] sptPath = sourcePath[sourcePath.Length - 1].Split('\\');
  19.                 int count = sptPath.Length;
  20.                 string temp = sptPath[count - 1].Substring(sptPath[count - 1].Length - 16).Substring(0, 12);
  21.                 sptPath = temp.Split('-');
  22.  
  23.                 int pathNo1 = 0;
  24.                 int extnNumber = 0;
  25.  
  26.                 int pathYearMonth;
  27.                 bool success = Int32.TryParse(sptPath[0], out pathYearMonth);
  28.                 if (success)
  29.                 {
  30.                     if (pathYearMonth == yearMonth || pathNo2 != 0)
  31.                     {
  32.                         if (pathNo2 != 0 && Convert.ToInt32(sptPath[0]) == yearMonth)
  33.                         {
  34.                             pathNo1 = Convert.ToInt32(sptPath[1]) + 1;
  35.                             extnNumber = pathNo1 < pathNo2 ? pathNo2 : pathNo1;
  36.                         }
  37.                         else if (Convert.ToInt32(sptPath[0]) == yearMonth)
  38.                         {
  39.                             extnNumber = Convert.ToInt32(sptPath[1]) + 1;
  40.                         }
  41.                         else if (pathNo2 != 0)
  42.                         {
  43.                             extnNumber = pathNo2;
  44.                         }
  45.  
  46.                         name = CovertName(extnNumber, yearMonth);
  47.  
  48.                     }
  49.                 }
  50.  
  51.             }
  52.  
  53.             return name;
  54.         }
  55.  
  56.  
  57. public string CovertName(int extnNumber, Int64 yearMonth)
  58.         {
  59.  
  60.             if (extnNumber < 10)
  61.             {
  62.                 return yearMonth + "-00" + extnNumber;
  63.             }
  64.             else if (extnNumber < 100)
  65.             {
  66.                 return yearMonth + "-0" + extnNumber;
  67.             }
  68.             else
  69.             {
  70.                 return yearMonth + "-" + extnNumber;
  71.             }
  72.  
  73.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement