check file existance in progress 4GL - progress-4gl

How to check existance of particular file by use of code.
Eg.
def var a as character.
a = "abc.p"
run value(a).
---> here first i want to check if abc.p exist in workspace or not.

You can use the SEARCH function. Directly from the online manual:
SEARCH function
Searches the directories and libraries defined in the PROPATH environment variable for a file. The SEARCH function returns the full pathname of the file unless it is found in your current working directory. If SEARCH does not find the file, it returns the Unknown value (?).
Syntax
SEARCH ( opsys-file )
opsys-file
A character expression whose value is the name of the file you want to find. The name can include a complete or partial directory path. If opsys-file is a constant string, you must enclose it in quotation marks (" "). The value of opsys-file must be no more than 255 characters long.
Example:
DEFINE VARIABLE cPgm AS CHARACTER NO-UNDO.
cPgm = "test.p".
IF SEARCH(cPgm) <> ? THEN
RUN VALUE(cPgm).
If you provide a fully qualified pathname, SEARCH checks if the file exists. In this case, SEARCH does not search directories on the PROPATH.

If you do not want to use the propath you can use the FILE-INFO system handle.
After setting FILE-NAME, you can check the FILE-TYPE if it exists. See also the Progress Help for FILE-INFO.
FILE-INFO:FILE-NAME = a.
IF FILE-INFO:FILE-TYPE MATCHES "*F*"
THEN RUN VALUE(FILE-INFO:FULL-PATHNAME).

Related

Filename pattern validation in adf v2

I would like to validate my input filename whether it's in specified name.
Like my filename should be <><><>_<>.csv
Yes i am using event based i will get filename from trigger.
expected format: company_contry_yearmonth_timestamp.CSV
There is no explicit regex way of validating if the incoming file name matches a pattern. But if you are using activity like lookup or copy activity. You can specify in the source dataset settings a wildcard file name or file path to fetch a file matching the pattern.
- wildcardFileName
The file name with wildcard characters under the given container and
folder path (or wildcard folder path) to filter source files. Allowed
wildcards are: * (matches zero or more characters) and ? (matches zero
or single character). Use ^ to escape if your file name has a wildcard
or this escape character inside. See more examples in Folder and file
filter examples.
example:
You can use a if condition, with an expression as below using contains()
Here a storage event trigger, gets the trigged file name into a parameter. We then use contains() function to see if the file name contains a specified string
#contains(pipeline().parameters.filenameTriggered,'pattern')
If true a wait activity is executed.

Warning Control Character '\S' is not valid when concatinating two strings

I have two variables such as:
path='data\voc11\SegmentationClassExt\%s.png'
name='123'
I want to concatenate two strings into one like so:
data\voc11\SegmentationClassExt\123.png
I used the code below:
sprintf(path, name)
However I receive the following error:
Warning: Control Character '\S' is not valid. See 'doc sprintf' for control characters valid in the format string.
ans =
dataoc11
I am using MATLAB on Windows. Could you give me any solution for that. I tried to change path='data\\voc11\\SegmentationClassExt\\%s.png' and when I did that, the above code will work. However, the current data is
path='data\voc11\SegmentationClassExt\%s.png';
use the matlab function fullfile
filename = fullfile ( path, [name '.png'] );
or
filename = fullfile ( path, sprintf ( '%s.png', name ) );
Note: you should avoid using path as a variable as it is already a Matlab function
Before we start, it's highly advised that you do not use path as a local variable. path is a global variable that MATLAB uses to resolve function scope, especially if you are going to use any functions from toolboxes. Overwriting path with your own string will actually make MATLAB not function properly. Use a different variable name.
Now to resolve your problem, you can use either fullfile as what #matlabgui has suggested, or if you don't care about OS compatibility and are only working in Windows, you can either manually change the path as you have placed so that you can introduce two back slashes and it will indeed work on Windows OS, or you can perhaps use a string replace function so that all back slashes will be accompanied with an additional back slash.
Either one of these two methods will work:
Method 1 - Using regular expressions
pat = 'data\voc11\SegmentationClassExt\%s.png';
pat_new = regexprep(pat, '\\', '\\\\');
The function regexprep performs a string replacement by regular expressions. We search for all single backslashes and replace them with double backslashes. Note that the single back slash \ is a special character in regular expressions so if you explicitly what to look for back slashes, you must place an additional back slash beside it.
Method 2 - Using strrep
pat = 'data\voc11\SegmentationClassExt\%s.png';
pat_new = strrep(pat, '\', '\\');
strrep stands for String Replace. It works very similar to regular expressions as we have discussed above. However, what's nice is that you don't have to append an additional back slash when looking for the actual character.
Once you do this, you can use sprintf as normal:
pat_new = sprintf(pat_new, name);

Store user input as wildcard

I am having some trouble with a data processing function in MATLAB. The function takes the name of the file to be processed as an input, finds the desired files, and reads in the data.
However, several of the desired files are variants, such as Data_00.dat, Data.dat, or Data_1_March.dat. Within my function, I would like to search for all files containing Data and condense them into one usable file for processing.
To solve this, I would like desiredfile to be converted into a wildcard.
Here is the statement I would like to use.
selectedfiles = dir *desiredfile*.dat % Search for file names containing desiredfile
This returns all files containing the variable name desiredfile, rather than the user input.
The only solution that I can think of is writing a separate function that manually condenses all the variants into one file before my function is run, but I am trying to keep the number of files used down and would like to avoid this.
You could concatenate strings for that. Considering desiredFile as a variable.
desiredFile = input('Files: ');
selectedfiles = dir(['*' desiredfile '*.dat']) % Search for file names containing desiredfile
Enclosing strings between square brackets [string1 string2 ... stringN]concatenates them. Matlab's dir function receives a string.
I believe you can achieve that using the dir command.
dataSets = dir('/path/to/dir/containing/Data*.dat');
dataSets = {dataSets.name};
Now simply loop over them, more information here.
To quote the matlab help:
dir lists the files and folders in the MATLABĀ® current folder. Results appear in the order returned by the operating system.
dir name lists the files and folders that match the string name. When name is a folder, dir lists the contents of the folder. Specify name using absolute or relative path names. You can use wildcards (*).

Name of files in a specific directory using matlab

I want to find the name of files in a specific directory. I know that the dir command return the name of files but it contains the file name with the their extension. Therefore, I used strfind to remove the extension of files as follows:
a = dir(fullfile(dataset_path, [dataset_category '\qrel']))
for i= 3: length(a)
name{i} = a(i).name(1:strfind(a(i).name, '.')-1)
I want a better approach without loop. I wonder is it a way to use vectorization for this purpose. I used the following code but it return an error
a = dir(fullfile(dataset_path, [dataset_category '\qrel']))
name = a.name(1:strfind(a.name, '.')-1)
You can do that with regular expressions:
name = regexprep({a.name}, '\.[^\.]*$', '');
This collects all names in a cell array ({a.name}). For each string it matches a dot (\.) followed by zero or more characters other than a dot ([^\.]*) at the end of the string ($), and removes that. Thanks to #Shai for the "other than a dot" correction, which makes sure that only the final dot is matched.

How can I extract a single file from a ZIP archive using Perl's Archive::Zip?

I have a zip file X and I'd like do extract a single file, located in x/x/x/file.txt. How do I do this using Archive::Zip and Perl?
You can use the extractMember method:
extractMember( $memberOrName [, $extractedName ] )
Extract the given member, or match its name and extract it. Returns undef if member doesn't exist in this Zip. If optional second arg is given, use it as the name of the extracted member. Otherwise, the internal filename of the member is used as the name of the extracted file or directory. If you pass $extractedName, it should be in the local file system's format. All necessary directories will be created. Returns AZ_OK on success.
See Archive::Zip::FAQ, "extract file(s) from a Zip". The current version of the example file is online at http://cpansearch.perl.org/src/ADAMK/Archive-Zip-1.30/examples/extract.pl.