glob in specific directory

Discussion in 'Perl Misc' started by shumaker@cs.fsu.edu, Apr 20, 2005.

  1. Guest

    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?
     
    , Apr 20, 2005
    #1
    1. Advertising

  2. wrote:
    > 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
     
    Mark Clements, Apr 20, 2005
    #2
    1. Advertising

  3. Guest

    "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.
     
    , Apr 20, 2005
    #3
  4. wrote:

    > 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?"
     
    Chris Mattern, Apr 20, 2005
    #4
  5. Guest

    Thanks.

    I found documentation that glob doesn't work with directory paths that
    have spaces in them.
     
    , Apr 20, 2005
    #5
  6. Guest

    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.
     
    , Apr 20, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Georgy Pruss
    Replies:
    15
    Views:
    733
    Tim Roberts
    Dec 1, 2003
  2. Tim Peters
    Replies:
    1
    Views:
    366
    Duncan Booth
    Dec 1, 2003
  3. Sean Berry

    Question about glob.glob <--newbie

    Sean Berry, May 4, 2004, in forum: Python
    Replies:
    3
    Views:
    350
    David M. Cooke
    May 4, 2004
  4. Elbert Lev

    glob.glob unicode bug or feature

    Elbert Lev, Jul 31, 2004, in forum: Python
    Replies:
    5
    Views:
    404
    Neil Hodgson
    Aug 2, 2004
  5. lameck kassana
    Replies:
    0
    Views:
    300
    lameck kassana
    Feb 26, 2009
Loading...

Share This Page