A
A
Alexey2015-08-05 10:11:11
Python
Alexey, 2015-08-05 10:11:11

How to transfer an arbitrary number of arbitrary files to an arbitrary folder?

The task is this:
There is a folder with about 1 million files in it. Ideally, you need to create a batch file that you can access with two parameters:
1. The name of the folder to be created
2. The number of files
How the script should work:
When accessing it, an arbitrary number of files is taken from the folder and moved to the newly created folder.
Ideally, all this can be implemented using .bat (you can of course use python or c#)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vitaly Pukhov, 2015-08-05
@Neuroware

It comes easy:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication9
{
    class Program
    {
        static void Main(string[] args)
        {
            string sourcefolder = @"D:\Image2"; //папка в которой млн файлов
            int filescount = 100; //количество файлов в 1 папке
            int n = 0;
            string targetfolder = @"D:\Новая папка (8)"; //папка в которой будут лежать другие папки
            var files = Directory.GetFiles(sourcefolder, "*.jpg", SearchOption.AllDirectories);
            for (int i = 0; i < files.Length ; i++)
            {
                if (i%filescount ==0)
                {
                    n++;
                    string newfolder = targetfolder + string.Format("\\{0:00000000}", n);
                    Directory.CreateDirectory(newfolder);
                }
                string newfilename = targetfolder + string.Format("\\{0:00000000}\\{1}", n, Path.GetFileName(files[i]));
                File.Copy(files[i], newfilename);
                Console.WriteLine(newfilename);
            }
        }
    }
}

After launching, it will "break" the source folder into a bunch of smaller ones, each with as many files as you specify. Checked the code. 100% working.

A
Alexey Boldyrev, 2015-08-05
@allexb

use
add counter for required number of files

Y
YaroslavS, 2015-08-05
@YaroslavS

in python something like this test before use

import shutil
import os
import sys
#читаеш аргументы командной строки
dest_folder = sys.argv[1]
count = int(sys.argv[2])
source_folder = 'you source folder'
#проверка есть ли папка
if not os.path.exists(dest_folder):
    os.mkdir(dest_folder)
# listdir получить список файлов в директории
#shutil.move перенос
for name in os.listdir(source_folder)[:count]:
    shutil.move(os.path.join(source_folder, name), os.path.join(dest_folder, ''))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question