How can I bulk rename files using PowerShell?

前端 未结 4 1286
时光取名叫无心
时光取名叫无心 2021-02-20 12:22

I\'m trying to recursively rename a bunch of TFS folders using tf rename, PowerShell and a regular expression but I\'m having some issues with PowerShell as I haven

相关标签:
4条回答
  • 2021-02-20 12:36

    Notes:

    • TFS has native cmdlets -- no need for tf.exe in most cases.
    • The time complexity of workspace operations depends on the number of pending renames already in the workspace. In TFS 2005/2008 it's significantly worse than linear. Bottom line, you should really consider batching up renames into multiple checkins if you have a large # of items, otherwise every single "tf rename" (or New-TfsPendingChange -Rename if using the cmdlets) will start taking minutes.
    0 讨论(0)
  • 2021-02-20 12:40

    Running the above commands requires the tf.exe to have been aliased as 'tf'.. or it did on my machine at least.

    Run this command:

    Set-Alias tf "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer\tf.exe"
    

    Update the path to tf.exe as appropriate

    Also consider adding the line to your profile for future use

    notepad $PROFILE
    
    0 讨论(0)
  • 2021-02-20 12:46

    Try this:

    dir . | foreach { $newname = $_.Name -replace "^5", "2.3.2"; tf rename $_ $newname }
    
    0 讨论(0)
  • 2021-02-20 12:52

    I would first filter by 5* so you only process names that start with 5. Also, in this case since tf.exe isn't a PowerShell cmdlet, you don't want to use a scriptblock to determine a new name. Just use a grouping expression like so:

    dir -filter 5* | foreach { tf rename $_ ($_.Name -replace '^5', '2.3.2')}
    

    BTW, when you are trying to debug parameter passing to a native EXE like this it is immensely helpful to use the echoargs.exe utilty from the PowerShell Community Extensions. This is what it told me about your original approach:

    6# dir -filter 5* | foreach { echoargs rename $_ { $_.Name -replace '^5', '2.3.2' } }
    Arg 0 is <rename>
    Arg 1 is <5foo.txt>
    Arg 2 is <-encodedCommand>
    Arg 3 is <IAAkAF8ALgBOAGEAbQBlACAALQByAGUAcABsAGEAYwBlACAAJwBeADUAJwAsACAAJwAyAC4AMwAuADIAJwAgAA==>
    Arg 4 is <-inputFormat>
    Arg 5 is <xml>
    Arg 6 is <-outputFormat>
    Arg 7 is <text>
    
    0 讨论(0)
提交回复
热议问题