glob in specific directory

S

shumaker

I am writing a DTS package task in perl but am having trouble with the
glob statment. Basically I want to take all *.dat files in a certain
directory, read them in line by line and output them to a single output
file. I have to use full path names because I don't run the perl
script from a specific directory, it's run by the sql server. (Unless
someone knows how to set the working directory from the perl script?)

I set mydirectory to the value of a global DTS package variable, which
is a user supplied string
$mydirectory == "M:/Database Files/Import/"

open(OUT,"> ".$mydirectory."outputfilename.txt") or
die "Can't open output file: $!";

The output file is created in $mydirectory with no problem, but the
data isn't getting read in.

The script worked fine with:

while (glob "*.dat")#for each .dat file
{
open(IN,$_); #open the .dat file
.. . .

when it was a regular script being run from the directory, but when I
try to tell it to search a specific directory:

while (glob $mydirectory."*.dat")
{
open(IN,$_); #open the .dat file
.. . .

Nothing is getting written to me output file, and I'm pretty sure it's
something to do with my syntax for the glob or how I'm specifying the
path string.

PS. If my string variable contains single backslash characters instead
of forward slash characters, do the backslashes get treated as escape
sequences, or are escape sequences only interpreted in string literals?
 
M

Mark Clements

I am writing a DTS package task in perl but am having trouble with the
glob statment. Basically I want to take all *.dat files in a certain
directory, read them in line by line and output them to a single output
file. I have to use full path names because I don't run the perl
script from a specific directory, it's run by the sql server. (Unless
someone knows how to set the working directory from the perl script?) chdir?

I set mydirectory to the value of a global DTS package variable, which
is a user supplied string
$mydirectory == "M:/Database Files/Import/"
This isn't an assignment.

Mark
 
S

shumaker

"This isn't an assignment."

Sorry. I know it's not an assignment, I was just saying it is equal to
that. The actual statement in my code is:

$mydirectory =
$DTSGlobalVariables->Item("adhoc_output_dat_file_directory")->{Value};

But I was trying to avoid posting anything DTS specific for clarity.

It seems to be a problem with confusing glob because of spaces in my
path.
 
C

Chris Mattern

I am writing a DTS
DTS?

package task in perl but am having trouble with the
glob statment. Basically I want to take all *.dat files in a certain
directory, read them in line by line and output them to a single output
file. I have to use full path names because I don't run the perl
script from a specific directory, it's run by the sql server. (Unless
someone knows how to set the working directory from the perl script?)

Um, you might want to read:

perldoc -f chdir
I set mydirectory to the value of a global

Why global?
DTS package variable, which
is a user supplied string

It's a user supplied string run by the SQL server...did you remember
to untaint the variable?
$mydirectory == "M:/Database Files/Import/"

open(OUT,"> ".$mydirectory."outputfilename.txt") or
die "Can't open output file: $!";

The output file is created in $mydirectory with no problem, but the
data isn't getting read in.

The script worked fine with:

while (glob "*.dat")#for each .dat file
{
open(IN,$_); #open the .dat file

Why aren't you checking to see if your open worked, like you
did with the output file?
. . .

when it was a regular script being run from the directory, but when I
try to tell it to search a specific directory:

while (glob $mydirectory."*.dat")
{
open(IN,$_); #open the .dat file
. . .

Nothing is getting written to me output file, and I'm pretty sure it's
something to do with my syntax for the glob

No, that's fine.
or how I'm specifying the
path string.

Possibly. If you checked your open, you could find out why it's
failing. Have die() report back the name it tried to open as
well.
PS. If my string variable contains single backslash characters instead
of forward slash characters, do the backslashes get treated as escape
sequences, or are escape sequences only interpreted in string literals?

Only in string literals.

--
Christopher Mattern

"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
 
S

shumaker

Thanks.

I found documentation that glob doesn't work with directory paths that
have spaces in them.
 
S

shumaker

Just in case someone else needs the solution for making glob work with
a directory name that has spaces:

while (glob ("\Q$mydirectory*.dat") )
{

The \Q is the key.
Note that the string in $mydirectory ends with a / to seperate the
directory from the *.dat filename.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top