从 CSV 中的多行返回中提取列数据

发布于 2025-01-09 13:01:35 字数 1189 浏览 4 评论 0原文

我有一个包含 3 列的 CSV 文件:名称、工作和目录。

我试图让用户输入名称或工作,它将返回第 3 列(目录)的一行或多行输出

    $Profile = $PSScriptRoot + "\Profile.csv"
    $Data = Import-Csv -Path $Profile
    $JobChoice = Read-Host "Enter a Backup option"
    
$Results = $Data |  Where-Object {( $_.Name, $_.Job ) -eq [string]$JobChoice} 
     
$Results | Select-Object -ExpandProperty ($_.Directory)

如果我在提示时输入上面的示例:“满意” 它只会显示一次返回。但它显示了所有三列,我只需要它返回当前的行目录。

还, 如果我输入“SaveGame”作为读取主机输入。它将返回具有该作业的所有游戏,但仅返回目录。 我想保留此功能,因为它根据我选择的名称或工作接受一个或多个游戏

我什至尝试了以下每种组合:

Select-Object -ExpandProperty ($_.Directory)

我的目标是将数据添加到 CSV,然后解析它并将它们发送到xcopy 批量复制返回的目录。

有什么帮助吗?

CSV:

Name,Job,Directory
Satisfactory,Savegame,C:\Users\jrkid\AppData\Local\FactoryGame\Saved\SaveGames
TheLongDark,Savegame,C:\Users\jrkid\AppData\Local\Hinterland
TheInfected,Savegame,C:\Users\jrkid\AppData\Local\TheInfected
WrenchGame,Savegame,C:\Users\jrkid\AppData\Local\WrenchGame
Valheim,Savegame,C:\Users\jrkid\AppData\LocalLow\IronGate
RisingWorld,Savegame,C:\Users\jrkid\AppData\LocalLow\JIW-Games
7DTD,Savegame,C:\Users\jrkid\AppData\Roaming\7DaysToDie

I have a CSV file with 3 columns: Name, Job, And Directory.

I'm trying to Have the user type in Either a Name, or a Job, and it will return one or multiple lines output from column 3 (Directories)

    $Profile = $PSScriptRoot + "\Profile.csv"
    $Data = Import-Csv -Path $Profile
    $JobChoice = Read-Host "Enter a Backup option"
    
$Results = $Data |  Where-Object {( $_.Name, $_.Job ) -eq [string]$JobChoice} 
     
$Results | Select-Object -ExpandProperty ($_.Directory)

The above example if I Type when prompted: "satisfactory"
it will only show one return. but it shows all three columns i just need it to return the current lines directory.

Also,
If I enter "SaveGame" as the Read-Host input. it will return all games with that job, but only the directories.
I Would like to keep this feature as it accepts one game or multiple based on if i choose Name, or Job

I have even tried every combination of:

Select-Object -ExpandProperty ($_.Directory)

My goal of this is to add the data to the CSV then parse it and send them to xcopy to batch copy the returned directories.

Any help?

The CSV:

Name,Job,Directory
Satisfactory,Savegame,C:\Users\jrkid\AppData\Local\FactoryGame\Saved\SaveGames
TheLongDark,Savegame,C:\Users\jrkid\AppData\Local\Hinterland
TheInfected,Savegame,C:\Users\jrkid\AppData\Local\TheInfected
WrenchGame,Savegame,C:\Users\jrkid\AppData\Local\WrenchGame
Valheim,Savegame,C:\Users\jrkid\AppData\LocalLow\IronGate
RisingWorld,Savegame,C:\Users\jrkid\AppData\LocalLow\JIW-Games
7DTD,Savegame,C:\Users\jrkid\AppData\Roaming\7DaysToDie

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

凡间太子 2025-01-16 13:01:35

您可以使用以下语句,该语句可以理解为:

$csv 的对象 where Property 的值 Name< /code> OR 属性 Job 的值等于 $JobChoice。然后,您可以在该语句后加上 .Directory 来枚举 Directory 属性的所有值。

请参阅 成员枚举了解详细信息。

$csv = Import-Csv path/to/csv.csv
$JobChoice = Read-Host "Enter a Backup option"
$csv.Where({ $JobChoice -eq $_.Name -or $JobChoice -eq $_.Job }).Directory

回顾一下您的代码,您当前使用的语句也可以工作,但是 -in-contains 会更有效。

$csv.Where({ ($_.Name, $_.Job) -contains $JobChoice }).Directory

您的代码的唯一问题是使用 ($_.Directory) ,它应该只是:

| Select-Object -ExpandProperty Directory

You can use the following statement, which can be read as:

An object of $csv where the value of the Property Name OR the value of the property Job are equal to $JobChoice. Then you can follow that statement with .Directory to enumerate all values of the Directory property.

See Member Enumerations for details.

$csv = Import-Csv path/to/csv.csv
$JobChoice = Read-Host "Enter a Backup option"
$csv.Where({ $JobChoice -eq $_.Name -or $JobChoice -eq $_.Job }).Directory

Looking back at your code, the statement you're currently using would also work too, however -in or -contains would be more efficient.

$csv.Where({ ($_.Name, $_.Job) -contains $JobChoice }).Directory

The only issue with your code was the use of ($_.Directory), it should have been just:

| Select-Object -ExpandProperty Directory
谷夏 2025-01-16 13:01:35

您只想将目录作为输出吗?将 Select-Object 替换为 ForEach-Object 并使用 {} 代替 ()

$Results | ForEach-Object { $_.Directory}

Are you just wanting the Directory as the output? Replace Select-Object with ForEach-Object and use {} instead of ()

$Results | ForEach-Object { $_.Directory}
筱果果 2025-01-16 13:01:35

这段代码是我需要的解决方案。

$csv.Where({ ($_.Name, $_.Job) -contains $JobChoice }).Directory

这允许我从 CSV 文件中提取管道目录。
下面是工作脚本。

CSV

Name           Type     DirType    Directory                    
7DTD           Game      User     Roaming\7DaysToDie                    
ASXP        Application  User     Roaming\HiFi                  
BattleNet   Application  User     Local\Battle.net                  
CereProc    Application  User     Local\CereProc

                

变量Robocopy

$global:Profile = $PSScriptRoot + "\Profile.csv"
$global:Data = Import-Csv -Path $global:Profile
$global:BackupPath = "E:\PowerShell Scripts\Automate Backup\MainFrameV2\Test1"
$global:LogFile = $PSScriptRoot + "\Log.txt"
$global:AppData = $env:USERPROFILE+"\AppData"
$global:ProgramFiles = $env:ProgramFiles
$global:ProgramFiles86 = $env:ProgramFiles+" (x86)"
$global:ProgramData = $env:ProgramData
$global:DateFormat = "(%m-%d-%Y)"
$global:cWaitTime = 2 # Retry copy wait time

$global:TypeChoice = Read-Host

带有工作代码的

$global:Data | Where({ ($global:TypeChoice -eq $_.Type -or $global:TypeChoice -eq $_.Name) -and $_.DirType -eq "USER"}) | ForEach  {                   
                            robocopy ($global:AppData+"\"+$_.Directory) ($global:BackupPath +"\"+$global:Time+"\"+($_.Type)+"\"+ ($_.Name))  /S /mt:8 /tee /eta /log+:$global:LogFile /xf "*.ccc" /w:$global:cWaitTime
                            trap { exit 8 }

This Code Was the solution i needed.

$csv.Where({ ($_.Name, $_.Job) -contains $JobChoice }).Directory

This allows me to extract the pipeline directory from the CSV File.
Below is the working script.

CSV

Name           Type     DirType    Directory                    
7DTD           Game      User     Roaming\7DaysToDie                    
ASXP        Application  User     Roaming\HiFi                  
BattleNet   Application  User     Local\Battle.net                  
CereProc    Application  User     Local\CereProc

                

Variables

$global:Profile = $PSScriptRoot + "\Profile.csv"
$global:Data = Import-Csv -Path $global:Profile
$global:BackupPath = "E:\PowerShell Scripts\Automate Backup\MainFrameV2\Test1"
$global:LogFile = $PSScriptRoot + "\Log.txt"
$global:AppData = $env:USERPROFILE+"\AppData"
$global:ProgramFiles = $env:ProgramFiles
$global:ProgramFiles86 = $env:ProgramFiles+" (x86)"
$global:ProgramData = $env:ProgramData
$global:DateFormat = "(%m-%d-%Y)"
$global:cWaitTime = 2 # Retry copy wait time

$global:TypeChoice = Read-Host

Robocopy With the Working Code

$global:Data | Where({ ($global:TypeChoice -eq $_.Type -or $global:TypeChoice -eq $_.Name) -and $_.DirType -eq "USER"}) | ForEach  {                   
                            robocopy ($global:AppData+"\"+$_.Directory) ($global:BackupPath +"\"+$global:Time+"\"+($_.Type)+"\"+ ($_.Name))  /S /mt:8 /tee /eta /log+:$global:LogFile /xf "*.ccc" /w:$global:cWaitTime
                            trap { exit 8 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文