添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

I would like to know how I can rename the files from a specific folder with a sequence of only even and odds numbers in PowerShell. E.g. Folder1: pag_001.jpg, pag_003.jpg, pag_005.jpg.... pag_201.jpg , Folder2: pag_002.jpg, pag_004.jpg, pag_006.jpg.... pag_200.jpg . It is because I have a document that was scanned first the odds pages and secondly the even pages, therefore their file names are in a consecutive sequence from 1 to 201. Then I separated one half of the files which are the odds pages in a new place: Folder1 , and the second half,the even pages in the Folder2 . That is why I would like change the names first and the join again together with their new names.

I have tried this based in a similar post :

At the moment I could generate even number sequences like that:

ForEach ($number in 1..100 ) { $number * 2}

and odd numbers like that:

ForEach ($number in 0..100 ) { $number *2+1}

and wanted apply the sequences generated before to rename my files like that:

cd C:\test\Folder1
$i = $number * 2
Get-ChildItem *.jpg | %{Rename-Item $_ -NewName ('pag_{0:D3}.jpg' -f $i++)}

but it doesn't works! Any suggestions are welcome

Regards,

Your $i++ adds 1 each time, this is why it also add even numbers,

You can create array of Odd Numbers then use the $i++ to step one item in the array, like this:

$path = "C:\test\Folder1"
$oddNumbersArray = 0..100 | % {$_ *2 +1}
$i = 0
Get-ChildItem $path -Filter *.jpg | % {Rename-Item $_ -NewName ("pag_$($oddNumbersArray[$i]).jpg") ;$i++}

For Even Numbers change the $oddNumbersArray line to {$_ *2}

using -f operator inside the expression is a bit ambiguous, since you don't actually substitute any placeholders in the string. Why not just move $i++ outside the Rename-Item operation? – Mathias R. Jessen Nov 16 '15 at 12:59

Bunch of ways to do this. For mine we add each index as a member so that it is more easily accessible in the rename item script block.

$index = 0 
Get-ChildItem $path -Filter "*.jpg" | ForEach-Object{
    $index = $index +2
    $_ | Add-Member -Name "Index" -MemberType NoteProperty -Value $index -PassThru
} | Rename-Item -NewName {'pag_{0:D3}.jpg' -f $_.Index} -WhatIf 

Using Add-Member in a ForEach-Object we update the value of index and then add it as a property of the same name. Then in your rename-item scriptblock we can call that property. Remove the -WhatIf after you verified the new names are what you wanted. Switch $index between 0 and -1 for even and odd respectively.

Another method using a global index variable and mitigating the pipeline by using calculated properties to create the pipeline variables that Rename-Item uses.

$path = "C:\Temp\csv"
$global:index = 0 # Use -1 for odd
Get-ChildItem $path -Filter "*.csv" | 
    Select-Object @{Name="Path";Expression={$_.FullName}},
        @{Name="NewName";Expression={$global:index = $global:index + 2; 'pag_{0:D3}.jpg' -f $global:index}} | 
    Rename-Item  -WhatIf
                I feel bad since I know it can be more verbose than this. So I will try to update it.
                    – Matt
                Nov 16 '15 at 13:17
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

site design / logo © 2019 Stack Exchange Inc; user contributions licensed under cc by-sa 4.0 with attribution required. rev 2019.10.7.35131