Question : Replace + rename strings


1
cenz asked 5年 ago

Hi,
I’ve done 2 ps1 files : “replace.ps1” and “rename.ps1”, both files are work, but I cannot merge them into a single .ps1 file.
Here is the “replace.ps1” code as below:

Start your code here 
get-childitem '*.m3u8' | 
foreach-object { (get-content $_.fullname) |
foreach-object { $_ -replace 'H:\\Media Field\\Music\\', 'public1/' `
                    -replace 'H:\\Media Field\\Music \([^\)]+\)\\Compilations \([^\)]+\)\\', 'public2/Compilations (VA)/' `
                    -replace 'H:\\Media Field\\Music \([^\)]+\)\\Compilations\\', 'public2/Compilations/' `
                    -replace 'H:\\Media Field\\Music \([^\)]+\)\\OST\\', 'public2/OST/' `
                    -replace '\\', '/' } |              
set-content $_.fullname -encoding utf8 }

Here is the “rename.ps1” code as below:

Start your code here
get-childItem *.m3u8 | foreach { rename-item $_ $_.name.replace('.m3u8','.m3u')}

Thanks
Tony

×用微信扫描并分享
1 Answers
1
Mooser Lee 管理员 answered 5年 ago
get-childitem '*.m3u8' | foreach-object {
# Replace
$content = get-content $_.fullname
$newContent = $content | foreach{
$_ -replace 'H:\\Media Field\\Music\\', 'public1/' `
-replace 'H:\\Media Field\\Music \([^\)]+\)\\Compilations \([^\)]+\)\\', 'public2/Compilations (VA)/' `
-replace 'H:\\Media Field\\Music \([^\)]+\)\\Compilations\\', 'public2/Compilations/' `
-replace 'H:\\Media Field\\Music \([^\)]+\)\\OST\\', 'public2/OST/' `
-replace '\\', '/'
}
$newContent | Out-File $_.fullname -Encoding utf8
# Rename
rename-item $_.FullName $_.name.replace('.m3u8','.m3u')
}