|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
use General::FileSystem "-die", "-debug"; # die on errors
eval {
makePath("/tmp/my-own/dir");
my $fileContentRef = loadFile("/etc/issue");
saveFile("/tmp/my-own/dir/issue", $fileContentRef);
# This is equivalent to the previous two lines, but optimized
copyFile("/etc/issue", "/tmp/my-own/dir/issue");
makeDir("/tmp/my-own/dir2", 0711);
copyFile("/etc/issue", "/tmp/my-own/dir2/issue");
moveFile("/tmp/my-own/dir2/issue", "/tmp/my-own/dir2/issue2");
removeFile("/tmp/my-own/dir2/issue2");
cleanDir("/tmp/my-own/dir2"); # no effect, it's empty already
removeDir("/tmp/my-own");
};
if ($@) {
print "File System Error: $@";
};
or just:
use General::FileSystem;
copyFile("origin.txt", "backup.txt");
loadFile, saveFile, appendFile, removeFile, copyFile, moveFile, makeDir, makePath, cleanDir, removeDir, copyDir, moveDir, listFileNames, findFile, findExecutable, defaultDirPerm, preserveStat, parsePath, getCwd.
On fatal file system errors all functions call the error handler, that may throw exception (die), issue a warning or quietly return undef. You may control this by passing one of the arguments -die, -warn or -quiet in use or by setting $ERROR_HANDLER to one of these values (don't specify a dash in this case).
$contentRef = loadFile($fileName)
* fileName - name of the file to be loaded.
saveFile($fileName, \$fileContent);
* fileName - name of the file to be saved into * fileContentRef - reference to file-content string * createSubdirs - optional flag (default is 0 - don't create subdirs)
appendFile($fileName, \$fileAppendContent);
* fileName - name of the file to be saved into * fileAppendContentRef - reference to file-append-content string
removeFile($fileName);
* fileName - name of the file to be deleted
makeDir($PREVIEW_DIR);
* directory to make * optional creating dir permissions (default is $DEFAULT_DIR_PERM)
makePath($PUBLISH_DIR);
* path to make * optional creating dir permissions (default is $DEFAULT_DIR_PERM)
copyFile($from, $to);
* file name to copy from * file name to copy to
moveFile($from, $to);
* file name to move from * file name to move to
cleanDir($PREVIEW_DIR);
* directory to clean
* optional flag:
0 - don't go recursively, unlink files in first level only
1 - recursively clean subdirs (default)
2 - unlink subdirs
3 - unlink given directory
removeDir($TMP_DIR);
* directory to clean
Destination directory must not exist. Use: "trap { removeDir($dest); };" to remove it before copying.
copyDir($dirFrom, $dirTo);
* source directory to copy * destination directory to copy to (may not exist) * optional creating dir permissions (default is $DEFAULT_DIR_PERM)
Destination directory must not exist. Use: "trap { removeDir($dest); };" to remove it before copying.
moveDir($dirFrom, $dirTo);
* source directory to move from * destination directory to move to (must not exist)
# mini file lister
$dir = '/home/ftp';
foreach my $file (@{listFileNames($dir)}) {
print "File $file\n" if -f "$dir/$file";
print "Dir $file\n" if -d "$dir/$file";
}
* directory to list (or array ref of directories) * optional flag, 1 means work recursively, the default is 0
Returns the fully qualified file name.
my $gtkrc = findFile(".gtkrc", [$home, "$home/.gnome"]);
* file name to search for * array ref of directories to search in
Returns the fully qualified file name.
my $gzipExe = findExecutable("gzip", ["/usr/gnu/bin", "/gnu/bin"]);
* file name to search for (only executables are tested) * optional array ref of directories to search in
The default of this package is 0775.
If no parameters specified, the current value is returned.
defaultDirPerm(0700);
* optional default directory permission (integer)
TODO: specify values for diferent preserves:
0 nothing 1 mode file mode (type and permissions) 2 uid numeric user ID of file's owner 4 gid numeric group ID of file's owner 8 atime last access time since the epoch 16 mtime last modify time since the epoch 32 ctime inode change time (NOT creation time!) since the epo
The default of this package is 0.
If no parameters specified, nothing is set (only current value is returned).
preserveStat(1);
* optional flag (currently 0 or 1)
my ($dirName, $baseName) = parsePath($fileName);
# in: "/data/projects/magazine" out: ("/data/projects", "magazine")
# in: "/magazine" out: ("", "magazine")
# in: "dir/" out: (dir", "")
# in: "magazine" out: (".", "magazine")
# in: "c:\projects\magazine" out: ("c:\projects", "magazine")
# in: "c:\magazine" out: ("c:", "magazine")
# in: "c:magazine" out: ("c:.", "magazine")
Note, the rule is this: you can join both scalars using a directory delimiter (slash or backslash) and you will always get the the original (logical) file name.
my $cwd = getCwd();
Last modified on April 26, 2010