View Raw SPL
/*****************************************************************************
* *
* IMPORTSELECT.SPL Copyright (C) 2002,2025 DSP Development Corporation *
* All Rights Reserved *
* *
* Author: Rabin Tamang *
* *
* Synopsis: Imports data files into a specified directory. *
* *
* Creation: 27 Mar 2002 RPT Creation *
* 21 Mar 2025 RRR updated *
* *
*****************************************************************************/
#include
#if @HELP_IMPORTSELECT
IMPORTSELECT
Purpose: Imports DADiSP formatted files that reside in a specified folder.
Syntax: IMPORTSELECT("fpath", "fnames", "hedfiles", overwrite, resethead, silent, skiphead, delimit)
"fpath" - A string. The source folder.
"fnames" - Optional. A string, the file names separated by commas.
If not specified or "", defaults to all the files in
FPATH.
"hednames" - Optional. A string, the header file names separated by
commas. If not specified or "", the header file for each
file name is the file name with extension ".hed". The
header file is ignored if it does not exist.
overwrite - Optional. An integer, the overwrite mode if the series
already exists.
0: Prompt User
1: Overwrite (default)
2: Append
3: Skip
resethead - Optional. A 0 or 1 integer, reset header values to
defaults after import. Defaults to 0, do not reset.
silent - Optional. A 0 or 1 integer, suppress import messages.
Defaults to 0, display messages.
skiphead - Optional. A 0 or 1 integer, skip embedded header values
in file. Defaults to 0, do not skip embedded header
values.
delimit - Optional. A string, the file list delimiter. Defaults
to comma (",").
Returns: A message with the number of files imported.
Example:
importselect(gethome+"Modules\impselect\data")
Imports all the files in the Modules\impselect\data folder.
Example:
importselect(gethome+"Modules\impselect\data", "sin.dat, tri.dat, sqr.dat", 2)
Imports all the files "sin.dat", tri.dat" and sqr.dat" in the
Modules\impselect\data folder. If the series already exist, the
new data is appended to the existing data.
Remarks:
A status message is displayed upon completion.
Files that are not DADiSP formatted files will return multiple errors
and the file will not be imported correctly.
#endif
/* import configuration state */
static impsel_rst = {};
static impsel_sil = {};
static impsel_hdr = {};
/* import a folder of files with optional name list */
importselect(fpath="", fnames="", hednames="", overwrite=1, resethed=0, silent=0, skiphed=0, delimit=",")
{
local wpath, numfiles, j, filename, hed, cnt = 0, status;
/* parse inputs */
(fpath, fnames, hednames, overwrite, resethed, silent, skiphed, delimit) = importselect_parse_args(fpath, fnames, hednames, overwrite, resethed, silent, skiphed, delimit);
/* check source folder */
if (not(direxists(fpath)))
{
error(sprintf(_impselerr_fne, __FUNC__, fpath));
}
/* add trailing PATHCHAR */
if (not(fpath[strlen(fpath)] == PATHCHAR))
{
fpath += PATHCHAR;
}
if (strlen(fnames) == 0)
{
/* all files excluding header files */
fnames = importselect_filelist(fpath, 0, ".hed");
}
else if (importselect_has_wildcard(fnames))
{
/* match wildcards */
wpath = fpath + fnames;
fnames = importselect_filelist(wpath, 0, ".hed");
}
/* convert \n to delimiter */
fnames = strrep(fnames, strescape("\n"), delimit);
/* configuration options */
impsel_rst = getconfig("imp_reset_defaults");
impsel_sil = getconfig("imp_silent_flag");
impsel_hdr = getconfig("imp_skip_header");
setconfig("imp_reset_defaults", resethed);
setconfig("imp_silent_flag", silent);
setconfig("imp_skip_header", skiphed);
j = 0;
while (1)
{
j++;
/* get name from list */
filename = strget(j, fnames, delimit);
/* remove leading whitespace */
filename = strtrim(filename);
if (strlen(filename) == 0)
{
break;
}
/* data file */
filename = fpath + filename;
/* header file */
hed = importselect_hedfile(filename, hednames, j, delimit);
if (not(silent))
{
echo(sprintf(_impselstr_idd, j, filename));
}
/* import with or without header file */
if (strlen(hed) > 0)
{
status = importfile(filename, hed, 0, 0, 0, 0, overwrite, 1);
}
else
{
status = importfile(filename, 0, 0, 0, 0, overwrite, 1);
}
if (status == _IMPSEL_SKIP_SERIES)
{
/* this one is skipped */
continue;
}
else if (status == _IMPSEL_CANCEL_ALL)
{
/* end */
break;
}
else if (status == _IMPSEL_OVERWRITE_ALL)
{
/* replace going forward */
overwrite = 1;
}
else if (status == _IMPSEL_APPEND_ALL)
{
/* append going forward */
overwrite = 2;
}
cnt++;
}
if (not(silent))
{
/* print "Imported N Files" */
printf(_impselstr_ids, cnt, (cnt == 1) ? _impselstr_fil : _impselstr_fls);
}
/* restore configuration parameters */
setconfig("imp_reset_defaults", impsel_rst);
setconfig("imp_silent_flag", impsel_sil);
setconfig("imp_skip_header", impsel_hdr);
/* reset */
impsel_rst = {};
impsel_sil = {};
impsel_hdr = {};
}
/* parse inputs */
importselect_parse_args(argv)
{
local path = {}, names = {}, hnames = {}, ovr = {}, resethed = {}, silent = {}, skiphed = {}, delimit = {};
loop (j = 1..argc)
{
if (isstring(getargv(j)))
{
if (isarray(path))
{
path = getargv(j);
}
else if (isarray(names))
{
names = getargv(j);
}
else if (isarray(hnames))
{
hnames = getargv(j);
}
else if (isarray(delimit))
{
delimit = getargv(j);
}
}
else if (isscalar(getargv(j)))
{
if (isarray(ovr))
{
ovr = getargv(j);
}
else if (isarray(resethed))
{
resethed = getargv(j);
}
else if (isarray(silent))
{
silent = getargv(j);
}
else if (isarray(skiphed))
{
skiphed = getargv(j);
}
}
}
if (isarray(path))
{
path = dirpath();
}
if (isarray(names))
{
names = "";
}
if (isarray(hnames))
{
hnames = "";
}
if (isarray(ovr))
{
ovr = 1;
}
if (isarray(resethed))
{
resethed = 0;
}
if (isarray(silent))
{
silent = 0;
}
if (isarray(skiphed))
{
skiphed = 0;
}
if (isarray(delimit))
{
delimit = ",";
}
return(path, names, hnames, ovr, resethed, silent, skiphed, delimit);
}
/* returns a newline delimited list of files */
importselect_filelist(spec, recurse, exclude)
{
local tempfile, cmd, options, s = "";
if (argc < 3)
{
if (argc < 2)
{
if (argc < 1) spec = "*.*";
recurse = 0;
}
exclude = "";
}
/* flag to recurse into sub-directories */
options = recurse ? "/S" : "";
/* build temporary file */
tempfile = getmiscpath(1, 1) + "templist.txt";
/* directory listing command */
if (strlen(exclude) > 0)
{
cmd = sprintf('dir "%s" /b %s | findstr /v /i "\%s$"> "%s"', spec, options, exclude, tempfile);
}
else
{
cmd = sprintf('dir "%s" /b %s > "%s"', spec, options, tempfile);
}
/* run listing */
run(cmd, -1);
/* check tempfile existence */
if (fstat(tempfile) > 0)
{
/* return listing as a string */
s = strfile(tempfile, 0, 1, 0, 0);
delfile(tempfile);
}
return(s);
}
/* get header file from list or generate from datafile name */
importselect_hedfile(fname, hednames, idx, delimit)
{
local path, file, ext, hedname = "";
if (strlen(fname) > 0)
{
if (strlen(hednames) > 0)
{
hedname = strget(idx, hednames, delimit);
}
else
{
(path, file, ext) = fileparts(fname);
hedname = path + PATHCHAR + file + ".hed";
if (not(fileexists(hedname)))
{
hedname = "";
}
}
}
return(hedname);
}
/* does filespec have * or ? */
importselect_has_wildcard(s)
{
local status;
status = (strlen(strfind("*", s)) > 0) || (strlen(strfind("?", s)) > 0);
return(status);
}
/* error handler */
importselect_error(errnum, errmes)
{
if (not(isempty(impsel_rst)))
{
setconfig("imp_reset_defaults", impsel_rst);
impsel_rst = {};
}
if (not(isempty(impsel_sil)))
{
setconfig("imp_silent_flag", impsel_sil);
impsel_sil = {};
}
if (not(isempty(impsel_hdr)))
{
setconfig("imp_skip_header", impsel_hdr);
impsel_hdr = {};
}
error(errmes);
}