Last active
December 22, 2015 19:20
-
-
Save allanx2000/aefd4f58cebb9b6d4227 to your computer and use it in GitHub Desktop.
"Script" to move files from subfolders to an out folder.... Windows 10 sucks and is full of bugs.... Win 8 wouldn't need.....
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace BatchMove | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
string rootFolder = @"C:\Users\Allan\Desktop\New folder"; | |
string outFolder = @"C:\Users\Allan\Desktop\New folder (2)"; | |
MoveAll(rootFolder, outFolder, true); | |
} | |
private static void MoveAll(string inFolder, string outFolder, bool overwriteSameName, bool getSubDir = true, string pattern = "*.*") | |
{ | |
var files = Directory.GetFiles(inFolder, "*.*"); | |
foreach (string f in files) | |
{ | |
FileInfo fi = new FileInfo(f); | |
string newPath = Path.Combine(outFolder, fi.Name); | |
if (File.Exists(newPath)) | |
{ | |
if (overwriteSameName) | |
{ | |
File.Delete(newPath); | |
} | |
else | |
{ | |
int i = 0; | |
do | |
{ | |
newPath = Path.Combine(outFolder, fi.FullName.Replace(fi.Extension, i + "." + fi.Extension)); | |
i++; | |
} | |
while (File.Exists(newPath)); | |
} | |
} | |
fi.MoveTo(newPath); | |
} | |
if (getSubDir) | |
{ | |
var dirs = Directory.GetDirectories(inFolder); | |
foreach (var d in dirs) | |
{ | |
MoveAll(d, outFolder, overwriteSameName, true, pattern); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment