Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I am trying to automate a process that would create an existing file and renames it with yesterday's date..
existing file is coming as CSHTRN01.txt
my expectation is CI070605.MST
Where (07) is the current month, (06) current Day and (05) current year..
Please help

Hi
I can set yesterday date into an environment variable, but how will the filename be given to the batch file, from the command line.
And the date variable always starts the the first letter of the filename.The batch file will be quite involved as getting yesterdays date right for month endings and year endings, can be quite tricky.

Hi dtech10,
The 'yesterday' thing came up a few months ago.
I decided that a BAT was just too much of a kludge.
I worrked on a QB45 prog. It was pretty messy, since I'm not adept at the 'case'.
I guess I should dust it off and finish what I started.
M2
If at first you don't succeed, you're about average.

Hi M2
I managed to get a batch file that works
in WinXP, seems ok.
Just waiting for Eddie's reply.QB is a better platform for this sort of program.

Perl can calculate and format yesterdays date in 1 line of code (actually, 2 lines if you include the loading of the module). Additionaly, it would be platform independant so it would work on Windows, Linux, UNIX, and Mac.
use POSIX qw(strftime);
$date = strftime("%m%d%y", localtime(time - 86400));

FM,
How do you get it into a variable to use for renaming?
M2
If at first you don't succeed, you're about average.

In my example yesterday's date was put into a variable called $date.
Here's another example with explaination.
#!perl -w
# load the strict pragma
use strict;# load the strftime function from the POSIX module.
use POSIX qw(strftime);# pass the date format and current time minus 86400 seconds (1 day) and assign $date the result
my $date = strftime("%m%d%y", localtime(time - 86400));# assign the $fname var the name of the new file
my $fname = "CI$date.MST";# print the $fname var, verifing the desired name
print $fname;#############
C:\>type test.pl
#!perl -wuse strict;
use POSIX qw(strftime);my $date = strftime("%m%d%y", localtime(time - 86400));
my $fname = "CI$date.MST";print $fname;
C:\>test.pl
CI070605.MST

I left out the renaming part. Assuming the original file name is in a ver called $orig the renaming would look like:
rename($orig, $fname);

![]() |
New to Java....
|
Need help with batch logi...
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |