system.io.file - How to move files from one path to another in c# -
in application have 3 paths
- \shared\1.txt
- \shared\
- \shared\*.txt
this file paths come in variable.
now how can check if path has single file or multiple or has wildcard ? & move them path.
well, since neither *
nor ?
can in path: in
char[] forbidden = path.getinvalidpathchars();
so can them
string path = @"c:\mydata\shared\*.txt"; ... boolean iswildcard = path.containsany('?', '*');
as file/directory
boolean isfile; if (file.exists(path)) isfile = true; // file exists else if (directory.exists(path)) isfile = false; // directory exists else if (string.equals(path.getextension(path), ".txt", stringcomparison.invariantcultureignorecase)) isfile = true; // has txt extension, let file else isfile = false;
however seems have no need have branches (iswildcard, isfile) , move files:
string path = @"c:\mydata\shared\*.txt"; ... string sourcedirectory = path.getdirectoryname(path); string destination = @"c:\destination"; directory.getfiles(sourcedirectory, "*.txt") .foreach(file => file.move(file, path.combine(destination, path.getfilename(file))));
Comments
Post a Comment