Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to change the suffix when the PowerShell file name contains []

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to change the suffix when the PowerShell file name contains []. The article is very detailed and has certain reference value. Interested friends must read it!

demand

The well-known problem is that the way video sites publish videos has changed a lot. For example, MP4 file renamed MP41, similar to the following

PS E:\BaiduNetdiskDownload> ls *.mp41 -Recurse Record: E:\BaiduNetdiskDownloadMode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 2019/8/11 20:42 1322066936 [SUBPIG][Eien no Nispa SP].mp41

However, according to the actual

[SUBPIG][Eien no Nispa SP].mp41

This file name, with rename to modify the way, but was prompted to fail.

PS E:\BaiduNetdiskDownload> Rename-Computer "[SUBPIG][Eien no Nispa SP].mp41" "[SUBPIG][Eien no Nispa SP].mp4"Rename-Computer : Could not find positional form parameter that accepts actual parameter "[SUBPIG][Eien no Nispa SP].mp4". Location Line:1 Character: 1+ Rename-Computer "[SUBPIG][Eien no Nispa SP].mp41" "[SUBPIG][Eien no N...+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Rename-Computer],ParameterBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.RenameComputerCommand autocomplete

Why is that? In fact, the reason is very simple, because PowerShell has a special definition for the [] symbol, so it cannot be written like this.

Let's try running it directly from the command line, and the system will automatically fill in what character escapes

PS E:\BaiduNetdiskDownload> & '.\ [SUBPIG][Eien no Nispa SP].mp41'

You can see that the string is preceded by an ampersand followed by a space, and the string is surrounded by '(single quotes). This method can be used to run command-line tools or executable programs that contain special characters, but it is not used when passing parameters.

Let's try specifying a path or file name in one of the parameters and see what the system automatically completes.

PS E:\BaiduNetdiskDownload> Rename-Item -Path '.\` [SUBPIG`]`[Eien no Nispa SP`].mp41'

This is probably the correct way to write a function.

function EscapeWord ($param1) { if ($param1 -match "[[]" -or $param1 -match "[]]") { $param1.replace('[', '`[').replace(']', '`]') }}EscapeWord "E:\BaiduNetdiskDownload\[SUBPIG][FAKE AFFAIR EP05].mp41"E:\BaiduNetdiskDownload\`[SUBPIG`]`[FAKE AFFAIR EP05`].mp41

First, a single piece of data is executed.

PS E:\BaiduNetdiskDownload> Rename-Item -Path '.\` [SUBPIG`]`[Eien no Nispa SP`].mp41' -NewName '.\` [SUBPIG`]`[Eien no Nispa SP`].mp4' Rename-Item : Object under specified path E:\BaiduNetdiskDownload\`[SUBPIG`]`[Eien no Nispa SP`].mp41 does not exist.

Actually, it failed.

work around

It is really speechless, since [] has been redefined under PowerShell, it is always good under CMD. Mix it with CMD. This time I cut the name string, cutting off the last string, and replacing the name. The requirements can still be met, and the modification record is saved additionally in a file.

(Get-ChildItem *.mp41 -Recurse) | ForEach-Object { $fullname = $_.fullname $fullname $NewName = $_.name $NewName = ($NewName[0.. ($NewName.Length - 2)] -join "") $NewName $info = "rename `"$fullname`" `"$NewName`"" cmd /c $info $info | Out-File c:\333.txt -Append -Force -Encoding utf8}

highlight test

final plan

The problem is solved, but the implementation method is too twisted. If there is a native method, the above operation will not be considered.

I searched for keywords

rename file PowerShell match []

I found the following ultimate solution at my employer.

https://answers.microsoft.com/en-us/windows/forum/windows_10-files/how-to-rename-image-files-in-a-folder-all-to-jpg/2a7e2873-e04b-472b-b239-afad2f2020fc

If you can't solve it, it's a sharp weapon to go directly to. net.

Get-ChildItem *.mp41 -Recurse | Rename-Item -newname { [io.path]::ChangeExtension($_.name, "mp4") }

I also found an interesting alternative at https://stackoverflow.com/questions/5574648/use-regex-powershell-to-rename-files, but the same, limited to [], doesn't quite fit my scenario.

Get-ChildItem *.mp41 |ForEach-Object{ Rename-Item $_ $(($_.name -replace '^filename_+','') -replace '_+','') } The above is "How to change suffix when PowerShell filename contains []" All the contents of this article, thank you for reading! Hope to share the content to help everyone, more relevant knowledge, welcome to pay attention to the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report