Pulling the most recent file out of a directory

Post questions here relative to DataStage Server Edition for such areas as Server job design, DS Basic, Routines, Job Sequences, etc.

Moderators: chulett, rschirm, roy

Post Reply
SWW
Participant
Posts: 29
Joined: Thu Nov 11, 2004 12:51 pm
Location: Louisville

Pulling the most recent file out of a directory

Post by SWW »

Hey all,

I have created a process that uses a control job which directs a file to be copied to a landing area, unzipped, parts copied to main folder, process the data from the main folder, clear the landing area and main folder, and archive the zip file. I have code to map to drives that contain the file I need(datafile1). The problem is there are multiple files in that folder and I only need the most recent file.

I read in some other discussions about reading Timestamps of a file. How can I tell DS to read the Timestamp and pull the most recent file?

Any thoughts or suggestions!!

Thanks
SWKYDERBY
ketfos
Participant
Posts: 562
Joined: Mon May 03, 2004 8:58 pm
Location: san francisco
Contact:

Post by ketfos »

Hi,
you can have before job routine in datastage where you can use unix command to list files in the folder, sorted by time modified (-t)
ls -t > temp
tail -1 temp

this will output the file with latest timestamp.

Ketfos
Alexandre
Participant
Posts: 12
Joined: Fri Dec 31, 2004 5:04 am
Location: Paris
Contact:

Post by Alexandre »

ketfos wrote: ls -t > temp
tail -1 temp
You can have the last line using pipes instead of using a temporary file :

Code: Select all

ls -t | tail -1
Alexandre.
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post by chulett »

However, if you look at the original post you'll see they are (hopefully) on a Windows server. So, a Windows solution would be something similar with 'dir':

Code: Select all

dir /b /od
This gets you a 'bare' listing (just filenames) sort in date order ascending. I'm not aware of any way to reverse the listing, so you'd need to do something like capture this output using DSExecute, see how many entries are in the returned dynamic array and grab the last one.
-craig

"You can never have too many knives" -- Logan Nine Fingers
danjm
Participant
Posts: 34
Joined: Tue Sep 09, 2003 8:44 am
Location: Canada
Contact:

Post by danjm »

I think you can reverse the order by placing a hypen before the paramater you want reversed as:

dir /b /-o /d
Dan Marshall
Alberta Education
Edmonton, AB
danjm
Participant
Posts: 34
Joined: Tue Sep 09, 2003 8:44 am
Location: Canada
Contact:

Post by danjm »

actually the syntax would be more like:

dir /b/o-d

...for what you are trying to achieve.. I think.
Dan Marshall
Alberta Education
Edmonton, AB
kcbland
Participant
Posts: 5208
Joined: Wed Jan 15, 2003 8:56 am
Location: Lutz, FL
Contact:

Post by kcbland »

Since executing a system command returns screen output into an array, to process the array in reverse order is simple. Just start at the end of the array and work backwards.

Code: Select all

Call DSExecute("NT", "dir /b /o /d \overthere\thesefiles", ScreenOutput, ExitCode)
NumberOfLines=DCOUNT(ScreenOutput,@AM)
For Line=NumberOfLines To 1 STEP -1
   Filename=TRIM(ScreenOutput<Line>)
   If Filename # "" Then 
...
...
...
   End
Next Line
Kenneth Bland

Rank: Sempai
Belt: First degree black
Fight name: Captain Hook
Signature knockout: right upper cut followed by left hook
Signature submission: Crucifix combined with leg triangle
SWW
Participant
Posts: 29
Joined: Thu Nov 11, 2004 12:51 pm
Location: Louisville

Post by SWW »

Guy,

Thanks for all your help. Below is the code I'm using to try and pull the latest file. I'm not very good with loops so don't be to hard on me.

Steve


*Loop through Source Parm Dir to pull the most recent file
Call DSLogInfo("Setting up Dynamic Array Loop to pull the most recent file from Source file directory", "Job Control")

* This is going to the mapped drive and sorting the zip files

Call DSExecute("NT", "K:\UnitedTote\ dir /b /o /d *.zip", ScreenOutput, ExitCode)

NumberOfLines = DCOUNT(ScreenOutput,@AM)

for i =NumberOfLines to 1 STEP -1
Filename = TRIM(ScreenOutput<i>)

If Filename # "" THEN
*Copy file to a local drive
Call DSLogInfo("Copying files by calling batch script via DSExecute command...", "Job Control")
Call DSLogInfo("Executing script to copy files from source data dir to copy to dir ", "Job Control")

* THIS IS WHERE I'M TRYING PLACE THE FILENAME IN THE COPY CMD
*WHERE K:\UnitedTote IS THE MAPPED DRIVE AND
* Filename IS SUPPOSED TO BE THE LATEST FILE.

Call DSExecute("NT","copy K:\UnitedTote\Filename # " :sCOPY_TO_DIR_Parm, msgs, rtncd)
Call DSLogInfo("Executing command - ":sBATCH_DIR_Parm:"copy_RGS_files.bat ", "Job Control")
Call DSLogInfo("msgs is ":msgs, "Job Control")
Call DSLogInfo("rtncd is ":rtncd, "Job Control")
End
next i


Any suggestions!!!
SWKYDERBY
kcbland
Participant
Posts: 5208
Joined: Wed Jan 15, 2003 8:56 am
Location: Lutz, FL
Contact:

Post by kcbland »

Try using this:

Code: Select all

*Loop through Source Parm Dir to pull the most recent file
      Call DSLogInfo("Setting up Dynamic Array Loop to pull the most recent file from Source file directory", "Job Control")

* This is going to the mapped drive and sorting the zip files

      Call DSExecute("NT", "K:\UnitedTote\ dir /b /o /d *.zip", ScreenOutput, ExitCode)

      NumberOfLines = DCOUNT(ScreenOutput,@AM)

      for i =NumberOfLines to 1 STEP -1
         Filename = TRIM(ScreenOutput)

         If Filename # "" THEN
*Copy file to a local drive
            Call DSLogInfo("Copying files by calling batch script via DSExecute command...", "Job Control")
            Call DSLogInfo("Executing script to copy files from source data dir to copy to dir ", "Job Control")

* THIS IS WHERE I'M TRYING PLACE THE FILENAME IN THE COPY CMD
*WHERE K:\UnitedTote IS THE MAPPED DRIVE AND
* Filename IS SUPPOSED TO BE THE LATEST FILE.
            CopyCommand = "copy K:\UnitedTote\":Filename:" ":sCOPY_TO_DIR_Parm
            Call DSLogInfo("Copy command: ":CopyCommand, "Info")
            Call DSExecute("NT",CopyCommand, msgs, rtncd)


            Call DSLogInfo("Executing command - ":sBATCH_DIR_Parm:"copy_RGS_files.bat ", "Job Control")

            Call DSLogInfo("msgs is ":msgs, "Job Control")
            Call DSLogInfo("rtncd is ":rtncd, "Job Control")
         End
      next i
Kenneth Bland

Rank: Sempai
Belt: First degree black
Fight name: Captain Hook
Signature knockout: right upper cut followed by left hook
Signature submission: Crucifix combined with leg triangle
SWW
Participant
Posts: 29
Joined: Thu Nov 11, 2004 12:51 pm
Location: Louisville

Post by SWW »

Kenneth,

Here is the code I used. The parameter Filename is not being populated correctly. I listed the log messages after where they occurred. Seems it doesn't like something relating to the VOC.

Also is there some way to see what value Filename takes on each time. If I knew it was actually going to the mapped drive and pulling a value, that's half the battle.

Thanks for all your time.

Steve



*Loop through Source Parm Dir to pull the most recent file
Call DSLogInfo("Setting up Dynamic Array Loop to pull the most recent file from Source file directory", "Job Control")
* This is going to the mapped drive and sorting the zip files
Call DSExecute("NT", "K:\UnitedTote\ dir /b /o /d *.zip", ScreenOutput, ExitCode)
NumberOfLines = DCOUNT(ScreenOutput,@AM)
for i =NumberOfLines to 1 STEP -1
Filename = TRIM(ScreenOutput)
If Filename # "" THEN
*Copy file to a local drive
Call DSLogInfo("Copying files by calling batch script via DSExecute command...", "Job Control")
Call DSLogInfo("Executing script to copy files from source data dir to copy to dir ", "Job Control")
* THIS IS WHERE I'M TRYING PLACE THE FILENAME IN THE COPY CMD
*WHERE K:\UnitedTote IS THE MAPPED DRIVE AND
* Filename IS SUPPOSED TO BE THE LATEST FILE.
CopyCommand = "copy K:\UnitedTote\":Filename: " ":sCOPY_TO_DIR_Parm
Call DSLogInfo("Copy command: ":CopyCommand, "Info")

THIS IS THE MESSAGE IN THE LOG
Message:
CTRL_JOB_RGS..JobControl (Info): Copy command: copy K:\UnitedTote\'K:\UnitedTote\' is not recognized as an internal or external command,
operable program or batch file.
D:\Tote_Raw\RGS\RGS_Zip\

Call DSExecute("NT",CopyCommand, msgs, rtncd)
Call DSLogInfo("Executing command - ":sBATCH_DIR_Parm:"copy_RGS_files.bat ", "Job Control")
Call DSLogInfo("msgs is ":msgs, "Job Control")


THIS IS THE MESSAGE IN THE LOG
Message:
CTRL_JOB_RGS..JobControl (Job Control): msgs is The syntax of the command is incorrect.
Verb "OPERABLE" is not in your VOC.
Verb "D:\TOTE_RAW\RGS\RGS_ZIP\" is not in your VOC.




Call DSLogInfo("rtncd is ":rtncd, "Job Control")
End
next i
SWKYDERBY
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

You may have extra line terminators in ScreenOutput. These will have been translated into field marks (@FM). Try:

Code: Select all

Filename = Trim(Convert(@FM,"",ScreenOutput))
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
Post Reply