开发者

Batch to copy files to another folder with breaks

开发者 https://www.devze.com 2023-03-12 14:40 出处:网络
I need开发者_如何转开发 a script, prefferably a windows batch or C# to do as following: Show a prompt that first ask for the source folder,

I need开发者_如何转开发 a script, prefferably a windows batch or C# to do as following:

Show a prompt that first ask for the source folder, then it should ask for the destination folder. At last, it shall ask how many files it should copy to the destination, from the source.

// We talk about aprox 100.000 files and they can be moved in random order.

After the process has been run, the program shall make a break of 10 minutes, then loop the process it was told to earlier, by previous answers to the prompt.


I've tried a little, but haven't found a solution. As far as i can see, XCOPY is unable to work around all these criterias.

Thanks in advance, Mark


  1. RoboCopy (the See also section might interest you as well) or
  2. (more recent:) RichCopy (download)


You can use something like this:

string source  = Console.ReadLine();
string destination = Console.ReadLine();
int numberOfFilesToCopy = int.Parse(Console.ReadLine());

DirectoryInfo di = new DirectoryInfo(source);
var files = di.GetFiles();
for(i=0;i < math.Max(files.Length, numberOfFilesToCopy);i++)
{
    files[i].CopyTo(destination);
}


In C# using System.IO.File.Copy(sourceFileName,destFileName) followed by a System.IO.File.Delete(path) will do the "move" for you. You can create a simple console app that takes in the information you need and then does the work.

Have a look at the docs for System.IO.File for more info on File operations.


I'm not sure this fulfills all your requirements, but it may be useful to have a look at robocopy (robocopy /? in the command line).


Don't think I missed anything =D

@ECHO OFF
::User Prompts
SET /p source=Source Folder? Use format DRIVE:\PATH\    :
SET /p destination=Destination Folder?  Use format DRIVE:\PATH\    :
SET /p count=How many files to copy?    :

::Setup the Batch file to schedule
DIR /B "%source%">>"%userprofile%\batchtemp\source.BAT"
SET batchfile=%userprofile%\batchtemp\source.BAT
ECHO SETLOCAL ENABLEDELAYEDEXPANSION>>"%batchfile%"
ECHO FOR /F "USEBACKQ tokens=*" %%A IN ("%batchfile%") DO ( >>"%batchfile%"
ECHO  COPY /Y "%%~fA" "%destination%\%%~nxA">>"%batchfile%"
ECHO  SET /a count=!count!-1>>"%batchfile%"
ECHO  IF %count% EQU 0 GOTO CLEANUP>>"%batchfile%"
ECHO )>>"%batchfile%"
ECHO :CLEANUP>>"%batchfile%"
ECHO ENDLOCAL>>"%batchfile%"

::Setup the scheduled task based on a future time in minutes.
REM Given that the job will run on the same day not overlapping a 24 hour day

FOR /F "tokens=1-3 delims=: " %%F IN ('time /t') DO (
 SET hours=%%F
 SET minutes=%%G
)
FOR /F "tokens=1-4 delims=/ " %%F IN ('date /t') DO (
 SET day=%%F 
 SET thedate=%%G/%%H/%%I
)

SET /a minutes=%minutes%+10
IF %minutes% GRT 60 SET /a minutes=%minutes%-60 & SET /a hours=%hours%+1
SCHTASKS /Create /TR "%batchfile%" /ST %hours%:%minutes%:00 /MO ONCE /D %day% /SD "%thedate%" /ED "%thedate%" /TN "Copy Files"
0

精彩评论

暂无评论...
验证码 换一张
取 消