ParaMonte Fortran 2.0.0
Parallel Monte Carlo and Machine Learning Library
See the latest version documentation.
pm_sysPath::isFailedRemove Interface Reference

Generate and return .true. if the requested path removal action from the file system failed, otherwise return .false. indicating success.
More...

Detailed Description

Generate and return .true. if the requested path removal action from the file system failed, otherwise return .false. indicating success.

The procedures of this generic interface perform the path removal action whether the input path points to a file or an empty or non-empty directory.
The removal strategy depends on the nature of the input path:

  1. If the input path points to a single file, then the path is removed from the file system using Fortran intrinsic routines.
  2. If the input path points to a multiple files via a pattern specification or a directory, then the path is removed via the relevant shell commands.
Parameters
[in]path: The input scalar character of default kind SK containing a POSIX-style or Windows-style path to a file or directory.
[in]recursive: The input scalar logical of default kind LK. If .true. the removal action will be performed recursively (including all subdirectories).
This input argument is relevant only if the input path is a directory potentially with subdirectories.
(optional, default = .false.)
[in]forced: The input scalar logical of default kind LK. If .true. the removal action will be performed without any questions asked (and nonexistent files will be ignored).
Note that setting force = .true. is required to ignore errors raised on Windows related to non-existence of the specified path or pattern.
(optional, default = .false.)
[in]wait: The input scalar logical of default kind LK with the same functionality as the wait argument of the Fortran intrinsic execute_command_line().
  1. If .true., the procedure will wait for the shell action to terminate before returning the control to the Fortran program.
  2. Otherwise, the system call will be done asynchronously and the success of the action will not be verified.
(optional, default = .true.)
[in]ntry: The input scalar positive integer of default kind IK representing the number of times the requested action should be attempted.
Multiple tries are particularly useful on Windows platforms where applications take the ownership of a particular resource on the system and may not allow other applications to utilize the resource.
For example, Dropbox is a well-known example of an application that blocks any other applications from making any changes to a specific path with which it is working, preventing any manipulation of the path by other applications.
(optional, default = 1)
[in,out]errmsg: The input/output scalar character of default kind SK of arbitrary length type parameter.
If present and an error occurs, it is assigned an explanatory message describing the nature of the error that has occurred.
If the length of errmsg is too short for the output error message, the message tail will be clipped as needed.
A length of LEN_IOMSG characters is likely sufficient to capture most error messages in full.
(optional. If missing, no error message will be output.)
Returns
failed : The output scalar logical of default kind LK. It is .true. if and only if an error occurs while performing the requested task.
This includes the possibility of the input path not existing before the removal action.


Possible calling interfaces

use pm_kind, only: LK, IK, SK
character(2047, SK) :: errmsg
logical(LK) :: failed, forced
logical(LK) :: recursive
logical(LK) :: wait
integer(IK) :: ntry
failed = isFailedRemove(path, recursive = recursive, forced = forced, wait = wait, ntry = ntry, errmsg = errmsg)
Generate and return .true. if the requested path removal action from the file system failed,...
This module defines the relevant Fortran kind type-parameters frequently used in the ParaMonte librar...
Definition: pm_kind.F90:268
integer, parameter LK
The default logical kind in the ParaMonte library: kind(.true.) in Fortran, kind(....
Definition: pm_kind.F90:541
integer, parameter IK
The default integer kind in the ParaMonte library: int32 in Fortran, c_int32_t in C-Fortran Interoper...
Definition: pm_kind.F90:540
integer, parameter SK
The default character kind in the ParaMonte library: kind("a") in Fortran, c_char in C-Fortran Intero...
Definition: pm_kind.F90:539
This module contains classes and procedures for manipulating system file/folder paths.
Definition: pm_sysPath.F90:274
Warning
Note that this procedure does not verify the conformance of the input paths to the naming conventions of the current processor shell. For example,
  1. if the shell is POSIX-conforming, then the path is expected to conform to the POSIX conventions.
  2. if the shell is Windows CMD or PowerShell, the path is expected to conform to the Windows conventions.
This procedure interprets the input paths verbatim.
This procedure potentially calls the operating system to create the input destination path directories.
To minimize security vulnerabilities and to properly handle the presence of whitespace or other potentially problematic characters, the input path will be interpreted verbatim by calling getPathVerbatim.
Asynchronous removal action.
Note that even if the processor supports asynchronous command execution (wait = .false.), there is no mechanism provided for finding out later whether the command being executed asynchronously has terminated or what its exit status exitstat was.
The input ntry must be a positive (non-zero) integer.
This condition is verified only if the library is built with the preprocessor macro CHECK_ENABLED=1.
Remarks
The procedures under discussion are impure.
The procedures under discussion are recursive.
See also
isFailedCopy
isFailedMove
isFailedRemove
isFailedMakeDir


Example usage

1program example
2
3 use pm_kind, only: LK, IK, SK
4 use pm_io, only: display_type
5 use pm_container, only: css_type
8
9 implicit none
10
11 integer :: i, unit
12
13 type(display_type) :: disp
14 disp = display_type(file = "main.out.F90")
15
16 call disp%skip()
17 call disp%show("!%%%%%%%%%%%%%")
18 call disp%show("! Remove file.")
19 call disp%show("!%%%%%%%%%%%%%")
20 call disp%skip()
21
22 block
23 character(:, SK), allocatable :: path, dir
24 call disp%skip()
25 call disp%show("dir = getPathNew(prefix = SK_'tempdir_') ! define a temp directory name.")
26 dir = getPathNew(prefix = SK_'tempdir_')
27 call disp%show("dir")
28 call disp%show( dir , deliml = SK_"""" )
29 call disp%show("if (isFailedMakeDir(dir)) error stop 'directory creation failed.'")
30 if (isFailedMakeDir(dir)) error stop 'directory creation failed.'
31 call disp%show("path = getPathNew(dir) ! define a temp path name.")
32 path = getPathNew(dir)
33 call disp%show("path")
34 call disp%show( path , deliml = SK_"""" )
35 call disp%show("open(newunit = unit, file = path, status = 'replace'); close(unit) ! Create a file in a randomly-named directory.")
36 open(newunit = unit, file = path, status = 'replace'); close(unit) ! Create a file in a randomly-named directory.
37 call disp%show("isExtant(path)")
38 call disp%show( isExtant(path) )
39 call disp%show("if (isFailedRemove(path)) error stop 'removal failed.'")
40 if (isFailedRemove(path)) error stop 'removal failed.'
41 call disp%show("[isFile(path), isExtant(path)]")
42 call disp%show( [isFile(path), isExtant(path)] )
43 call disp%show(.not."if ( isFailedRemove(path)) error stop 'removal of non existing path fails unless `forced`.'")
44 if (.not. isFailedRemove(path)) error stop 'removal of non existing path fails unless `forced`.'
45 call disp%show("if (isFailedRemove(path, forced = .true._LK)) error stop 'removal of non existing path fails unless `forced`.'")
46 if (isFailedRemove(path, forced = .true._LK)) error stop 'removal of non existing path fails unless `forced`.'
47 call disp%show("isDir(dir)")
48 call disp%show( isDir(dir) )
49 call disp%show("if (isFailedRemove(dir, forced = .true._LK, recursive = .true._LK)) error stop 'removal of directory failed.'")
50 if (isFailedRemove(dir, forced = .true._LK, recursive = .true._LK)) error stop 'removal of directory failed.'
51 call disp%show("isDir(dir)")
52 call disp%show( isDir(dir) )
53 call disp%skip()
54 call disp%skip()
55 end block
56
57 call disp%skip()
58 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
59 call disp%show("! Remove directory or files matching pattern.")
60 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
61 call disp%skip()
62
63 block
64 type(css_type), allocatable :: list(:)
65 character(:, SK), allocatable :: dir, pattern
66 call disp%skip()
67 call disp%show("dir = getPathNew(prefix = SK_'tempdir_') ! define a temp directory name.")
68 dir = getPathNew(prefix = SK_'tempdir_')
69 call disp%show("dir")
70 call disp%show( dir , deliml = SK_"""" )
71 call disp%show("if (isFailedMakeDir(dir)) error stop 'directory creation failed.'")
72 if (isFailedMakeDir(dir)) error stop 'directory creation failed.'
73 call disp%show("call setResized(list, 5_IK)")
74 call setResized(list, 5_IK)
75 call disp%show("do i = 1, 3; list(i)%val = getPathNew(dir, prefix = SK_'tempfile_'); end do ! define temp path names.")
76 do i = 1, 3; list(i)%val = getPathNew(dir, prefix = SK_'tempfile_'); end do
77 call disp%show("do i = 4, size(list); list(i)%val = getPathNew(dir, prefix = SK_'dumpfile_'); end do ! define temp path names.")
78 do i = 4, size(list); list(i)%val = getPathNew(dir, prefix = SK_'dumpfile_'); end do
79 call disp%show("list")
80 call disp%show( list , deliml = SK_"""" )
81 call disp%show("ls(dir)")
82 call disp%show( ls(dir) , deliml = SK_"""" )
83 call disp%show("do i = 1, size(list); open(newunit = unit, file = list(i)%val, status = 'replace'); close(unit); end do ! Create random files with specified patterns.")
84 do i = 1, size(list); open(newunit = unit, file = list(i)%val, status = 'replace'); close(unit); end do ! Create random files with specified patterns.
85 call disp%show("ls(dir)")
86 call disp%show( ls(dir) , deliml = SK_"""" )
87 call disp%show("pattern = dir//getDirSep()//SK_'*ump*'")
88 pattern = dir//getDirSep()//SK_'*ump*'
89 call disp%show("pattern")
90 call disp%show( pattern , deliml = SK_"""" )
91 call disp%show("glob(pattern)")
92 call disp%show( glob(pattern) , deliml = SK_"""" )
93 call disp%show("if (isFailedRemove(pattern, forced = .true._LK, recursive = .true._LK)) error stop 'removal of file(s) matching pattern '''//pattern//''' failed.'")
94 if (isFailedRemove(pattern, forced = .true._LK, recursive = .true._LK)) error stop 'removal of file(s) matching pattern '''//pattern//''' failed.'
95 call disp%show("glob(pattern)")
96 call disp%show( glob(pattern) , deliml = SK_"""" )
97 call disp%skip()
98 call disp%show("pattern = SK_'./*emp*'")
99 pattern = SK_'./*emp*'
100 call disp%show("pattern")
101 call disp%show( pattern , deliml = SK_"""" )
102 call disp%show("glob(pattern)")
103 call disp%show( glob(pattern) , deliml = SK_"""" )
104 call disp%show("if (isFailedRemove(pattern, forced = .true._LK, recursive = .true._LK)) error stop 'removal of directories and files matching pattern '''//pattern//''' failed.'")
105 if (isFailedRemove(pattern, forced = .true._LK, recursive = .true._LK)) error stop 'removal of directories and files matching pattern '''//pattern//''' failed.'
106 call disp%show("glob(pattern)")
107 call disp%show( glob(pattern) , deliml = SK_"""" )
108 call disp%skip()
109 end block
110
111end program example
Allocate or resize (shrink or expand) an input allocatable scalar string or array of rank 1....
This is a generic method of the derived type display_type with pass attribute.
Definition: pm_io.F90:11726
This is a generic method of the derived type display_type with pass attribute.
Definition: pm_io.F90:11508
Generate and return the directory separator symbol based on the runtime system shell.
Generate and return a unique (directory or file) path name in the specified directory or the default ...
Generate and return the result of globing the specified input pattern.
Definition: pm_sysPath.F90:954
Generate and return .true. is the input path is an extant system directory, otherwise return ....
Generate and return .true. if the input path (whether a file or a directory) exists and ....
Generate and return .true. if the attempt to create the requested directory path fails,...
Generate and return .true. is the input path is a file (not a directory), otherwise return ....
Generate and return a list of all paths within the specified input path.
Definition: pm_sysPath.F90:882
This module contains procedures and generic interfaces for resizing allocatable arrays of various typ...
This module contains the derived types for generating allocatable containers of scalar,...
This module contains classes and procedures for input/output (IO) or generic display operations on st...
Definition: pm_io.F90:252
type(display_type) disp
This is a scalar module variable an object of type display_type for general display.
Definition: pm_io.F90:11393
This is the css_type type for generating instances of container of scalar of string objects.
Generate and return an object of type display_type.
Definition: pm_io.F90:10282

Example Unix compile command via Intel ifort compiler
1#!/usr/bin/env sh
2rm main.exe
3ifort -fpp -standard-semantics -O3 -Wl,-rpath,../../../lib -I../../../inc main.F90 ../../../lib/libparamonte* -o main.exe
4./main.exe

Example Windows Batch compile command via Intel ifort compiler
1del main.exe
2set PATH=..\..\..\lib;%PATH%
3ifort /fpp /standard-semantics /O3 /I:..\..\..\include main.F90 ..\..\..\lib\libparamonte*.lib /exe:main.exe
4main.exe

Example Unix / MinGW compile command via GNU gfortran compiler
1#!/usr/bin/env sh
2rm main.exe
3gfortran -cpp -ffree-line-length-none -O3 -Wl,-rpath,../../../lib -I../../../inc main.F90 ../../../lib/libparamonte* -o main.exe
4./main.exe

Example output
1
2!%%%%%%%%%%%%%
3! Remove file.
4!%%%%%%%%%%%%%
5
6
7dir = getPathNew(prefix = SK_'tempdir_') ! define a temp directory name.
8dir
9"tempdir_20241027_224348_402_pid_1"
10if (isFailedMakeDir(dir)) error stop 'directory creation failed.'
11path = getPathNew(dir) ! define a temp path name.
12path
13"tempdir_20241027_224348_402_pid_1/new_20241027_224348_407_pid_1"
14open(newunit = unit, file = path, status = 'replace'); close(unit) ! Create a file in a randomly-named directory.
15isExtant(path)
16T
17if (isFailedRemove(path)) error stop 'removal failed.'
18[isFile(path), isExtant(path)]
19F, F
20if (.not. isFailedRemove(path)) error stop 'removal of non existing path fails unless `forced`.'
21if (isFailedRemove(path, forced = .true._LK)) error stop 'removal of non existing path fails unless `forced`.'
22isDir(dir)
23T
24if (isFailedRemove(dir, forced = .true._LK, recursive = .true._LK)) error stop 'removal of directory failed.'
25isDir(dir)
26F
27
28
29
30!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
31! Remove directory or files matching pattern.
32!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
33
34
35dir = getPathNew(prefix = SK_'tempdir_') ! define a temp directory name.
36dir
37"tempdir_20241027_224348_418_pid_1"
38if (isFailedMakeDir(dir)) error stop 'directory creation failed.'
39call setResized(list, 5_IK)
40do i = 1, 3; list(i)%val = getPathNew(dir, prefix = SK_'tempfile_'); end do ! define temp path names.
41do i = 4, size(list); list(i)%val = getPathNew(dir, prefix = SK_'dumpfile_'); end do ! define temp path names.
42list
43"tempdir_20241027_224348_418_pid_1/tempfile_20241027_224348_422_pid_1", "tempdir_20241027_224348_418_pid_1/tempfile_20241027_224348_423_pid_1", "tempdir_20241027_224348_418_pid_1/tempfile_20241027_224348_425_pid_1", "tempdir_20241027_224348_418_pid_1/dumpfile_20241027_224348_426_pid_1", "tempdir_20241027_224348_418_pid_1/dumpfile_20241027_224348_428_pid_1"
44ls(dir)
45""
46do i = 1, size(list); open(newunit = unit, file = list(i)%val, status = 'replace'); close(unit); end do ! Create random files with specified patterns.
47ls(dir)
48"dumpfile_20241027_224348_426_pid_1", "dumpfile_20241027_224348_428_pid_1", "tempfile_20241027_224348_422_pid_1", "tempfile_20241027_224348_423_pid_1", "tempfile_20241027_224348_425_pid_1"
49pattern = dir//getDirSep()//SK_'*ump*'
50pattern
51"tempdir_20241027_224348_418_pid_1/*ump*"
52glob(pattern)
53"tempdir_20241027_224348_418_pid_1/dumpfile_20241027_224348_426_pid_1", "tempdir_20241027_224348_418_pid_1/dumpfile_20241027_224348_428_pid_1"
54if (isFailedRemove(pattern, forced = .true._LK, recursive = .true._LK)) error stop 'removal of file(s) matching pattern '''//pattern//''' failed.'
55glob(pattern)
56
57
58pattern = SK_'./*emp*'
59pattern
60"./*emp*"
61glob(pattern)
62"./tempdir_20241027_224348_418_pid_1"
63if (isFailedRemove(pattern, forced = .true._LK, recursive = .true._LK)) error stop 'removal of directories and files matching pattern '''//pattern//''' failed.'
64glob(pattern)
65
66
67
Test:
test_pm_sysPath
Todo:
High Priority: The current Fortran standard 202x does not allow passing characters of non-default kind to the intrinsic Fortran statements and procedures.
As such, the implementation of this procedure for non-default character kinds leads to compile-time kind mismatch errors.
This procedure should be converted back to a generic interface in the future when non-default character kinds are also fully supported by the intrinsic functions.


Final Remarks


If you believe this algorithm or its documentation can be improved, we appreciate your contribution and help to edit this page's documentation and source file on GitHub.
For details on the naming abbreviations, see this page.
For details on the naming conventions, see this page.
This software is distributed under the MIT license with additional terms outlined below.

  1. If you use any parts or concepts from this library to any extent, please acknowledge the usage by citing the relevant publications of the ParaMonte library.
  2. If you regenerate any parts/ideas from this library in a programming environment other than those currently supported by this ParaMonte library (i.e., other than C, C++, Fortran, MATLAB, Python, R), please also ask the end users to cite this original ParaMonte library.

This software is available to the public under a highly permissive license.
Help us justify its continued development and maintenance by acknowledging its benefit to society, distributing it, and contributing to it.

Author:
Amir Shahmoradi, Tuesday March 7, 2017, 3:09 AM, Institute for Computational Engineering and Sciences (ICES), The University of Texas Austin

Definition at line 3749 of file pm_sysPath.F90.


The documentation for this interface was generated from the following file: