C++/Rename.Bat - day difference
QUESTION: Good days to you.
I would like to create a batch file (rename.bat), change a file name according to day difference from original.
I hope you can help me for this, thank you.
On 01 JAN 2013, original file name is A0001.dat
on 02 JAN 2013, need rename to A0002.dat
on 03 JAN 2013, need rename to A0003.dat
and so on...A####.dat
#### is day differance for original file name.
ANSWER: Build this program to generate an executable called "change_file_name.exe":
int main( int argc, char** argv )
if( argc != 2 )
std::cerr << "usage: " << argv << " <file_to_rename>\n" ;
return 1 ;
const char* const file_name_prefix = "A" ;
const char* const file_name_suffix = ".dat" ;
const int base_year = 113 ; // 1900 + base_year
const int MAX_DAYS_PER_YEAR = 366 ;
const int WIDTH = 5 ;
const char FILL = '0' ;
std::time_t now = std::time(0) ;
const std::tm* ptm = std::localtime(&now) ;
const int day_number = ptm->tm_yday + ( ptm->tm_year - base_year ) * MAX_DAYS_PER_YEAR ;
std::ostringstream stm ;
stm << file_name_prefix
<< std::setw(WIDTH) << std::setfill(FILL) << day_number
<< file_name_suffix ;
std::string file_name = stm.str() ;
std::cout << "attempting to rename '" << argv << "' to '" << file_name << "'\n" ;
if( std::rename( argv, file_name.c_str() ) != 0 )
std::cerr << "*** error *** failed to rename file.\n" ;
return 2 ;
And then, in the batch file, have a line
to rename 'old_file_name' to 'A####.dat'
Note: This is illustrative; this program does not account accurately for leap years.
---------- FOLLOW-UP ----------
QUESTION: == this exe is easy to create by visual basic with datediff function,
however, if you can provide pure bat file will be the best.
Computation of the dates elapsed using a shell script would also be somewhat verbose.
For several example scripts (Unix), see:
Alas, I'm not at all familiar with the Windows shell.