We help IT Professionals succeed at work.

Reading a filename

Simon336697 asked
Medium Priority
Last Modified: 2010-04-16
Hi guys!

Hope you can help.

I want to, through perl, do the following....

Find a particular file.
The file is:         sdat*.zip

The path is: c:\sdat*.zip
where sdat could be sdat<any number or numbers>.zip
eg.sdat328.zip, sdat542.zip, any number at all.

I want to be able to:
a) Determine if sdat*.zip exists (if -e)
b) Read the filename (not read its contents), but the name of the file into a variable, minus the dot and file extension, and minus the sdat part, so only the number is to go into a variable.
c) Compare this number against another eg.5000
d) blah blah.

Basically, its just the first 2 parts i need help with.

Any help greatly appreciated.


Watch Question

Top Expert 2007
foreach (<C:/sdat*.zip>) {
  if (/sdat(\d+)\.zip/) {
     my $num  = $1;

     if ($num == 5000) {
        print "$num equals 5000\n";
     else {
        print "$num does not equal 5000\n";


Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts


Thanks Tintin champion.

Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.