2015-10-27 35 views
0

我是WinPowerShell的新手。請問您是否真的想給我一些代碼或信息,如何編寫一個程序,該程序可以處理下一個文件夾中的所有* .txt文件: 1.爲文件中的每行添加字符數 2.如果長度的行超過1024個字符在該文件夾中創建一個子文件夾,並在那裏移動文件(我將如何知道哪個文件的每行1024字符以上)爲每行計算字符數

我試過VB和VBA(這個比較熟悉我),但我想學習一些新的酷東西!

非常感謝!

編輯:我發現的一些代碼的部分即開始

$fileDirectory = "E:\files"; 
foreach($file in Get-ChildItem $fileDirectory) 
    { 
     # Processing code goes here 
    } 

OR

$fileDirectory = "E:\files"; 
foreach($line in Get-ChildItem $fileDirectory) 
{ 
if($line.length -gt 1023){# mkdir and mv to subfolder!} 
} 
+1

你到目前爲止有什麼?如果你的目的是要學習,那麼只要求其他人提供解決方案就會適得其反。 – TZHX

+0

你認爲爲什麼會適得其反?使用stackoverflow,例如,當談到VBA時,我學到了很多東西。請找到我的編輯帖子。我非常瞭解OOP。我只需要一個代碼,來查看,讓我說,「一種新的思維方式」。請支持我。謝謝 – Stefan0309

+1

你在問幾件事情,但看起來你沒有表現出努力,這是提出問題的前兆? ......那裏的編輯看起來很棒。什麼是PowerShell版本 – Matt

回答

0

這裏是我的評論,上面縮進和行中.ps1腳本形式損壞。

$long = @() 

foreach ($file in gci *.txt) { 
    $f=0 
    gc $file | %{ 
     if ($_.length -ge 1024) { 
      if (-not($f)) { 
       $f=1 
       $long += $file 
      } 
     } 
    } 
} 

$long | %{ 
    $dest = @($_.DirectoryName, '\test') -join '' 
    [void](ni -type dir $dest -force) 
    mv $_ -dest (@($dest, '\', $_.Name) -join '') -force 
} 

我還提到標籤,並打破那裏。而不是$f=0if (-not($f)),就可以打破內環與break這樣的:

$long = @() 

foreach ($file in gci *.txt) { 
    :inner foreach ($line in gc $file) { 
     if ($line.length -ge 1024) { 
      $long += $file 
      break inner 
     } 
    } 
} 

$long | %{ 
    $dest = @($_.DirectoryName, '\test') -join '' 
    [void](ni -type dir $dest -force) 
    mv $_ -dest (@($dest, '\', $_.Name) -join '') -force 
} 

難道您發現調用foreach的兩種不同的方式?有詳細的foreach命令,然後有command | %{}其中迭代項目由$_表示。

+0

它的工作!只是一個小問題,如果我在例如files/cchbc文件/ samsung文件/ etc等幾個子目錄中,我需要更改哪行代碼,從所有子文本文本提取並將其sub-subFolder Test ?謝謝! – Stefan0309

+0

@ Stefan0309在第二個示例中,將'foreach(gfile * .txt中的$ file)'更改爲'foreach(gfile -filter * .txt -recurse中的$ file)'。另外,將'foreach(gc $ file中的$ line)'改成'foreach($ gc $ file.FullName中的$ line)'。我認爲其餘的應該沒問題。 – rojo

+0

我對hae> 1024chars的文件有錯誤 'mv:無法找到路徑'D:\ files \ aleks \ Sales and service business assistant.txt',因爲它不存在。 At line:4 char:5 + mv $ _ -dest(@($ dest,'\',$ _。Name)-join'')-force + ~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo:ObjectNotFound:(D: \ files \ aleks \ ... s assistant.txt:String)[Move-Item],ItemNotFoundExce ption + FullyQualifiedErrorId:PathNotFound,Microsoft.PowerShell.Commands.MoveItemCommand' – Stefan0309

2

如果你願意學習,爲什麼不從這裏開始。

您可以使用PS中的Get-Content命令來獲取文件的一些信息。 http://blogs.technet.com/b/heyscriptingguy/archive/2013/07/06/powertip-counting-characters-with-powershell.aspxGetting character count for each row in text doc

+0

此外,[此Tec​​hNet博客條目](http://blogs.technet.com/b/heyscriptingguy/archive/2009/05/06/how-can-i-use-the-if​​-statement-in-windows- powershell.aspx)是我學習如何理解'if'語句,它使用了在其他語言中不常見的比較運算符。 – rojo

1

隨着你的第二次編輯,我確實看到了一些努力,所以我想幫助你。

$path = "D:\temp" 
$lengthToNotExceed = 1024 
$longFiles = Get-ChildItem -path -File | 
    Where-Object {(Get-Content($_.Fullname) | Measure-Object -Maximum Length | Select-Object -ExpandProperty Maximum) -ge $lengthToNotExceed} 

$longFiles | ForEach-Object{ 
    $target = "$($_.Directory)\$lengthToNotExceed\" 
    If(!(Test-Path $target)){New-Item $target -ItemType Directory -Force | Out-Null} 

    Move-Item $_.FullName -Destination $target 
} 

您可以使這是一個單線,但它會不必要地複雜。在由Get-Content返回的數組上使用度量對象。該數組或多或少是一個字符串數組。在PowerShell字符串中有一個length屬性查詢。

這將返回文件中的最大長度線。我們使用Where-Object來過濾只有我們想要的長度的結果。

然後對於每個文件,我們嘗試將它移動到與匹配的文件位於相同位置的子目錄。如果沒有子文件夾存在,我們製作它。


注意事項:

  1. 您需要爲-File開關至少3.0。您可以更新Where-Object以創建另一個子句:$_.PSIsContainer
  2. 這對於包含大量行的文件執行效果不佳。
+0

與命令get-host,我發現我使用2.0版本。我會在win8上試試這個,在我的其他comp上。謝謝! – Stefan0309