0

I want to rename all file in a specified folder ending with .txt to .csv that are older then 1 day. I have the following that will get me all files ending with .txt and rename them to .csv

For /R %1 %%G IN (*.txt) DO ren "%%G" "%%~nG.csv"

The part I am struggling with is only renaming the files that are older then a day. I am looking for a solution that will work despite the date format that may be set up on a system.

Kevin Kunderman
  • 2,036
  • 1
  • 19
  • 30
  • what windows version are you using? i am asking this because forfiles.exe provides the useful switch which could help to build a solution to your problem – AcidJunkie May 13 '14 at 16:39
  • possible duplicate of [Batch file to delete files older than N days](http://stackoverflow.com/questions/51054/batch-file-to-delete-files-older-than-n-days) – Ken White May 13 '14 at 16:43
  • @AcidJunkie I am developing on Windows 7 but would like the script to be able to run on Windows Server 2008,2012, and Windows Vista,7,8.1 – Kevin Kunderman May 13 '14 at 16:43
  • @KevinKunderman hm okay. still you could use forfile.exe, but then you have to assemble the current date so it could be used for the /d argument. On Windows 2012 / Windows 8, forfile.exe allows to specify the file age in days. Is vbscript not an option? – AcidJunkie May 13 '14 at 16:47

1 Answers1

1

You would use "forfiles". The code would be:

forfiles /m "*.txt" /c "cmd /c Ren @fname.txt *.csv" /d -01

That will rename all .txt files to .csv files.

Edit

Ren *.txt was changed to @fname.txt

Behavior
  • 180
  • 11