-1

I need help to rename files in c#.

I have 10 files in one folder. My requirement is I have add date part (MMddyyyyhhmmss) in all 10.

ex :

   1_gender_vadra_ram_R4.txt

   2_gender_vadra_syam_R4.txt

Now out put would be

   1_gender_vadra_ram_R4_MMddyyyyhhmmss.txt
   2_gender_vadra_syam_R4_MMddyyyyhhmmss.txt

I have tried below code but failed to get above file names.

varArchiveDataFolder is variable which value will come from ssis.

varArchiveDataFolder ="D:\Ram\
DirectoryInfo d = new DirectoryInfo(Dts.Variables["varArchiveDataFolder"].Value.ToString());
        FileInfo[] infos = d.GetFiles();

foreach(FileInfo f in infos)
{
    string CurrDate = System.DateTime.Now.ToString("MM/dd/yyyy HH:mm");
    CurrDate = CurrDate.Substring(0, 2) + CurrDate.Substring(3, 2) + CurrDate.Substring(6, 4) + CurrDate.Substring(11, 2) + CurrDate.Substring(14, 2);    
    File.Move(f.FullName, f.FullName.ToString().Replace("R4","R4_"+ "Currdate"));
}

My actual requirement is I need to delete files from Archive directory, if file is greater than 30 days of today.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Venkat
  • 3
  • 1
  • 2
  • Is the problem that you need to change the file name, or that you need to delete old files? Does your code sample successfully rename them? `FileInfo.CreationDate` can be used to determine if they're older than 30 days. Also you should try storing your "destination" value from `File.Move()` in a variable so while debugging you can check its actual value to verify it's correct. – sab669 Aug 25 '15 at 15:02
  • 1
    CurrDate is a variable but you use it like a string. Instead of "R4_"+ "Currdate" write "R4_"+ Currdate. This will concatenate strings. – Paweł Mikołajczyk Aug 25 '15 at 15:03
  • @PawełMikołajczyk additionally the variable is spelled incorrectly in his string. `CurrDate` is the variable where as his string is `"Currdate"` with a lower case d – sab669 Aug 25 '15 at 15:07
  • @Venkat Also are you just trying to remove the `/` from your date string? Just use `String.Replace` to replace all instances of a character or string, rather than a bunch of concatenated `Substring` calls. – sab669 Aug 25 '15 at 15:10
  • Sab669, atually i have to delete files in my archive folder if files are more than 30 days of load date. So i thought once load is done and file is moved to archive folder, i will rename file with loaded date. – Venkat Aug 26 '15 at 08:46
  • @Sab669, atually i have to delete files in my archive folder if files are more than 30 days of load date. So i thought once load is done and file is moved to archive folder, i will rename file with loaded date. In delete process , i thought first i wll delete files (days>30) and renames the files which don't have date in the file name. can you pls provide me the code to check how to check files which was not renamed (i.e _R4.txt instead of _R4_date.txt). thank you very much – Venkat Aug 26 '15 at 08:53

2 Answers2

1

Use DateTime.Now rather than attaching the string Currdate.

File.Move(f.FullName, f.FullName.ToString().Replace("R4","R4_"+ DateTime.Now.ToString("MMddyyyyhhmmss")));
laskdjf
  • 1,166
  • 1
  • 11
  • 28
1

If the objective is just to delete files that are greater then some date, why not try this. The code below, actually check last accessed time, but you can use other like create or modified time based on your need.

Delete files older than 3 months old in a directory using .NET

Community
  • 1
  • 1
N0mi
  • 714
  • 7
  • 14