从 CSV 中的多行返回中提取列数据
我有一个包含 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用以下语句,该语句可以理解为:
$csv
的对象 where Property 的值Name< /code> OR 属性
Job
的值等于$JobChoice
。然后,您可以在该语句后加上.Directory
来枚举Directory
属性的所有值。请参阅 成员枚举了解详细信息。
回顾一下您的代码,您当前使用的语句也可以工作,但是
-in
或-contains
会更有效。您的代码的唯一问题是使用
($_.Directory)
,它应该只是:You can use the following statement, which can be read as:
An object of
$csv
where the value of the PropertyName
OR the value of the propertyJob
are equal to$JobChoice
. Then you can follow that statement with.Directory
to enumerate all values of theDirectory
property.See Member Enumerations for details.
Looking back at your code, the statement you're currently using would also work too, however
-in
or-contains
would be more efficient.The only issue with your code was the use of
($_.Directory)
, it should have been just:您只想将目录作为输出吗?将 Select-Object 替换为 ForEach-Object 并使用 {} 代替 ()
Are you just wanting the Directory as the output? Replace Select-Object with ForEach-Object and use {} instead of ()
这段代码是我需要的解决方案。
这允许我从 CSV 文件中提取管道目录。
下面是工作脚本。
CSV
变量Robocopy
带有工作代码的
This Code Was the solution i needed.
This allows me to extract the pipeline directory from the CSV File.
Below is the working script.
CSV
Variables
Robocopy With the Working Code