我如何编写一个脚本,用路径中的所有文件名填充CSV列?

rpppsulh  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(64)

我想使用Get-ChildItem获取路径中的所有项目,然后使用正则表达式,匹配文件名中“@”(andrea.figuera@gmail.com.pst)之前的所有内容,并将第一个字母和句点之后的字母大写。(Andrea.Figuera变成Andrea.Figuera)。然后,我想删除'.',并在CSV中,我想填充两列:“FileName”和“*Name *”,这样我就可以匹配它们并使用相应的“Name”值重命名文件。
Powershell能做到这一点吗?谢谢你,谢谢!
这是我目前拥有的脚本written by member Santiago Squarzon。它可以工作,但我还不想实现CSV编辑,而是想一次理解一个过程,但我正在努力理解相关命令。

Cd "C:\Users\Nick\Downloads\MATIC PST"

$map = Import-Csv -Path 'namesemails.csv' |
    # group the objects by the `OldName` column
    Group-Object OldName -AsHashTable

# get all `.pst` files
Get-ChildItem -Path '*.pst' |
    # filter only those files existing in our map
    Where-Object { $map.ContainsKey($_.Name) } |
    # here we assume the file exist so, we get the value that maps with
    # this file name and from there we expand the `.NewName`
    Rename-Item -NewName { $map[$_.Name].NewName }

我将这条语句添加到脚本的开头,以匹配文件名中的保管人名称,但我不确定这样做是否正确。

$regexpattern = '([^@.]+)\.([^@]+)(?=@)'

    #Get all .pst files and extract custodian names from filenames, if available
    $files = Get-ChildItem -Path '*.pst' | Group-Object Name -AsHashTable
    foreach ($file in $files) 
    {
    $file | Select-String $regexpattern -AllMatches
    }
vwhgwdsa

vwhgwdsa1#

以下工作

$path = 'c:\temp\*.pst'
$regexPattern = '(^[^\.]+\.[^@]+)(@.*)'
$files = Get-ChildItem -Path $path

foreach ($file in $files) 
{
    if($file.Name -match $regexPattern)
    {
       $name = $Matches[1]
       $name = $name.Substring(0,1).ToUpper() + $name.Substring(1)
       $period = $name.IndexOf('.')
       $name = $name.Substring(0,$period) + $name.Substring($period + 1,1).ToUpper() + $name.Substring($period + 2)

       Write-Host $name $Matches[0]
    }
}

相关问题