Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 如何使用批处理文件删除子字符串_Powershell_Batch File_Batch Rename - Fatal编程技术网

Powershell 如何使用批处理文件删除子字符串

Powershell 如何使用批处理文件删除子字符串,powershell,batch-file,batch-rename,Powershell,Batch File,Batch Rename,我有几千个格式相似但不同的文件,即: [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 我想删除“[文

我有几千个格式相似但不同的文件,即:

[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
我想删除“[文本]”并保持其他内容不变。因为文本不同,所以我不能严格控制字符数

set var=%1
@echo %var:~-7%
我尝试涉猎powershell命令行并尝试:

dir *.xls | Rename-Item -NewName {$_.Name -replace '[Explode\s\'}
但给出了以下错误:

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

我只是想找个替代品来工作。。。不走运。

您的PowerShell相当可靠,唯一的问题是方括号是保留字符,必须用前面的反斜杠转义。适用于您的正则表达式应该是:

-replace '\[.*?\]\s*'
你可以在这个链接上看到详细的解释:

编辑:对不起,我刚开始工作,看到了你的信息。我用一个测试文件尝试了这个正则表达式,并且能够毫无问题地重命名它。已创建测试文件:

C:\Temp\[glarb]ThisIsATest.tst
然后,我在PowerShell中运行了以下行:

Get-ChildItem C:\Temp\*.tst | Rename-Item -NewName {$_.Name -replace "\[.*\]\s*"}
之后我看了看,留下了文件:

C:\Temp\ThisIsATest.tst

我不知道你的代码为什么不起作用,可能是你的PowerShell版本中的一个bug。正则表达式和命令在运行于Win8.1的PS v4中确实有效。

您的PowerShell相当可靠,唯一的问题是方括号是保留字符,必须使用前面的反斜杠转义。适用于您的正则表达式应该是:

-replace '\[.*?\]\s*'
你可以在这个链接上看到详细的解释:

编辑:对不起,我刚开始工作,看到了你的信息。我用一个测试文件尝试了这个正则表达式,并且能够毫无问题地重命名它。已创建测试文件:

C:\Temp\[glarb]ThisIsATest.tst
然后,我在PowerShell中运行了以下行:

Get-ChildItem C:\Temp\*.tst | Rename-Item -NewName {$_.Name -replace "\[.*\]\s*"}
之后我看了看,留下了文件:

C:\Temp\ThisIsATest.tst

我不知道你的代码为什么不起作用,可能是你的PowerShell版本中的一个bug。在Win8.1中运行的PS v4中,正则表达式和命令确实可以工作。

我的正则表达式Foo不是很强(它在待办事项列表中),但您应该能够使用子字符串来完成这项工作

dir *.xls | Rename-Item -NewName { ($_.Name.Substring($_.Name.IndexOf(']') + 1)).Trim() }
仔细看看实际发生了什么变化,我们将返回一个子字符串
$\uName
,它来自第一个']'字符后面的索引,然后还修剪输出,处理任何额外的空格(即空格)

这有一个明显的限制,即不适用于以下形式的文件:

this[should]notBeEdited.xls

我的正则表达式Foo不是很强(它在待办事项列表中),但是您应该能够使用子字符串来完成这一点

dir *.xls | Rename-Item -NewName { ($_.Name.Substring($_.Name.IndexOf(']') + 1)).Trim() }
仔细看看实际发生了什么变化,我们将返回一个子字符串
$\uName
,它来自第一个']'字符后面的索引,然后还修剪输出,处理任何额外的空格(即空格)

这有一个明显的限制,即不适用于以下形式的文件:

this[should]notBeEdited.xls
使用纯批次:

@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"
使用my,一个纯脚本实用程序(混合JScript/batch),从XP开始在任何Windows机器上本机运行:

call jren "^\[.*?] *" "" /fm "[*]*"
如果直接从命令行使用命令,则可以放弃调用。

使用纯批处理:

@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"
使用my,一个纯脚本实用程序(混合JScript/batch),从XP开始在任何Windows机器上本机运行:

call jren "^\[.*?] *" "" /fm "[*]*"

如果直接从命令行使用该命令,则可以挂断呼叫。

我尝试了这两种方法,结果得到:重命名项:无法重命名,因为“Microsoft.PowerShell.Core\FileSystem::C:\1[Explode]Thisfile1.xls”中的项不存在。在第1行char:24+Dir*.xls | Rename Item这是我使用的另一个命令:Dir.xls | Rename Item-NewName{$\ u.Name-replace'[.?]\s*}看起来Powershell多年来一直存在括号问题。我尝试移动项目以及MS和StackExchange建议的-LiteralPath,但没有成功。谢谢你的RegEx链接,我把它加入书签。。。正如Windos所说,我的Reg Foo也不强:)我尝试了两种方法,结果得到:重命名项:无法重命名,因为“Microsoft.PowerShell.Core\FileSystem::C:\1[Explode]Thisfile1.xls”中的项不存在。在第1行char:24+Dir*.xls | Rename Item这是我使用的另一个命令:Dir.xls | Rename Item-NewName{$\ u.Name-replace'[.?]\s*}看起来Powershell多年来一直存在括号问题。我尝试移动项目以及MS和StackExchange建议的-LiteralPath,但没有成功。谢谢你的RegEx链接,我把它加入书签。。。正如Windos所说,我的Reg-Foo也不强:)编辑-OMG,我的JREN解决方案是草率的:-(我忘了转义
[
元字符-现在全部修复。编辑-OMG,我的JREN解决方案是草率的:-(我忘了转义
[
元字符-现在全部修复)。