batch rename using windows powershell

Mohammed Ismail

I am busy with a windows powershell script to batch rename a bunch of folders with IDs, which works perfectly fine. My question is how do i add validation to ensure all the IDs are unique when all my files get renamed. Here is the code:

    param(    
    [Parameter(Mandatory=$true)]
    [int]$idx
)

Get-ChildItem *.sql | Foreach-Object {
 $iPrefix = ('{0:d5}' -f $idx)
 $path = (Split-Path -Path($_))
 $filename = (Split-Path -Path($_) -Leaf) -replace "\[|\]",""
 #%{ write-host $path}
 %{ write-host $filename}
 
 if(!$filename.StartsWith("script","CurrentCultureIgnoreCase"))
 { 
     #%{ write-host "Script$iPrefix - $filename"}
     Rename-Item -LiteralPath(($_)) -NewName("Script$iPrefix - $filename")
     ++$idx
     %{ write-host "Renamed: " + $filename}
 }
}

Here is a screenshot of what i want to avoid: Renamed Folders

As you can see Script02185 is repeated twice, because the script was ran at two different times. How do i ensure that the numbers will remain unique?

rokumaru

Try this.

$files = Get-ChildItem . -Filter *.sql

$prefixedFiles, $unprefixedFiles = $files.Where({ $_.Name -match "^Script\d{5} - " }, 'split')

$usedIDs = [int[]]$prefixedFiles.Name.Substring(6, 5)
$unusedIDs = [System.Collections.Generic.HashSet[int]](1..99999)
$unusedIDs.ExceptWith($usedIDs)

$enumerator = $unusedIDs.GetEnumerator()

$unprefixedFiles | Rename-Item -NewName {

    if (!$enumerator.MoveNext()) { throw "`nThere are no unused IDs." }
    "Script{0:D5} - {1}" -f $enumerator.Current, ($_.Name -replace '\[|\]')

} -ErrorAction Stop -PassThru

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Using rename in the terminal to remove not accepted Windows characters

分類Dev

Using parameters in batch files at Windows command line

分類Dev

Using parameters in batch files at Windows command line

分類Dev

Bash Script: Batch file rename to remove/change all characters illegal under a windows filesystem

分類Dev

Batch rename files to a sequential numbering

分類Dev

Using rename to rename files and directories

分類Dev

Powershell recursive filename read and rename

分類Dev

Using a variable in replacing a substring in another variable in a loop in Windows batch

分類Dev

Make duplicate of same file using batch file in windows

分類Dev

Failing in batch powershell

分類Dev

How do I Batch Rename Folders in OSX?

分類Dev

Batch rename files with unknown names and unknown extensions

分類Dev

Batch File to rename multiple files and move to folder

分類Dev

Batch rename files recovered by Photorec with their offset values

分類Dev

Delete and rename files using unlink() and rename()

分類Dev

How to move a window between desktops using powershell/.net on windows 10

分類Dev

Execute a powershell command from batch

分類Dev

GIT Hooks on Windows with batch

分類Dev

Rename Files using wildcard paths

分類Dev

Problem with "rename" using regex (Linux)

分類Dev

Script to batch rename based on first character of original filename?

分類Dev

CMD Batch rename. Read new names from text file

分類Dev

Linux: Rename multiple images after parent directory and add suffix (batch)

分類Dev

Batch For Do Test if exist rename url link on desktop

分類Dev

Batch file to rename files by adding different suffix to each file

分類Dev

When copy long string to clipboard using Windows batch, there is "The syntax of the command is incorrect" error

分類Dev

Move files older than 90 days in a set of directories where the directory name contains certain text using batch or powershell

分類Dev

Using rename to rename upper case to lower case and add a character

分類Dev

Rename user account on Windows 7 Home

Related 関連記事

  1. 1

    Using rename in the terminal to remove not accepted Windows characters

  2. 2

    Using parameters in batch files at Windows command line

  3. 3

    Using parameters in batch files at Windows command line

  4. 4

    Bash Script: Batch file rename to remove/change all characters illegal under a windows filesystem

  5. 5

    Batch rename files to a sequential numbering

  6. 6

    Using rename to rename files and directories

  7. 7

    Powershell recursive filename read and rename

  8. 8

    Using a variable in replacing a substring in another variable in a loop in Windows batch

  9. 9

    Make duplicate of same file using batch file in windows

  10. 10

    Failing in batch powershell

  11. 11

    How do I Batch Rename Folders in OSX?

  12. 12

    Batch rename files with unknown names and unknown extensions

  13. 13

    Batch File to rename multiple files and move to folder

  14. 14

    Batch rename files recovered by Photorec with their offset values

  15. 15

    Delete and rename files using unlink() and rename()

  16. 16

    How to move a window between desktops using powershell/.net on windows 10

  17. 17

    Execute a powershell command from batch

  18. 18

    GIT Hooks on Windows with batch

  19. 19

    Rename Files using wildcard paths

  20. 20

    Problem with "rename" using regex (Linux)

  21. 21

    Script to batch rename based on first character of original filename?

  22. 22

    CMD Batch rename. Read new names from text file

  23. 23

    Linux: Rename multiple images after parent directory and add suffix (batch)

  24. 24

    Batch For Do Test if exist rename url link on desktop

  25. 25

    Batch file to rename files by adding different suffix to each file

  26. 26

    When copy long string to clipboard using Windows batch, there is "The syntax of the command is incorrect" error

  27. 27

    Move files older than 90 days in a set of directories where the directory name contains certain text using batch or powershell

  28. 28

    Using rename to rename upper case to lower case and add a character

  29. 29

    Rename user account on Windows 7 Home

ホットタグ

アーカイブ