批处理之家's Archiver

gflrlm 发表于 2019-10-16 22:48

PowerShell文件分组_每5万个文件新建一个文件夹存放

某文件夹内有100w个文件,命名如下:
1.txt
2.txt
3.txt
...
1000000.txt

按照每5万个文件 ,根据文件名的数字大小 存放到对应的文件夹内,文件夹命名如下:

代码第二行:

[color=Red]$Path ="D:\001----nobackup---imdb\" 【设置你的100w个文件所在文件夹】
[/color]下面的代码31 32行,   

[color=Red]$step=50000 【设置步进,每隔多少个文件】
$total=1100000 【设置总文件个数,这里设置为110w,应当大于总文件夹个数】
[/color]

代码如下:[code]
  #$Path ="D:\000\"
  $Path ="D:\001----nobackup---imdb\"
  $Filter = '*.*'
  $_Debug = 1
function processing_grep(  $file ){  
  #Foreach($file in $fileList){
  
        if($file -match ".*\\(.*).txt") {# 获取 id
           $file_id=$matches[1]
        }
        #write-host "f = " $file ", id= <"$file_id">"
   for($i=0;$i -le $total/$step;$i++) {
           $start=$step*$i+1
           $end=$step*($i+1)
           $path_mkdir="$Path"+"$start"+"-"+"$end"
       #write-host "($file_id -ge $start) -and ($file_id -le $end)"
           if((1*$file_id -ge $start) -and (1*$file_id -le $end) ) {
              mv  $file  $path_mkdir
              write-host "move $file to $path_mkdir" -ForegroundColor red
           }
   }#for
   #cmd /c "pause"
   
  #}#foreach
  }
   

  $step=10 #50000
  $total=200 #600000
  $step=50000
  $total=600000
  for($i=0;$i -le $total/$step;$i++) {
          $start=$step*$i+1
          $end=$step*($i+1)
          $path_mkdir="$Path"+"$start"+"-"+"$end"

      #write-host "mkdir <$path_mkdir>"
          if(!(Test-Path $path_mkdir)) {
             mkdir $path_mkdir
             write-host "mkdir $path_mkdir"
          }
  }


  
  write-host " filelist begin:"
  #$filelist = @()
  #Get-ChildItem -Path $Path  | ?{$_.psiscontainer -eq $false} |  %{$filelist += ($_.FullName )}
  ,@( Get-ChildItem -Path $Path  | ?{$_.psiscontainer -eq $false} |  %{processing_grep ($_.FullName )} )
  
      [/code]代码运行结果如下:

xczxczxcz 发表于 2019-10-17 16:40

很复杂                                     .

for_flr 发表于 2021-12-21 15:28

[code]Foreach($a in 1000..1999){sc "$a.txt" ""}  #生成1000个测试TXT文本[/code][code]For($i=0;$i -ne 20;$i+=1){
    md $i
    dir -filter *.txt|select-object -first 50|%{move $_ $a}
}[/code]正在学习powershell,写一个按数量分文件的简单代码。

页: [1]

Powered by Discuz! Archiver 7.2  © 2001-2009 Comsenz Inc.