| Author |
Finding "this file name".
|
|
| hbest@hotmail.com 2006-01-17, 3:55 am |
| In JavaScript, I use the following to get "this file name" (the name
of the file that the JavaScript code is in):
temp1=location.pathname;
temp2=temp1.lastIndexOf("?");
if(temp2<0)
temp2=temp1.length;
temp1=temp1.slice((temp1.lastIndexOf("/")+1),temp2);
thisFileName=temp1.slice((temp1.lastIndexOf("\\")+1));
How can I do this in Perl so that it comes up with the long version of
the file name? (I've already figured out how to get the short
version.)
| |
| usenet@DavidFilmer.com 2006-01-17, 3:55 am |
| hbest@hotmail.com wrote:
> In JavaScript, I use the following to get "this file name" (the name
> of the file that the JavaScript code is in):
> How can I do this in Perl so that it comes up with the long version of
> the file name?
The "safe and proper" way to do this is to use the FindBin module (a
core module that comes with Perl):
#!/usr/bin/perl
use warnings; use strict;
use FindBin;
print "This program is $FindBin::Bin/$FindBin::Script\n"
__END__
You can use RealBin and RealScript to have all links resolved for you,
if you prefer.
| |
| hbest@hotmail.com 2006-01-19, 3:57 am |
| I tried (under Windows ME):
use FindBin;
print "This program is $FindBin::Bin/$FindBin::Script\n"
__END__
....but again all I got was the short version of the file name. HELP!!!
Howard Charles
| |
| usenet@DavidFilmer.com 2006-01-19, 3:57 am |
| hbest@hotmail.com wrote:
> ...but again all I got was the short version of the file name. HELP!!!
Oh, sorry, I didn't understand what you meant by "short" and "long"
name. I thought you were talking about the filename with and without
the path. But I think you're talking about Windows "extended"
filenames (where the "long" name is somehow part of the file inode or
something, but the file is still really limited to the legacy DOS 8.3
stuff).
Urp, sorry, I'm a UN*X weenie. Any Windows weenies out there wanna
chime in? How do you determine the Windows extended filename given the
real filename?
| |
| Paul Lalli 2006-01-19, 7:56 am |
|
usenet@DavidFilmer.com wrote:
> hbest@hotmail.com wrote:
>
> Oh, sorry, I didn't understand what you meant by "short" and "long"
> name. I thought you were talking about the filename with and without
> the path. But I think you're talking about Windows "extended"
> filenames (where the "long" name is somehow part of the file inode or
> something, but the file is still really limited to the legacy DOS 8.3
> stuff).
>
> Urp, sorry, I'm a UN*X weenie. Any Windows weenies out there wanna
> chime in? How do you determine the Windows extended filename given the
> real filename?
perldoc Win32
Take a look at the function GetLongPathName
Never used it, but it certainly sounds like what the OP is asking for.
A quick test on my Windows box using ActiveState Perl confirms:
C:\>perl -MWin32 -le"print Win32::GetLongPathName('docume~1')"
Documents and Settings
Paul Lalli
| |
| Howard Charles 2006-01-21, 6:56 pm |
| Thank you very much, David and Paul. Between the two of you, my problem
is solved!
I never tried the "-MWin32 -le" perl command line switches, but the
following worked just fine without them:
use FindBin;
$thisFilePath=Win32::GetLongPathName("$FindBin::Bin");
$thisFileName=Win32::GetLongPathName("$FindBin::Script");
print "This path name: $thisFilePath\n";
print "This file name: $thisFileName\n";
__END__
Howard Charles
| |
| Paul Lalli 2006-01-22, 7:03 pm |
| Howard Charles wrote:
> I never tried the "-MWin32 -le" perl command line switches,
perldoc perlrun
-M means to load the following module
-l means to auto-chomp any input and auto-append a newline to any
output
-e means to execute the code in the following string, rather than
looking for a file or code in standard input
Paul Lalli
| |
| Howard Charles 2006-01-25, 6:57 pm |
| This is very strange.
The following works just fine:
#!/usr/local/bin/perl -w -l
use FindBin;
print 'This path name: '.Win32::GetLongPathName($FindBin::Bin);
print 'This file name: '. Win32::GetLongPathName($FindBin::Script)
;
__END__
....but when I execute the same two statements as two separate DOS
commands in a .bat file (Windows ME) as follows:
perl -MFindBin -le "print 'This path name:
'.Win32::GetLongPathName($FindBin::Bin)"
perl -MFindBin -le "print 'This file name:
'. Win32::GetLongPathName($FindBin::Script)
"
....the first DOS command works, but the second one doesn't! The second
one prints a null string for the
" Win32::GetLongPathName($FindBin::Script)
"!
Howard Charles
| |
| Paul Lalli 2006-01-25, 6:57 pm |
| Howard Charles wrote:
> This is very strange.
>
> The following works just fine:
>
> #!/usr/local/bin/perl -w -l
> use FindBin;
> print 'This path name: '.Win32::GetLongPathName($FindBin::Bin);
> print 'This file name: '. Win32::GetLongPathName($FindBin::Script)
;
> __END__
>
> ...but when I execute the same two statements as two separate DOS
> commands in a .bat file (Windows ME) as follows:
>
> perl -MFindBin -le "print 'This path name:
> '.Win32::GetLongPathName($FindBin::Bin)"
> perl -MFindBin -le "print 'This file name:
> '. Win32::GetLongPathName($FindBin::Script)
"
>
> ...the first DOS command works, but the second one doesn't! The second
> one prints a null string for the
> " Win32::GetLongPathName($FindBin::Script)
"!
I'm as to what you were expecting to happen. That code is in
a one-liner. There is no script, and therefore no script name. In this
case, Perl will report the script name as "-e", since the code is
coming from a -e command line switch. Passing that to GetLongPathName
results in an empty string, as Windows has know knowledge of any path
named "-e".
What value were you hoping for this code to produce?
Paul Lalli
| |
| Howard Charles 2006-01-26, 9:55 pm |
| Paul Lalli wrote:
> I'm as to what you were expecting to happen. That code is in
> a one-liner. There is no script, and therefore no script name. In this
> case, Perl will report the script name as "-e", since the code is
> coming from a -e command line switch. Passing that to GetLongPathName
> results in an empty string, as Windows has know knowledge of any path
> named "-e".
>
> What value were you hoping for this code to produce?
I was hoping that it would give the name of the .bat file.
It's not so important. I was just surprised that it didn't work, but
thanks to you, Paul, I now can see that there is a logical explanation
for why it doesn't work.
Howard Charles
| |
| Paul Lalli 2006-01-27, 7:56 am |
| Howard Charles wrote:
> Paul Lalli wrote:
>
> I was hoping that it would give the name of the .bat file.
I can't see how that would be possible. How is Perl to know that it
was called from a batch file rather than from a command line, a CGI
invocation, a cron job, or any other method?
(Note: I say "I can't see how..." because I honestly don't know. It is
entirely possible there is a method to determine this information that
I'm simply not familiar with.)
Paul Lalli
|
|
|
|