Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Accessing a folder whose name changes daily

by JennieInMI (Acolyte)
on Dec 18, 2012 at 15:34 UTC ( #1009404=perlquestion: print w/ replies, xml ) Need Help??
JennieInMI has asked for the wisdom of the Perl Monks concerning the following question:

I need to verify, via a script, the existence of a file in an installer folder. The name of the installer folder changes daily. It is deleted each night before the build and a new folder is created with the new installer name on it. So the name of the file I need to access is always the same, but the folder the file resides in will be different each time the script is run. Anyone have any idea how I can access that file in the folder with the ever-changing name?

Thank you for the answers. This is my first time doing this and I think I should have given a few more details.

I was attempting to access that file to verify that it was less than 6 hours old, which would mean the process that is designed to write it there had completed successfully. (This script audits an automated build.) I realized that I could just verify the modify time on the folder that holds the installer folder with the name that changes every night. That I can do (thanks to other posts here and on other sites).

Anyway, since I found another way to accomplish my goal, so I guess we're done! Thank you again for the answers. It's always cool to see a problem through new eyes.

Comment on Accessing a folder whose name changes daily
Re: Accessing a folder whose name changes daily
by 2teez (Priest) on Dec 18, 2012 at 15:42 UTC

    "..The name of the installer folder changes daily.."

    I think if the directory for the folder in which the files to search for resides doesn't change, you can as well use a number of perl modules which does file searching such as File::Find or File::Find::Rule

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
Re: Accessing a folder whose name changes daily
by SuicideJunkie (Priest) on Dec 18, 2012 at 15:45 UTC

    You could use localtime() to predict what the folder should be called, and validate that at the same time.

    Alternatively you could scan through all the directories and see if the file is anywhere, using opendir or File::Find

Re: Accessing a folder whose name changes daily
by salva (Monsignor) on Dec 18, 2012 at 15:50 UTC
    glob for it:
    my @candidates = < /path/root/*/filename >;

      Actually did try this and it didn't work for me. Possibly because I am running the script through a VS command prompt? Anyway, found another way to accomplish the task. Thank you though. <\p>

        then, as Jim have already stated below, try using File::Glob bsd_glob directly.
Re: Accessing a folder whose name changes daily
by rpnoble419 (Pilgrim) on Dec 18, 2012 at 17:06 UTC

    What OS are you using? Under Windows, I would setup a watch folder using Win32::ChangeNotify. Under Linux, I would glob the dir list and look for changes from the last glob.

    While testing this post I found this module on cpan you may want to look at File::ChangeNotify

Re: Accessing a folder whose name changes daily
by Jim (Curate) on Dec 18, 2012 at 17:50 UTC

    I agree with salva here. Simple globbing fits the bill nicely.

    If you're a Windows programmer, not a Unix/Linux programmer, then you may not already be familiar with the ability to use globs (wildcards) for folder names as well as for file names. As a Perl programmer, folder globbing is a powerful tool you'll want to add to your toolkit.

    Here's a concrete example using File::Glob::bsd_glob:

    C:\>perl -MFile::Glob=bsd_glob -E "say bsd_glob('C:/Program Files/Micr +osoft Office/*/WINWORD.exe')" C:/Program Files/Microsoft Office/Office12/WINWORD.exe C:\>

    (There could be a file named WINWORD.exe in more than one subfolder of C:\Program Files\Microsoft Office, so the value returned by bsd_glob is a list, not a scalar.)

    Many of my Perl scripts running on Windows use File::Glob like this:

    use English qw( -no_match_vars ); use File::Glob qw( bsd_glob ); @ARGV = map { bsd_glob($ARG) } @ARGV;

    Jim

      I do want to learn how to do this! I am on Windows too. I had tried simply using a metacharacter in the file path before I came and asked, but Perl kept telling me it couldn't use ctime on an undefined value. For some reason, no interpolation was taking place and the asterisk itself was being read as part of the path. I'm still not sure why. Thank you for posting this, especially with the examples. Examples are really helpful for someone less experienced. I will study this. Thank you again.

        I do want to learn how to do this!

        And we monks want to help you learn!

        I am on Windows too.

        I guessed that. (You mentioned "command prompt," which is Windows lingo.)

        I had tried simply using a metacharacter in the file path before I came and asked, but Perl kept telling me it couldn't use ctime on an undefined value. For some reason, no interpolation was taking place and the asterisk itself was being read as part of the path. I'm still not sure why.

        Nor can we be sure why based solely on your description of what's happening. We need more information. Show us what you tried that didn't work.

        Thank you for posting this, especially with the examples. Examples are really helpful for someone less experienced.

        Then you understand how showing us examples of your problem and the solution you tried that didn't work will help us help you.

        Jim

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1009404]
Approved by 2teez
Front-paged by 2teez
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (10)
As of 2014-04-19 18:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (483 votes), past polls