Search…
Managing Files And Directories

Manage files and directories

For complete API documentation refer to: pub.dev
DCli provides a complete set of tools for manipulating files and directories.
DCli also includes the paths package that provides tools for manipulating file paths.
A fundamental task of most CLI applications is the management of files and directories. DCli provides a large set of tools for file management.

pwd

The getter 'pwd' returns the present working directory.
1
print(pwd);
2
> /home/me
Copied!
Whilst you can change your working directory we don't recommend it. Read the section on the evils of cd.
If you think you need to change your working directory check to see if the DCli function takes a 'workingDirectory' argument.
If you need to spawn another CLI application that needs to run in a specific directory use the 'start' function.
1
'ls'.start(workingDirectory: HOME);
Copied!
If you really think you have no alternative (you are probably wrong) the you can use the Dart method Directory.current.

Find

The find command lets you explore your file system by searching for files that match a glob (wildcard).
1
List<String> results = find('[a-z]*.jpg').toList();
Copied!
The find command starts from the present working directory (pwd) searching for any files that start with the lowercase letters a-z and ending with the extension '.jpg'.
The default action of find is to do a recursive search.
1
List<String> results = find('[a-z]*.jpg', workingDirectory: '\' ).toList();
Copied!
If you need to do a search starting from a location other than you current directory you can use the 'workingDirectory' argument which controls where the find starts searching from.
1
List<String> results = find('[a-z]*.jpg', workingDirectory: '\', hidden: true ).toList();
Copied!
The find command will ignore hidden files (those starting with a '.') and directories. If you need to scan hidden files then pass 'hidden: true'. If you need to return directories as well as files then use the 'types' argument.
1
var progress = Progress((file) => print(file));
2
find('*.jpg', root: '\'
3
, types:[Find.directory, Find.file]
4
, progress: progress);
Copied!
If you are process a large amount of results you may want to process them as you go rather than waiting for the full result list to be available.
By passing a 'Progress' into 'find' your progress will be called each time a matching file is found allowing to display the progressive results to the user.

fileList

Returns the list of files and directories in the current working directory. Use the 'find' function to get a list of any other directory.
1
List<String> entities = fileList;
Copied!

copy

The copy function copies a single file to a to a directory or a new file.
1
copy("/tmp/fred.text", "/tmp", overwrite=true);
2
copy("/tmp/fred.text", "/tmp/fred2.text", overwrite=true);
Copied!
The first example will copy the file 'fred.text' to the '/tmp' directory, the second file also copies the file to the '/tmp' directory but renames the file as it goes.
If the 'overwrite' is not passed or is set to false (the default) an attempt to copy over an existing file will cause a 'CopyException' to be thrown.

copyTree

The copyTree function allows you to copy an entire tree or selected files from the tree to another location.
The copyTree function takes an optional 'filter' argument which allows you to selectively copy files. Only those files that match the filter are copied.
1
copyTree("/tmp", "/tmp/new_dir", overwrite:true, includeHidden:true
2
, filter: (file) => extension(file) == 'dart');
Copied!
The above copyTree only copies files from '/tmp' that have an '.dart' extension.

move

The move function copies a single file to a directory or a file. If the 'to' argument is a file then the file is renamed.
The move function tries to use the native OS 'rename' function however if the destination is on a different device the rename will fail. In this case the move function performs a copy then delete.
1
move('/tmp/fred.txt', '/tmp/folder/tom.txt');
Copied!

moveTree

The moveTree function allows you to move an entire tree or selected files from the tree to another location.
The moveTree function takes an optional 'filter' argument which allows you to selectively move files. Only those files that match the filter are moved.
1
moveTree("/tmp/", "/tmp/new_dir", overwrite: true
2
, filter: (entity) {
3
var include = extension(entity) == 'dart';
4
if (include) {
5
print('moving: $file');
6
}
7
return include;
8
);
Copied!
Like the move function the moveTree attempts an OS level rename but if that fails it resorts to performing a copy followed by a delete.

delete

The delete function deletes a file.
1
delete("/tmp/test.fred", ask: true);
Copied!
If you pass the 'ask' argument to the delete function then the user will be prompted to confirm the delete action.

deleteDir

The deleteDir function deletes a directory.
1
deleteDir("/tmp/testing";
Copied!
If the directory isn't empty then a DeleteDirException will be thrown.
You can delete an entire directory tree using the recursive option:
1
deleteDir("/tmp/testing", recursive=true);
Copied!

createDir

The createDir function creates a directory. If the directory already exists then a CreateDirException will be thrown.
1
if (!exists('/tmp/fred/tools')) {
2
createDir("/tmp/fred/tools");
3
}
Copied!
If the parent path doesn't exists then a CreateDirException will be thrown, to avoid this pass the recursive argument
1
createDir("/tmp/fred/tools", recursive: true);
Copied!

touch

The touch function updates the last modified date/time stamp of the passed file. If the 'create' argument is passed and the file doesn't exists then the file will be created. If the file doesn't exists and 'create: true' isn't passed then a 'TouchException' will be thrown.
1
touch('fred.txt, create: true');
Copied!

exists

The 'exists' function checks if a file, directory or symlink exists.
1
if (exists("/fred.txt"))
Copied!

isWritable

Test if a file or directory is writable.
1
if (isWritable('/fred.txt'))
Copied!

isReadable

Test if a file or directory is readable.
1
if (isReadable('/fred.txt'))
Copied!

isExecutable

Test if a file or directory is executable.
1
if (isExecutable('/fred.txt'))
Copied!

isFile

Test if the given path is a file.
1
if (isFile('/fred.txt'))
Copied!
Test if the given path is a symbolic link.
1
if (isLink('/fred.txt'))
Copied!

isDirectory

Test if the given path is a directory.
1
if (isDirectory('/fred.txt'))
Copied!

setModified

Sets the last modified date/time stamp on give path.. This is similar to touch exception that you can choose the date/time.
1
setLastModifed('/fred.txt', DateTime.now());
Copied!

lastModified

Returns a DateTime reflecting the last modified date/time stamp of the given path.
1
DateTime modified = lastModifed('/fred.txt');
Copied!

calculateHash

Calculates the sha256 hash of a file's content generating essentially a unique signature or checksum for the file.
This is likely to be an expensive operation if the file is large. You can use this method to check if a file has changes since the last time you took the file's hash.
1
var digest = calculateHash('/fred.txt');
Copied!
Last modified 1mo ago