system.io.file - How to move files from one path to another in c# -


in application have 3 paths

  1. \shared\1.txt
  2. \shared\
  3. \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

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -