[英]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.