SAP Data Services File Handling Functions
Over the time, I have worked with various ways to read a file, check if file exists and so on. Here, I would like to share what I have learned thus far which someone might find useful. Rest, please ignore if you already know the below.
Assuming DS is installed on windows server, if you are using Linux replace path of cmd to Linux shell.
Check File Exists
ltrim_blanks(word_ext(exec('cmd','dir "[$G_Input_Filepath]" /o /b',8),1,':')) = '0'
1) You basically use the dos directory command with switch /o /b so that it returns list output.
2) Split the string using word_ext to check if file exists.
3) The output of exec if file not found will be as below
4) If file is found,
Here is the complete script if you want to test
$path = 'D:\Test.txt'; print(exec('cmd','dir "[$path]" /o /b',8)); print(word_ext(exec('cmd','dir "[$path]" /o /b',8),1,':')); print(ltrim_blanks(word_ext(exec('cmd','dir "[$path]" /o /b',8),1,':')));
$Flag = exec('c:\\windows\\system32\\cmd.exe', '/c '||$path,2);
If file is found it returns,
You could just check if $Flag is not null to make sure the file exists.
If file is not found, you get the below output,
The advantage of above 2 methods is that you can use wildcards while with file_exists() you can’t.
file_exists("filename.txt") = 1 #works file_exists("Filename.???") = 1 # does not
You could also use
wait_for_file($Path,0, 1000) = 1
This function also supports wildcard file names and UNC paths.
Enforce File Name Pattern
Sometimes you might want to enforce certain pattern in file name while reading in data services to avoid reading the wrong files.
For this purpose, you can use placeholders while defining the file names. I normally use a job control table where I define the file path and file name for every file that will be read.
Assume the filename entry or variable is set to “Open Orders – ????-??.xls”
When this is passed to the any of the above methods, it looks for any file that matches the pattern defined.
So the job will pickup “Open Orders – 2015-04.xls” and will ignore if it has “Open Orders – 2015-4.xls” or “Open Orders – 201504.xls”
This way you can enforce only certain file name patterns to be read by the job.