搜索并用txt文件支持替换文件和文件夹名称

时间:2019-02-24 19:50:43

标签: powershell

我有很多文件夹,并且在这些不同的文件中。每个文件夹及其子文件具有相同的名称和不同的扩展名,因此在ABC文件夹中有ABC.png,ABC.prj,ABC.pgw文件,在DEF文件夹中有DEF.png,DEF.prj, DEF.pgw文件等。 使用脚本,我创建了一个带有png文件名列表的txt文件。然后,在第2行中为第1行中的名称添加一个新名称,在第4行中为第3行中的名称添加一个新名称,依此类推。 现在,我正在搜索一个powershell脚本,该脚本: -扫描所有文件夹以查找第1行中的名称,并将其替换为第2行中的名称 -扫描所有文件夹中第3行中的名称,然后将其替换为第4行中的名称,依此类推

我在下面尝试过此方法,但这不起作用。

您有什么建议吗?谢谢

Function test(Vec)
    n = Vec.Rows.Count
    Dim TestVector
    ReDim TestVector(n, 1)

    For i = 1 To n
        A = Vec(i) * 2
        TestVector(i, 1) = A
    Next i

    test = TestVector
End Function

2 个答案:

答案 0 :(得分:0)

您的代码段中的问题是它永远不会结束。 我尝试了一下,它仍然有效,但一直循环不断。 我创建了一个包含文件a.txt,b.txt和c.txt的文件夹。 在map.txt中,我有以下内容:

a.txt
a2.md
b.txt
b2.md
c.txt
c2.md

运行以下脚本,我设法按预期重命名了每个文件。

$0=0
$1=1

$find=Get-Content D:\map.txt | Select -Index $0
while($find) {
    $find=Get-Content D:\map.txt | Select -Index $0
    $repl=Get-Content D:\map.txt | Select -Index $1

    if(!$find -Or !$repl) {
        break;
    }

    Get-ChildItem D:\Files -Recurse | Rename-Item -NewName { $_.name -replace $find, $repl} -verbose
    $0=$0+2
    $1=$1+2
}

答案 1 :(得分:0)

我相信您的代码以及曼努埃尔给您的代码有很多问题。

  1. 尽管您有旧文件名和新文件名的列表,但您并未在-replace cmdlet中使用该文件名,而是尝试替换找到的所有文件。
  2. 使用.使用正则表达式替换,这意味着文件名中的特殊字符Any Character被视为*.png,而不仅仅是点。
  3. 您正在尝试查找-Filter文件,但没有在Get-ChildItem cmdlet中添加C:\1\Srv\MapsName.txt,因此现在它将返回所有文件类型。

无论如何,我为您提供了另一种方法:

如果您的输入文件picture1.png ABC_1.png picture2.png DEF_1.png picture3.png DEF_2.png 看起来像这样:

$mapsFile   = 'C:\1\Srv\2_MapsName.txt'
$searchPath = 'C:\1\NewMaps'

# Read the input file as an array of strings.
# Every even index contains the file name to search for. 
# Every odd index number has the new name for that file.
$lines = Get-Content $mapsFile

# Create a hashtable to store the filename to find
# as Key, and the replacement name as Value
$lookup = @{}
for ($index = 0; $index -lt $lines.Count -1; $index += 2) {
    $lookup[$lines[$index]] = $lines[$index + 1]
}

# Next, get a collection of FileInfo objects of *.png files
# If you need to get multiple extensions, remove the -Filter and add -Include '*.png','*.jpg' etc.
$files = Get-ChildItem -Path $searchPath -Filter '*.png' -File -Recurse

foreach ($file in $files) {
    # If the file name can be found as Key in the $lookup Hashtable
    $find = $file.Name
    if ($lookup.ContainsKey($find)) {
        # Rename the file with the replacement name in the Value of the lookup table
        Write-Host "Renaming '$($file.FullName)' -->  $($lookup[$find])"
        $file | Rename-Item -NewName $lookup[$find]
    }
}

以下代码将使用它来构建查找哈希表,以便它可以对输入文件中提到的文件执行操作,并使所有其他文件保持不变。

foreach

修改

如果输入文本文件'C:\ 1 \ Srv \ MapsName.txt'不包含文件名及其扩展名,请将最后的foreach ($file in $files) { # If the file name can be found as Key in the $lookup Hashtable # Look for the file name without extension as it is not given in the 'MapsName.txt' file. $find = [System.IO.Path]::GetFileNameWithoutExtension($file.Name) if ($lookup.ContainsKey($find)) { # Rename the file with the replacement name in the Value of the lookup table # Make sure to add the file's extension if any. $newName = $lookup[$find] + $file.Extension Write-Host "Renaming '$($file.FullName)' --> '$newName'" $file | Rename-Item -NewName $newName } } 循环更改为:

public class CustomViewManager extends SimpleViewManager<TextView> {

public static final String REACT_CLASS = "CustomView";

@Override
public String getName() {
    return REACT_CLASS;
}

@Override
public TextView createViewInstance(ThemedReactContext context) {

    TextView tv = new TextView(context);
    tv.setText("Текст");
    tv.setTextColor(Color.BLACK);
    tv.setWidth(100);
    tv.setHeight(100);
    return tv;

  }
}

希望有帮助