简体   繁体   中英

Moving all unknown and future folders to specific folder

I am trying to create script for my terminal server, which will move all folders with their contents, created in the wrong place. I don't know which names these folders will have. As I noticed, moving folders in C# is a problem for some reason. Can someone help me with my code? It just deleting my test folders and nothing moves.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Microsoft.VisualBasic.FileIO;

namespace ConsoleApp1
{
    public class Programm
    {
        public static void Main()
        {
            string root = @"C:\Users\user1\Desktop";
            string[] subdirectoryEntries = Directory.GetDirectories(root);
            string destDirname = @"D:\confiscated";
            foreach (string path in subdirectoryEntries)
            {
                FileSystem.MoveDirectory(path, destDirname, true);
            }
        }
    }
}

This is how to do it:

string startPath = @"YOURSTARTPATH";
string endPath = @"YOURENDPATH";

foreach (string directory in Directory.GetDirectories(startPath))
{
    Directory.Move(directory, Path.Combine(endPath, Path.GetFileName(directory)));
}

This way, you will use the already provided by the framework classes to work with directories.

No need to include Microsoft.VisualBasic.FileIO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM