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

Generate and return .true. if the requested copy action failed, otherwise return .false. indicating success.
More...

Detailed Description

Generate and return .true. if the requested copy action failed, otherwise return .false. indicating success.

The procedures of this generic interface perform the copy action depending on the type of the two input paths:

  1. If from is an existing file, then,
    1. If to is an existing directory, the source file from will be copied (with the same old file base name) into the existing destination folder.
      Example:
      1. from = "./file.txt", to = "./nested/existing/dir/" (Upon return, a new file file.txt will exist in the destination folder to).
    2. If to is an existing file, then the source file will overwrite the existing destination file if and only if the optional argument forced = .true. is specified.
      Example:
      1. from = "./file.txt", to = "./nested/existing/dir/existing.file" (Upon return, existing.file will have been overwritten only if forced = .true.).
    3. If to is a non-existing path, then,
      1. if to ends with any of the directory separators recognized by the runtime shell, then to is interpreted as a non-existing destination folder. If so,
        1. Firstly, the (potentially nested non-existing) destination directory to will be created.
        2. Secondly, the source file from will be copied into the newly created destination folder to.
          Example:
          1. (Unix/Windows) from = "./file.txt", to = "./nested/non-existing/path/" (Upon return, a new file file.txt will exist in the destination folder to).
          2. (Windows) from = ".\file.txt", to = ".\nested\non-existing\path\" (Upon return, a new file file.txt will exist in the destination folder to).
      2. if to does not end with any of the directory separators recognized by the runtime shell, then to is interpreted as a non-existing destination file. If so,
        1. Firstly, the (potentially nested non-existing) dirname of the destination file will be created.
        2. Secondly, the source file from will be copied to the destination folder with a new name that is the base name of to.
          Example:
          1. from = "./file.txt", to = "./nested/non-existing/path" (A new file named path will be created containing the contents of file.txt).
  2. If from is an existing directory, then,
    1. If to is an existing directory, the entire source directory from (along with its basename) will be copied into the existing destination folder.
      1. Existing files with the same name as in the source folder from will be overwritten only if the optional argument forced = .true. is specified.
        Example:
        1. from = "./paramonte/examples/", to = "./newdir/" (A new directory "./newdir/examples/" will be created containing the contents of from).
    2. If to is an existing file, the program will not perform the requested copy action and will return failed = .true..
    3. If to is a non-existing path, then to is interpreted as a non-existing destination directory.
      1. Firstly, the (potentially nested non-existing) destination directory to will be created.
      2. Secondly, the contents of the source directory from will be copied into the newly-created destination directory to.
      3. This behavior is similar to the behavior of cp linux command, except that this procedure automatically creates the dirname of to if it does not exist.
        Example:
        1. from = "./paramonte/examples/", to = "./new/nested/path" (non-existing). Result: The newly-created directory to = "./new/nested/path" will contain the contents of from.
  3. If from is neither an existing file nor an existing folder, the program will not perform the requested copy action and will return failed = .true..

This procedure is capable of copying a file or a directory to nonexistent (potentially nested) directories.

The copy request is done by first identifying the processor shell type (CMD, PowerShell, Bash, csh, zsh, ...) and then calling the runtime system shell via the Fortran intrinsic subroutine execute_command_line() to invoke mkdir shell command with appropriate flags.
Consequently, this procedure should function as expected in Windows CMD, PowerShell, and POSIX-compatible (Unix-like) shells.

  1. On POSIX-compliant shells, the commands cp -arn and cp -arf are used to perform the copy regularly or forcefully (with overwriting), respectively.
  2. On Windows-based terminals, the commands xcopy /e /q /s and xcopy /e /q /s /Y are used to perform the copy regularly or forcefully (with overwriting), respectively.
Parameters
[in]from: The input scalar character of default kind SK containing a POSIX-style or Windows-style path to a file or directory.
[in]to: The input scalar character of default kind SK containing a POSIX-style or Windows-style path to which the path from should be copied.
When the input argument from points to a single file, there is ambiguity about how the destination path should be interpreted.
As such, if to points to a (potentially non-existing) folder, make sure to end it with a suitable directory separator getDirSep before passing it to the procedures under this generic integer.
Otherwise, to is assumed to point to a file name.
[in]recursive: The input scalar logical of default kind LK. If .true. the copy action will be performed recursively (including all subdirectories).
This input argument is relevant only if the input source path from is a directory with subdirectories.
(optional, default = .false.. See the warning below regarding the implications of setting recursive = .true..)
[in]forced: The input scalar logical of default kind LK. If .true. the copy action will overwrite the destination to if it already exists.
This input argument is relevant only if the input destination path partially or entirely contains the same files in from.
(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 2047 characters for errmsg is likely enough 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 from not existing before the copy action or the destination path to not existing after the copy 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 = isFailedCopy(from, to, recursive = recursive, forced = forced, wait = wait, ntry = ntry, errmsg = errmsg)
Generate and return .true. if the requested copy action failed, otherwise return ....
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
When does the option recursive = .true. lead to infinite copy loop?
When to is a subdirectory of from.
Be warned that setting the input argument recursive = .true. when to is a subdirectory of from can lead to a never ending copy cycle which will likely eventually cause the process to crash.
Use recursive = .true. only if to is guaranteed to not be a subdirectory of from.
This procedure currently does not copy hidden files consciously. The result is currently platform and usage dependent.
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 copy 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.
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
6
7 implicit none
8
9 character(:, SK), allocatable :: cpath, dir, subdir, destination
10
11 type(display_type) :: disp
12 disp = display_type(file = "main.out.F90")
13
14 call disp%skip()
15 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
16 call disp%show("! Copy file to a new non-existing destination file.")
17 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
18 call disp%skip()
19
20 call disp%skip()
21 call disp%show("cpath = getPathNew(ext = SK_'.main.out.F90') ! define a random unique path name.")
22 cpath = getPathNew(ext = SK_'.main.out.F90')
23 call disp%show("cpath")
24 call disp%show( cpath , deliml = SK_"""" )
25 call disp%show("isFile(cpath)")
26 call disp%show( isFile(cpath) )
27 call disp%show("isFailedCopy(from = SK_'main.out.F90', to = cpath)")
28 call disp%show( isFailedCopy(from = SK_'main.out.F90', to = cpath) )
29 call disp%show("isFile(cpath)")
30 call disp%show( isFile(cpath) )
31 call disp%skip()
32
33 call disp%skip()
34 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
35 call disp%show("! Copy file to a new non-existing doubly-nested destination folder.")
36 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
37 call disp%skip()
38
39 call disp%skip()
40 call disp%show("dir = getPathNew(dir = getPathNew(), prefix = SK_'sub')//getDirSep() ! Ending the directory name with directory separator is crucial if it does not exit already, otherwise it is interpreted as file name.")
41 dir = getPathNew(dir = getPathNew(), prefix = SK_'sub')//getDirSep()
42 call disp%show("dir")
43 call disp%show( dir , deliml = SK_"""" )
44 call disp%show("isDir(dir)")
45 call disp%show( isDir(dir) )
46 call disp%show("isFile(dir)")
47 call disp%show( isFile(dir) )
48 call disp%show("isFailedCopy(from = SK_'main.out.F90', to = dir)")
49 call disp%show( isFailedCopy(from = SK_'main.out.F90', to = dir) )
50 call disp%show("isDir(dir)")
51 call disp%show( isDir(dir) )
52 call disp%show("isFile(dir//SK_'main.out.F90')")
53 call disp%show( isFile(dir//SK_'main.out.F90') )
54 call disp%skip()
55
56 call disp%skip()
57 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
58 call disp%show("! Copy file to a new location and overwrite the existing file.")
59 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
60 call disp%skip()
61
62 call disp%skip()
63 call disp%show("destination = dir//SK_'main.out.F90'")
64 destination = dir//SK_'main.out.F90'
65 call disp%show("destination")
66 call disp%show( destination , deliml = SK_"""" )
67 call disp%show("isFile(destination)")
68 call disp%show( isFile(destination) )
69 call disp%show("isFailedCopy(from = SK_'main.out.F90', to = destination, forced = .true.)")
70 call disp%show( isFailedCopy(from = SK_'main.out.F90', to = destination, forced = .true.) )
71 call disp%show("isDir(dir)")
72 call disp%show( isDir(dir) )
73 call disp%skip()
74
75 call disp%skip()
76 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
77 call disp%show("! Copy file to a new non-existing destination file name in a non-existing subfolder.")
78 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
79 call disp%skip()
80
81 call disp%skip()
82 call disp%show("subdir = getPathNew(dir = dir, prefix = SK_'subsub')")
83 subdir = getPathNew(dir = dir, prefix = SK_'subsub')
84 call disp%show("subdir")
85 call disp%show( subdir , deliml = SK_"""" )
86 call disp%show("isDir(subdir)")
87 call disp%show( isDir(subdir) )
88 call disp%show("cpath = subdir//getDirSep()//SK_'main.out.F90'")
89 cpath = subdir//getDirSep()//SK_'main.out.F90'
90 call disp%show("cpath")
91 call disp%show( cpath , deliml = SK_"""" )
92 call disp%show("isFile(cpath)")
93 call disp%show( isFile(cpath) )
94 call disp%show("isFailedCopy(from = SK_'main.out.F90', to = cpath)")
95 call disp%show( isFailedCopy(from = SK_'main.out.F90', to = cpath) )
96 call disp%show("isFile(cpath)")
97 call disp%show( isFile(cpath) )
98 call disp%skip()
99
100 call disp%skip()
101 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
102 call disp%show("! Copy 'the contents of' a folder recursively into another (potentially non-existing) destination folder.")
103 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
104 call disp%skip()
105
106 call disp%skip()
107 call disp%show("dir ! Note the ending directory separator which is essential for copying the contents of the folder and not the folder itself.")
108 call disp%show( dir , deliml = SK_"""" )
109 call disp%show("isDir(dir)")
110 call disp%show( isDir(dir) )
111 call disp%show("cpath = getPathNew(prefix = SK_'recursive_copy')")
112 cpath = getPathNew(prefix = SK_'recursive_copy')
113 call disp%show("isDir(cpath)")
114 call disp%show( isDir(cpath) )
115 call disp%show("isFailedCopy(from = dir, to = cpath, recursive = .true._LK)")
116 call disp%show( isFailedCopy(from = dir, to = cpath, recursive = .true._LK) )
117 call disp%show("isDir(cpath//getDirSep()//dir)")
118 call disp%show( isDir(cpath//getDirSep()//dir) )
119 call disp%skip()
120
121end program example
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 .true. is the input path is an extant system directory, otherwise return ....
Generate and return .true. is the input path is a file (not a directory), otherwise return ....
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
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! Copy file to a new non-existing destination file.
4!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5
6
7cpath = getPathNew(ext = SK_'.main.out.F90') ! define a random unique path name.
8cpath
9"new_20240822_211525_321_pid_1.main.out.F90"
10isFile(cpath)
11F
12isFailedCopy(from = SK_'main.out.F90', to = cpath)
13F
14isFile(cpath)
15T
16
17
18!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
19! Copy file to a new non-existing doubly-nested destination folder.
20!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
21
22
23dir = getPathNew(dir = getPathNew(), prefix = SK_'sub')//getDirSep() ! Ending the directory name with directory separator is crucial if it does not exit already, otherwise it is interpreted as file name.
24dir
25"new_20240822_211525_327_pid_1/sub20240822_211525_329_pid_1/"
26isDir(dir)
27F
28isFile(dir)
29F
30isFailedCopy(from = SK_'main.out.F90', to = dir)
31F
32isDir(dir)
33T
34isFile(dir//SK_'main.out.F90')
35T
36
37
38!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
39! Copy file to a new location and overwrite the existing file.
40!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
41
42
43destination = dir//SK_'main.out.F90'
44destination
45"new_20240822_211525_327_pid_1/sub20240822_211525_329_pid_1/main.out.F90"
46isFile(destination)
47T
48isFailedCopy(from = SK_'main.out.F90', to = destination, forced = .true.)
49F
50isDir(dir)
51T
52
53
54!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
55! Copy file to a new non-existing destination file name in a non-existing subfolder.
56!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
57
58
59subdir = getPathNew(dir = dir, prefix = SK_'subsub')
60subdir
61"new_20240822_211525_327_pid_1/sub20240822_211525_329_pid_1/subsub20240822_211525_341_pid_1"
62isDir(subdir)
63F
64cpath = subdir//getDirSep()//SK_'main.out.F90'
65cpath
66"new_20240822_211525_327_pid_1/sub20240822_211525_329_pid_1/subsub20240822_211525_341_pid_1/main.out.F90"
67isFile(cpath)
68F
69isFailedCopy(from = SK_'main.out.F90', to = cpath)
70F
71isFile(cpath)
72T
73
74
75!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
76! Copy 'the contents of' a folder recursively into another (potentially non-existing) destination folder.
77!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
78
79
80dir ! Note the ending directory separator which is essential for copying the contents of the folder and not the folder itself.
81"new_20240822_211525_327_pid_1/sub20240822_211525_329_pid_1/"
82isDir(dir)
83T
84cpath = getPathNew(prefix = SK_'recursive_copy')
85isDir(cpath)
86F
87isFailedCopy(from = dir, to = cpath, recursive = .true._LK)
88F
89isDir(cpath//getDirSep()//dir)
90F
91
92
Test:
test_pm_sysPath
Todo:
Normal Priority: The current Fortran standard 202x does not allow passing characters of non-default kind to the intrinsic Fortran statements and procedures.
This generic interface should be extended to in the future to support non-default character kinds when they fully supported by the Fortran intrinsic procedures.


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 at Austin

Definition at line 3484 of file pm_sysPath.F90.


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