Skip to content

Instantly share code, notes, and snippets.

@allanx2000
Last active December 22, 2015 19:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save allanx2000/aefd4f58cebb9b6d4227 to your computer and use it in GitHub Desktop.
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.....
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