简体   繁体   English

Windows脚本可将X个文件从一个文件夹复制到另一个文件夹

[英]Windows script to Copy X number of files from one folder to another

I am new to scripting world so might be asking some novice question but I did not find the exact code after so much googling. 我是脚本世界的新手,所以可能会问一些新手问题,但是经过大量的谷歌搜索之后,我没有找到确切的代码。

My requirement is to generate a script to copy X number of files from one folder to another. 我的要求是生成一个脚本,以将X个文件从一个文件夹复制到另一个文件夹。 Here number of files to be copied, source and target folder should be configurable in the script. 此处,应在脚本中配置要复制的文件数,源文件夹和目标文件夹。

I tried Xcopy, Robocopy but in none I found any parameter to restrict the number of files to be copied. 我尝试了Xcopy,Robocopy,但没有找到任何参数来限制要复制的文件数。 The program will run on Win7 or Win2008 server. 该程序将在Win7或Win2008服务器上运行。

Please help. 请帮忙。

For batch-file tag: 对于批处理文件标签:

@echo off

    setlocal enableextensions enabledelayedexpansion

    set "source=d:\temp\input"
    set "target=d:\temp\output"
    set num=10

    for /F "tokens=1,2 delims=:" %%f in ('dir /b /a-d "%source%\*"  ^| findstr /n "^" ') do (
        if %%f leq %num% (
            copy "%source%\%%g" "%target%" /y > nul 
        ) else goto endCopy
    )

:endCopy
    endlocal

I must say that I agree with vonPryz here. 我必须在这里说我同意vonPryz。 It seems odd. 好像很奇怪 However it's very easy to do what you need in PowerShell. 但是,在PowerShell中执行所需的操作非常容易。 Just grab the number of items you want using: 只需获取要使用的项目数即可:

$sourceFolder = "D:\source"
$destinationFolder = "D:\copiedfiles"
$maxItems = 200
Get-Childitem $sourceFolder | Select-Object -First $maxItems | Copy-Item $destinationFolder

This will even create the destination folder if it doesn't exist. 如果它不存在,甚至会创建目标文件夹。

Discuss with whoever gave the requirement and find out the exact filtering needs. 与提出要求的人讨论,并找出确切的过滤需求。

It makes little if any sense to copy N files from location X to location Y . copy N files from location X to location Y几乎没有意义。 Usually there is more sensible a criteria, like copy all files that are changed after DATETIME from location X to location Y. If there are more than N files, pick only N oldest/newest. 通常,会有更明智的条件,例如copy all files that are changed after DATETIME from location X to location Y. If there are more than N files, pick only N oldest/newest. Only the stakeholder can tell you what kind of behaviour is expected. 只有利益相关者可以告诉您预期的行为。 Without more information, the script is very much unlikely to provide the expected functionality. 没有更多信息,该脚本极不可能提供预期的功能。

After you know the exact requirement, do some more Googling for solution. 在知道确切的要求之后,请执行更多Google搜索以寻求解决方案。 It shouldn't take much effort to find out how to achieve the task in Powershell. 无需花费太多精力就可以找到如何在Powershell中完成任务。 Hint: use gci with appropriate parameters/filters and store its results in an array. 提示:将gci与适当的参数/过滤器一起使用,并将其结果存储在数组中。

Btw... the above PowerShell will throw exception about command not taking pipeline input. 顺便说一句...上面的PowerShell将引发有关不接受管道输入的命令的异常。 You need to copy each object, so the right command is: Get-Childitem $sourceFolder | ForEach-Object {Select-Object -First $maxItems | Copy-Item $destinationFolder} 您需要复制每个对象,因此正确的命令是: Get-Childitem $sourceFolder | ForEach-Object {Select-Object -First $maxItems | Copy-Item $destinationFolder} Get-Childitem $sourceFolder | ForEach-Object {Select-Object -First $maxItems | Copy-Item $destinationFolder}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将文件从 Windows 文件夹树复制到另一个文件夹 - Copy files from Windows folder tree to another folder Windows批处理脚本,用于将所有文件从Windows中的一个目录移动到另一个目录,并且文件夹中包含空格 - Windows Batch Script to move all files from one directory to another in windows, and folder contains spaces 如何使用批处理脚本将前15个txt文件从c驱动器中的一个文件夹复制到c驱动器中的另一个文件夹? - How to copy top 15 txt files from one folder in c drive to another folder in c drive using batch script? Windows批处理将文件从子文件夹复制到一个文件夹 - Windows batch copy files from subfolders to one folder 如何使用PowerShell仅将新文件从一个文件夹复制到另一个文件夹 - How To copy only new files from one folder to another with PowerShell 如何在批处理文件中将选定的文件从一个文件夹复制到另一个文件夹? - How to copy selected files from one folder to another in Batch file? 在Powershell中将X文件从一个文件夹移动到另一个文件夹 - Move X files from one folder to another in Powershell Windows批处理文件脚本将每个第十个文件从一个文件夹复制到另一个文件夹 - windows batch file script to copy every tenth file from a folder to another folder 使用Windows批处理文件将多个文件从具有相同文件名的不同文件夹复制到一个公用文件夹 - Copy multiple files from different folder with same filename to one common folder using windows batch file 将添加到文件夹的新文件复制到Windows中的另一个文件夹 - Copy new files added to a folder to another folder in Windows
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM