Batch file Powershell以增量方式将文件移动到文件夹并压缩每个文件夹

Batch file Powershell以增量方式将文件移动到文件夹并压缩每个文件夹,batch-file,powershell,Batch File,Powershell,我正在尝试编写一个脚本,它将随机选择N个文件,并将它们移动到名为1、2、3的文件夹中。。然后它必须压缩每个文件夹,并运行另一个批处理/脚本 我用来随机复制N个文件的脚本是: gci somefolder | random -c $x | mi -dest someotherfolder 我需要的示例: C:\somefolder (has 11000 files) C:\1 (has 2000 files) C:\2 (has 2000 files) C:\3 (has 2000 files)

我正在尝试编写一个脚本,它将随机选择N个文件,并将它们移动到名为1、2、3的文件夹中。。然后它必须压缩每个文件夹,并运行另一个批处理/脚本

我用来随机复制N个文件的脚本是:

gci somefolder | random -c $x | mi -dest someotherfolder
我需要的示例:

C:\somefolder (has 11000 files)
C:\1 (has 2000 files)
C:\2 (has 2000 files)
C:\3 (has 2000 files)
C:\4 (has 2000 files)
C:\5 (has 2000 files)
C:\6 (has 1000 files)
然后将每个文件夹压缩为

C:\1.zip
C:\2.zip
C:\3.zip
C:\4.zip
C:\5.zip
C:\6.zip
最后,它将运行一个简单的另一个批处理文件(考虑FTP’ing文件)

非常感谢您的功能!这将满足您的要求:

function out-zip { 
    Param([string]$path) 

    if (-not $path.EndsWith('.zip')) {$path += '.zip'} 

    if (-not (test-path $path)) { 
      set-content $path ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18)) 
    } 
    $ZipFile = (new-object -com shell.application).NameSpace($path) 
    $input | foreach {$zipfile.CopyHere($_.fullname)} 
}
$Path = "C:\Temp"
$FileList = gci $path
[int]$FilesPerZip=100
For($i=1;$i -le [Math]::Ceiling($FileList.count/$FilesPerZip);$i++){
    md "c:\$($i)"
    gci $path|random -Count $FilesPerZip|%{$_.moveto("c:\$i\$($_.name)")}
}
(1..[Math]::Ceiling($FileList.count/$FilesPerZip))|%{gi "c:\$_"|out-zip "C:\$_.zip"}
只需更新路径和每个zip文件中需要的文件数

这将计算总共有多少个文件,除以每个zip文件所需的文件数,然后四舍五入。然后它在目标目录中循环,随机抓取X文件并将它们放入顺序编号的文件夹中。最后,它可以打开这些文件夹

如果已经存在具有所需名称的zip文件(即C:\1.zip),则它将失败;如果也存在这些文件夹,则可能会引发错误;如果存在这些文件夹且其中包含匹配的文件,则它将失败,因此,您可能希望预先进行一些检查,以确保这些文件夹和文件不存在,但这回答了您的移动和压缩问题