[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[问题求助] powershell 数字比较不起作用

本帖最后由 gflrlm 于 2019-10-13 11:57 编辑

大家好,powershell发现一个奇怪的问题, 我 D:\000下面有几百个txt文件,命名是 1.txt, 2.txt 3.txt .... 100.txt
然后我想每10个移动到一个文件夹里面,文件夹命名 1-10,11-20,21-30.。。

代码如下:
  1.   $Path ="D:\000\"
  2.   #$Path ="D:\001----nobackup---imdb"
  3.   $Filter = '*.*'
  4.   $_Debug = 1
  5.    
  6.   write-host " filelist begin:"
  7.   $filelist = @()
  8.   Get-ChildItem -Path $Path  | ?{$_.psiscontainer -eq $false} |  %{$filelist += ($_.FullName )}
  9. # Get-ChildItem -Path $Path -name
  10.       
  11.   write-host "  in  end:"  $fileList.Count
  12.   $step=10 #50000
  13.   $total=200 #600000
  14.   for($i=0;$i -le $total/$step;$i++) {
  15.   $start=$step*$i+1
  16.   $end=$step*($i+1)
  17.   $path_mkdir="$Path"+"$start"+"-"+"$end"
  18.   if(!(Test-Path $path_mkdir)) {
  19.      mkdir $path_mkdir
  20.      write-host "mkdir $path_mkdir"
  21.   }
  22.   }
  23.   
  24.   Foreach($file in $fileList)
  25.   {
  26. if($file -match ".*\\(.*).txt") {# 获取 id
  27.    $file_id=$matches[1]
  28. }
  29. write-host "f = " $file ", id= <"$file_id">"
  30.    for($i=0;$i -le $total/$step;$i++) {
  31.    $start=$step*$i+1
  32.    $end=$step*($i+1)
  33.    $path_mkdir="$Path"+"$start"+"-"+"$end"
  34.        write-host "($file_id -ge $start) -and ($file_id -le $end)"
  35.    if(($file_id -ge $start) -and ($file_id -le $end) ) {
  36.       mv -force $file  $path_mkdir
  37.       write-host "move $file to $path_mkdir" -ForegroundColor red
  38.    }
  39.    }
  40.    exit
  41.   }
复制代码
但是从91.txt开始就不起作用了,移动不了,这个条件进不去 "if(($file_id -ge $start) -and ($file_id -le $end) ) {"
通过下面log发现,并没有问题。 真是神奇了。。
  1. f =  D:\000\91.txt , id= < 91>
  2. (91 -ge 1) -and (91 -le 10)
  3. (91 -ge 11) -and (91 -le 20)
  4. (91 -ge 21) -and (91 -le 30)
  5. (91 -ge 31) -and (91 -le 40)
  6. (91 -ge 41) -and (91 -le 50)
  7. (91 -ge 51) -and (91 -le 60)
  8. (91 -ge 61) -and (91 -le 70)
  9. (91 -ge 71) -and (91 -le 80)
  10. (91 -ge 81) -and (91 -le 90)
  11. (91 -ge 91) -and (91 -le 100)
  12. (91 -ge 101) -and (91 -le 110)
  13. (91 -ge 111) -and (91 -le 120)
  14. (91 -ge 121) -and (91 -le 130)
  15. (91 -ge 131) -and (91 -le 140)
  16. (91 -ge 141) -and (91 -le 150)
  17. (91 -ge 151) -and (91 -le 160)
  18. (91 -ge 161) -and (91 -le 170)
  19. (91 -ge 171) -and (91 -le 180)
  20. (91 -ge 181) -and (91 -le 190)
  21. (91 -ge 191) -and (91 -le 200)
  22. (91 -ge 201) -and (91 -le 210)
复制代码
PS, win10 系统,版本信息如下:


PS D:\>  $psversiontable

Name                           Value                                                                                   
----                           -----                                                                                   
PSVersion                      5.1.18362.145                                                                           
PSEdition                      Desktop                                                                                 
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                 
BuildVersion                   10.0.18362.145                                                                          
CLRVersion                     4.0.30319.42000                                                                        
WSManStackVersion              3.0                                                                                    
PSRemotingProtocolVersion      2.3                                                                                    
SerializationVersion           1.1.0.1

$file_id 值好像一直是91

TOP

猜测是把数字当着字符串比较了,
(91 -ge 201) -and (91 -le 210)
91 -ge 201 为 true,但 91 -le 210 为 false
试试 if((1*$file_id -ge $start) -and (1*$file_id -le $end) )

TOP

这样应该也可以
  1. $Path = "D:\000\"
  2. $step=10 #50000
  3. $total=100 #600000
  4. for($i=0; $i -lt $total; $i++) {
  5.   $start = [Math]::floor($i/$step)*$step + 1;
  6.   $end = [Math]::floor($i/$step+1)*$step;
  7.   $path_mkdir = $Path + $start + "-" + $end;
  8.           $file = $path + ($i + 1) + '.txt';
  9.           if( !(Test-Path $path_mkdir) ){
  10.               mkdir $path_mkdir;
  11.           }
  12.           if( Test-Path $file ){
  13.               mv -force $file ($path_mkdir + '\');
  14.           }
  15. }
复制代码

TOP

回复 3# WHY
这个可以,确实当做 字符串了。 我用的perl的思路,以为能自动转,哈哈。

TOP

回复 4# WHY


    这个也是可以的,理由同上,哈哈, 感谢各位帮忙,多谢

TOP

返回列表