简体   繁体   中英

How to use dash or hyphen to join a string array in Powershell

I wrote code like this:

$count = 0
$path = "C:\Videos\"
$oldvids = Get-ChildItem -Path $path -Include *.* -Recurse
foreach ($oldvid in $oldvids) {
    $curpath = $oldvid.DirectoryName
    $name = [System.IO.Path]::GetFileNameWithoutExtension($oldvid)
    $names = $name.Split(" - ")
    $names[0] = ""
    $metadata_title = $names -join "-"
    $ext = [System.IO.Path]::GetExtension($oldvid)

    if ($name.StartsWith("new_") -eq $false)
    {
        $newvid = $curpath + "/new_" + $name + ".mp4"
        if ([System.IO.File]::Exists($newvid) -eq $false)
        {
            $count++
            
            Write-Output $metadata_title
            
        }
    }
}

But this code causes a file name like this:

Chapter 1 - New Video

to become:

Chapter 1---New Video

How can I make sure a single - is actually only one? Do I have to escape it?

The idea is to eliminate first part of the file names, so from:

01 - Chapter 1 - Video 1

to:

Chapter 1 - Video 1

So I wanted to split using " - " and then join everything back without the first element in the split array.

Looking at your example and your explanation of changing metadata with ffmpeg on each file, I guess this is what you need:

$count = 0
$path = 'C:\Videos'
# get a list of old video files (these do not start with 'new_')
$oldvids = Get-ChildItem -Path $path -Filter *.mp4 -File -Recurse |
           Where-Object { $_.Name -notmatch '^new_' }

foreach ($oldvid in $oldvids) {
    # if the file is called 'C:\Videos\01 - Chapter 1 - Video 1.mp4'
    $tempName = $oldvid.Name -replace '^\d+\s*-\s*(.+)', 'new_$1'  # --> new_Chapter 1 - Video 1.mp4
    # or do
    # $tempName = 'new_' + ($oldvid.Name -split '-', 2)[-1].Trim() # --> new_Chapter 1 - Video 1.mp4
    # or
    # $tempName = $oldvid.Name -replace '^\d+\s*-\s*', 'new_'      # --> new_Chapter 1 - Video 1.mp4

    # combine the current file path with the temporary name
    $outputFile = Join-Path -Path $oldvid.DirectoryName -ChildPath $tempName

    #######################################################################
    # next do your ffmpeg command to change metadata
    # for input you use $oldvid.FullName and for output you use $outputFile
    Write-Host "Updated file $($oldvid.Name) as $tempName"
    #######################################################################

    # when done with ffmpeg, delete the original (or for safety move it to somewhere else)
    Write-Host "Deleting file '$($oldvid.Name)'"
    $oldvid | Remove-Item -WhatIf 

    # and rename the updated file by removing the 'new_' part from its name
    $newName = ($tempName -replace '^new_').Trim()
    Write-Host "Renaming updated file to '$newName'"
    $tempName | Rename-Item -NewName $newName

    # all done, proceed with the next file
    $count++
}

Note: I have added switch -WhatIf to the Remove-Item line. This is a safety measure that will only display what file would be deleted without actually deleting it.
If you are sure the correct file should be deleted, then remove that -WhatIf switch so the original file gets destroyed after maipulating it with ffmpeg.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM