Hi, Kari -
There is a Windows API function that will do this for you, called PathCleanupSpec. Here is the PB external function declaration:
FUNCTION Long PathCleanupSpec ( &
String pszDir, &
REF String pszSpec &
) LIBRARY "Shell32.dll"
The first argument contains the drive and directory path for the file. The second argument contains the file's filename and extension (the file specification), and may be modified by the API function if it contains any invalid characters, which is why it must be passed by reference.
If everything's ok, the return code is zero, otherwise one or more of the following flags will be returned:
Constant Long PCS_REPLACEDCHAR = 1
Constant Long PCS_REMOVEDCHAR = 2
Constant Long PCS_TRUNCATED = 4
Constant Long PCS_PATHTOOLONG = 8
Constant Long PCS_FATAL = 2147483648
Here is an example of how to call this WinAPI function from PB:
Long ll_rc
String ls_drive_and_dir, ls_orig_filespec, ls_filespec
ls_drive_and_dir = "C:\Windows\"
ls_orig_filespec = "*This <file specification> contains **INVALID** characters???.t|x|t"
ls_filespec = ls_orig_filespec
ll_rc = PathCleanupSpec(ls_drive_and_dir,ls_filespec) // Issues RC=2
MessageBox("Path Cleanup Spec","RC = " + String(ll_rc) + &
"~r~n~r~nDir:~t" + ls_drive_and_dir + &
"~r~n~r~nBefore:~t" + ls_orig_filespec + &
"~r~n~r~nAfter:~t" + ls_filespec)
ls_drive_and_dir = "C:\"
ls_orig_filespec = "This file specification is valid!.pdf"
ls_filespec = ls_orig_filespec
ll_rc = PathCleanupSpec(ls_drive_and_dir,ls_filespec) // Issues RC=0
MessageBox("Path Cleanup Spec","RC = " + String(ll_rc) + &
"~r~n~r~nDir:~t" + ls_drive_and_dir + &
"~r~n~r~nBefore:~t" + ls_orig_filespec + &
"~r~n~r~nAfter:~t" + ls_filespec)
The following URL is the documentation for this Windows API function:
https://learn.microsoft.com/en-us/windows/win32/api/shlobj_core/nf-shlobj_core-pathcleanupspec
The documentation includes a list of the characters that are invalid in a file specification.
Best regards, John