Reputation: 1530
I have several thousand files with similar, but different formats, ie:
[Block 1] Thisfile.txt
[Block 1] Thisfile1.txt
[Block 1] Thisfile2.txt
[Backup001] Thatfile1.doc
[Backup001] Thatfile2.doc
[Backup001] Thatfile3.doc
[Explode] Thisplace.xls
[Explode] Thisplace1.xls
[Explode] Thisplace2.xls
I want to remove the "[text] " and keep everything else the same. Since that text varies I can't do a strict number of characters ie
set var=%1
@echo %var:~-7%
I tried to dabble with powershell commandline and tried:
dir *.xls | Rename-Item -NewName {$_.Name -replace '[Explode\s\'}
But was given the following error:
Rename-Item : The input to the script block for parameter 'NewName' failed. Invalid regular expression pattern: [Explode\s\.
At line:1 char:33
+ Dir *.xls | Rename-Item -NewName <<<< {$_.Name -replace '[Explode]\s\'}
+ CategoryInfo : InvalidArgument: (C:\1\[Explode... Thisfile1.xls:PSObject) [Rename-Item], ParameterBindingException
+ FullyQualifiedErrorId : ScriptBlockArgumentInvocationFailed,Microsoft.PowerShell.Commands.RenameItemCommand
I've searched StackExchange, and the 'batch-rename' tag, and found (and tried) several similar things that I thought I could tweak, but no luck.
Here's the latest based on another StachExchange answer:
for %%F in (*.xls) do (
SET string=%%F
SET modified=!string:Explode=1!
echo !modified!
)
I was just trying to get ANY replace to work... No luck.
Upvotes: 1
Views: 856
Reputation: 36342
Your PowerShell was fairly solid, the only issue is that the square braces are reserved characters and have to be escaped with a preceding backslash. The RegEx that should work for you should be:
-replace '\[.*?\]\s*'
You can see the detailed explanation of that at this link:
https://regex101.com/r/yM6sQ9/1
Edit: Sorry, just got into work and saw your messages. I tried that regex with a test file and was able to rename it without issues. Test file created:
C:\Temp\[glarb]ThisIsATest.tst
I then ran the following line in PowerShell:
Get-ChildItem C:\Temp\*.tst | Rename-Item -NewName {$_.Name -replace "\[.*\]\s*"}
After which I looked and was left with the file:
C:\Temp\ThisIsATest.tst
I'm not sure why your code didn't work, perhaps it is a bug in your version of PowerShell. The regex and command do work in PS v4 running in Win8.1.
Upvotes: 4
Reputation: 130919
Using pure batch:
@echo off
for /f "delims=" %%F in (
'dir /b /a-d [*]*'
) do for /f "tokens=1* delims=]" %%A in (
"%%F"
) do for /f "tokens=*" %%C in ("%%B") do ren "%%F" "%%C"
Using my JREN.BAT regular expression renaming utility, a pure script utility (hybrid JScript/batch) that runs natively on any Windows machine from XP onward:
call jren "^\[.*?] *" "" /fm "[*]*"
You can drop the CALL if you use the command directly from the command line.
Upvotes: 1
Reputation: 1956
My RegEx-Foo is not very strong (it's on the to-do list), but you should be able to do this with a sub string.
dir *.xls | Rename-Item -NewName { ($_.Name.Substring($_.Name.IndexOf(']') + 1)).Trim() }
Looking a little closer at what actually changed, we're returning a sub string of $_.Name
which is from the index after the first ']' character and then also trims the output, dealing with any extra white spaces (namely spaces).
This has the obvious limitation of not working for files that are in the form of:
this[should]notBeEdited.xls
Upvotes: 1