重命名文件夹时,power shell 出现文件未找到错误 [英] power shell is giving file not found error when renaming a folder

查看:76
本文介绍了重命名文件夹时,power shell 出现文件未找到错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是更改文件夹的名称,如果其中的任何文件包含字符串ErrorCode :value",其中值可以是零以外的任何值.我在 $dirName 文件夹中有旧路径,在 $newPath 文件夹中有后缀错误的新路径.

What I am trying to do is change the name of a folder if any file in it contains contains string "ErrorCode :value" where the value can be anything other than zero. I have old path in $dirName folder and new path with a suffix Error in $newPath folder.

这是代码

$fileNames = Get-ChildItem -Path $scriptPath -Recurse -Include *.data
foreach ($file in $fileNames) {
    If (Get-Content $file | %{$_ -match '"ErrorCode": 0'}) 
{

}
else{
    $dirName= Split-Path $file
    $newPath=$dirName+"Error"
    Rename-Item $dirName $newPath
}
}

我希望它重命名文件夹,而不是它给我一个它不存在的错误.

I expect it to rename the folder instead it gives me an error that it does not exist.

我该如何解决?还有没有更好的方法来解决这种情况,因为我两天前就开始学习 powershell?

How do I solve that? Also is there any better approach for this situation because I have started learning powershell before two days?

推荐答案

如果同一文件夹中有多个 *.data 文件,您的代码将在找到第一个文件后重命名该文件夹不包含 "ErrorCode": 0.当它尝试获取下一个文件或再次重命名文件夹时,由于已重命名,它将无法找到它.

If there are multiple *.data-files in the same folder you code will rename the folder after it found the first file that does NOT contain "ErrorCode": 0. When it tries to get the next file or to rename the folder again, it won't be able to find it since it has been renamed.

您写了要重命名文件夹 if 文件 -match '"ErrorCode": 0' 但如果满足此条件,则执行 {}(无).但是,如果不满足条件,则执行代码 else{...}

You wrote you want to rename the folder if the file -match '"ErrorCode": 0' but if this condition is fulfilled you execute {} (nothing). However if the condition is not fulfilled you execute your code else{...}

为防止您的代码在文件夹中工作时多次重命名文件夹,请先将文件夹名称收集到数组中,然后再重命名:

To prevent your code from renaming the folder multiple times while working in the folder, collect the foldernames first in an array an rename them later:

$fileNames = Get-ChildItem -Path $scriptPath -Recurse -Include *.data
$FoldersToRename = @() #initialize as array

foreach ($file in $fileNames) {
    If (Get-Content $file | %{$_ -match '"ErrorCode": 0'}) 
    {
        $FoldersToRename += Split-Path $file
    }
}

$SingelFolders = $FoldersToRename | Select-Object -Unique #Select every folder just once
$SingelFolders | ForEach-Object { 
    $newPath=$_ + "Error"
    Rename-Item $_ $newPath
}

<小时>

edit:匹配任何东西,但 '"ErrorCode": 0'


edit: Matching anything BUT '"ErrorCode": 0'

-match 使用正则表达式 (regex),在这里非常方便.
除了 0 之外的任何一位数在正则表达式中都是 [1-9] .如果您的 ErrorCode 可以有多个数字,您可以使用 \d{2,} 来匹配 2 个或多个 ({2,}) 数字 (\d).组合这些看起来像这样:([1-9]|\d{2,}) (| = or)
这是上面的代码:

-match uses regular expressions (regex) wich comes very handy in here.
Any single-digit number but 0 would be [1-9] in regex. If your ErrorCode can have multiple digits, you can use \d{2,} to match 2 or more ({2,}) numbers (\d). Combined these would look like this: ([1-9]|\d{2,}) (| = or)
And here is it in the code from above:

foreach ($file in $fileNames) {
    If (Get-Content $file | %{$_ -match '"ErrorCode": ([1-9]|\d{2,})'}) 
    {
        $FoldersToRename += Split-Path $file
    }
}

edit2:忽略空格/tabs:

edit2: Ignoring whitespaces /tabs:

任何类型的空格的正则表达式是 \s.* 表示 0 个或多个:字符串将是 '"ErrorCode":\s*([1-9]|\d{2,})'

regex for anykind of whitespace is \s. * means 0 or more: the string would be '"ErrorCode":\s*([1-9]|\d{2,})'

edit3:代码"可选:

这是匹配任何类型的 Error 的最终正则表达式字符串,带有可选的引号、代码"和冒号:
"?Error(Code)?"?:?\s*([1-9]|\d{2,}) > {$_ -match '"?Error(Code)?"?:?\s*([1-9]|\d{2,})'}
匹配示例:

Here is the ultimate regex string to match ay kind of Error with optional quotation marks, "Code" and the colon:
"?Error(Code)?"?:?\s*([1-9]|\d{2,}) > {$_ -match '"?Error(Code)?"?:?\s*([1-9]|\d{2,})'}
Matchingexamples:

错误代码":404

错误代码":5

错误代码":0404

"错误代码":0404

"ErrorCode":0404

错误:1

错误 1

regex101.com

这篇关于重命名文件夹时,power shell 出现文件未找到错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆