zee12 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I am newbie here.. and also in perl learning... actually i want to create a new folder with Date and time stamp like folder name "man" along with that it must date when i ran the time.. so please can any one help me over it..ASAP. thanks in advance.

Update:

Hi Monks .. i have doubt on relative paths... i am using Absolute paths.. like this..Program is copying from source to destination.
mkdir "C:\\$dir1\\BC_TESTCASES"; $copy_s= "C:\\SPED_TDSCDMA_IRAT_PRODUCT\\BC_TESTCASES"; $copy_d= "C:\\$dir1\\BC_TESTCASES"; system "copy \"$copy_s\" \"$copy_d\" ";
i have to convert to realtive paths.. so m trying this...
mkdir "..\\BC_TESTCASES"; $copy_s= "..\\BC_TESTCASES"; print "$copy_s"; $copy_d= "$dir1/BC_TESTCASES"; system "copy \"$copy_s\" \"$copy_d\" ";
Its showing system cant fibd path specified ..how exactly do this thing..please reply ASAP..!!!!

Replies are listed 'Best First'.
Re: Need time Stamp for folder..
by AppleFritter (Vicar) on Aug 11, 2014 at 09:27 UTC

    Howdy zee12, welcome to the Monastery!

    Do you want to create a folder the timestamp (i.e. last modified time) of which matches the current date, or a folder with a name that includes the current date? If the former, simply create it using mkdir, as Grandfather suggested.

    If the latter, use mkdir, localtime (to get the current time) and the strftime function from the standard POSIX module (to format it nicely), e.g. like this:

    #!/usr/bin/perl use strict; use warnings; use POSIX qw/strftime/; mkdir strftime "man %Y-%m-%d", localtime;

    The POSIX module's strftime function is just a wrapper around the underlying C function, so see the strftime(3) man page on your system for more information on the various flags you can use to format dates and times.

      Thanks.. now i am able create a new file with Name and Date.. but when i run again its not showing folder with same name created at the same time.. am using Windows 7 machine.. Thanks in Advance.

        now i am able create a new file with Name and Date.. but when i run again its not showing folder with same name created at the same time.. am using Windows 7 machine..

        I'm not sure what you mean. Can you clarify what the expected and actual results are? E.g. "I'm using your snippet from Re: Need time Stamp for folder... The first time I run it, it does FOO; the second time, I expected it to do BAR, but it actually did BAZ".

        I'm guessing that what's happening is that no new folder is being created the second time. The reason for that is simple: the folder name, e.g. "man 2014-08-12", only changes when the date changes, so running the script twice on the same day will not create another folder (since, naturally, there cannot be two folders with the same name in the same parent folder).

        And it's creating folder names like that because that's what the strftime flags tell it to: "%Y" is the current year (four digits), "%m" is the current month (two digits), and "%d" is the current day (two digits). If you want a finer-grained time stamp, simply adjust the format string for strftime and add more flags, as necessary/desired.

        Have you read up on strftime's flags, as I suggested above?

        Here's Microsoft's documentation for the function. For a finer-grained timestamp, you may want to use something "%Y-%m-%d %H-%M-%S" (which will produce e.g. "2014-08-12 08:28:45"), though I'll caution that this is still granular down to one second.

        If you need to be able to (possibly) create more than one directory per second, you could use a module like Time::HiRes that provides micro- or nanosecond time. But then, this might be an example of an XY Problem, and instead of using timestamps to try and ensure that folder names are unique, you may be better off using another module that guarantees unique names.

        File::Temp has a tempdir function for that purpose, for instance. And although it's intended for temporary directories, they're not automatically deleted, so the module should be suitable for this purpose:

        This is the recommended interface for creation of temporary directories. By default the directory will not be removed on exit (that is, it won't be temporary; this behaviour can not be changed because of issues with backwards compatibility).

        There may well be other useful modules as well.

        Thanks.. now i am able create a new file with Name and Date.. but when i run again its not showing folder with same name created at the same time.. am using Windows 7 machine.. Thanks in Advance.

        What is it showing then? How are you checking?

Re: Need time Stamp for folder..
by GrandFather (Saint) on Aug 11, 2014 at 05:22 UTC

    See mkdir. Most OSen use the current time for newly created files and folders so it should "just work"tm.

    Perl is the programming world's equivalent of English
      thanks for all helps.. Just for example: i created folder like English 12-08-2014 with local time..when i ran first time .. so next time if i ran it again it should create like..same name English 12-08-2014 with local time separately...