我正在尝试整理一个 Excel 表格,其中列出了我已将快捷方式添加到 Steam 的所有游戏。Steam 允许您为非 Steam 游戏添加快捷方式并充当它们的启动器。用户添加的快捷方式链接位于名为 shortcuts.vdf 的本地数据库中,其中包含与 Steam 拥有的游戏相关的所有信息。我基本上感兴趣的是游戏的名称。
这是文件中的数据在记事本中的样子的片段:
shortcuts 0 appid °rõÄappname Cemu Exe "T:\Cemu\App+Update+DLC\Cemu.exe" StartDir "T:\Cemu\App+Update+DLC\" icon ShortcutPath LaunchOptions IsHidden AllowDesktopConfig AllowOverlay openvr Devkit DevkitGameID LastPlayTime ZùÏ_ tags 0 favorite 1 All Games 1 appid }yâappname Citra Nightly Exe "C:\Users\phili\AppData\Local\Citra\nightly-mingw\citra-qt.exe" StartDir "C:\Users\phili\AppData\Local\Citra\nightly-mingw\" icon ShortcutPath LaunchOptions IsHidden AllowDesktopConfig AllowOverlay openvr Devkit DevkitGameID LastPlayTime ¿
ã_ tags 0 favorite 1 All Games 2 appid {Sü¸appname RollerCoaster Tycoon 2: Triple Thrill Pack Open Exe "C:\Program Files\OpenRCT2\openrct2.exe" StartDir "C:\Program Files\OpenRCT2\" icon ShortcutPath LaunchOptions IsHidden AllowDesktopConfig AllowOverlay openvr Devkit DevkitGameID LastPlayTime !Ý_ tags 0 steam 1 All Games 3 appid Iì»Âappname Art Of Fighting 2 Exe "R:\Amazon Games\Library\Art of Fighting 2\ArtOfFighting2.exe" StartDir "R:\Amazon Games\Library\Art of Fighting 2\" icon ShortcutPath LaunchOptions IsHidden AllowDesktopConfig AllowOverlay openvr Devkit DevkitGameID LastPlayTime tags 0 Shortcuts 1 Amazon 2 All Games 4 appid ñappname Origin Exe "C:\Program Files (x86)\Origin\Origin.exe" StartDir "C:\Program Files (x86)\Origin\" icon ShortcutPath LaunchOptions IsHidden AllowDesktopConfig AllowOverlay openvr Devkit DevkitGameID LastPlayTime ÛÊ*] tags 0 steam 1 All Games 5 appid £á@¤appname A Good Snowman Is Hard To Build Exe "R:\Amazon Games\Library\A Good Snowman Is Hard To Build\Snowman.exe" StartDir "R:\Amazon Games\Library\A Good Snowman Is Hard To Build\" icon ShortcutPath LaunchOptions IsHidden AllowDesktopConfig AllowOverlay openvr Devkit DevkitGameID LastPlayTime tags 0 Shortcuts 1 Amazon 2 All Games 6 appid ¢‡ æappname Adam Wolfe Exe "R:\Amazon Games\Library\Adam Wolfe\AdamWolfe.exe" StartDir "R:\Amazon
我注意到我添加到 Steam 的游戏名称总是在单词appname
and之间Exe
,如下所示:
£á@¤appname A Good Snowman Is Hard To Build Exe
如何从该文件中修改/提取条目,以便我有一个 Excel 表,其中包含每行一个单元格之间appname
的条目?Exe
像这样:
我可能在考虑正则表达式?我没有 Microsoft Office,只有 Libre Office
Here is a very hacky bit of vbscript which essentially breaks down the
shortcuts.vdf
file into seperate lines (based on thenul
character) and then spits out the next line after the one calledAppName
. I did try to just splitfile
usingChr(0)
as the delimiter but it threw an error .Save the following file as
steam.vbs
:Copy the
shortcuts.vcf
file into the same folder as this code and then from a command line type:在任何文本编辑器中打开输出,您将看到您添加的所有游戏快捷方式的列表。
这应该很容易实现,您只需要 PowerShell,不需要 Office 套件。
从路径的语法来看,它们以驱动器号(即 T:) 开头并使用反斜杠 (
\
) 分隔目录,我假设您在 Windows 上。您在 Windows 上,使用它打开 PowerShell:
Win+ R→类型
PowerShell
→ Ctrl+ Shift+Enter然后,一旦你在 PowerShell 中,你可以使用这个正则表达式来获取游戏名称:
(appname)(.*?)(Exe)
Steam .vdf 格式是一种专有格式,因此很难正确解析该格式,尽管您可能可以使用此:GitHub:Steam-GetOnTop,我不使用 Steam,也没有使用该包,所以我不保证它会起作用。
但是由于文件是明文编码的,可以用notepad.exe打开,只需要和之间的内容
appname
,Exe
可以使用蛮力的方式:解释:
第一步:我们获取shortcuts.vdf的内容,将字符串转换为字符数组,过滤掉Control Characters,加入字符串
步骤 2:我们打印我们在第一步中获得的内容并将其传递到管道中;我们从管道中获取内容并选择所有匹配的字符串
(appname)(.*?)(Exe)
,并用于().matches.value
仅获取匹配项,然后我们将其删除appname
并将Exe
其导出到位于您桌面上的名为 gamename.txt 的文件中。但是,如果您坚持要导出到只有一列的 Excel 工作表,请使用以下命令:
该方法与第一种方法几乎相同,但我们将结果转换为
[PSCustomObject]
并将最终结果导出到位于路径“C:\Users\USERNAME\Desktop\gamename.csv”的 .csv 文件,而不是 .txt 文件.进一步说明:
Microsoft Docs:什么是 PowerShell?
Microsoft Docs:关于变量
Microsoft Docs:关于 Foreach
Microsoft Docs:关于 PSCustomObject 你想知道的一切
Microsoft Docs:关于数组你想知道的一切
Microsoft Docs:关于数组
Microsoft Docs:获取内容
Microsoft Docs:选择字符串
Microsoft Docs:Out-File
Microsoft Docs:导出-CSV
输出
正如您所说,它不包含任何个人信息并将文件附加到问题中,我将在下面发布我得到的结果:
Google Drive
我的第一种方法的输出:
游戏名.txt
我的第二种方法的输出:
游戏名.csv
从另一个答案输出 vbs 方法:
蒸汽.txt
代码效率分析
第一种方法是 358 个字符长
第二种方法是396个字符长
vbs 脚本长度为 541 个字符
第一种方法打高尔夫球:
286 个字符长
第二种方法打高尔夫球:
329 个字符长
vbs 不会对结果进行排序,也不会自动输出到文件。
以下是运行时测试:
My first method obviously won, the downvote is unfair, the OP didn't post the critical information, so my code couldn't have solved the OP's problem at first no matter how good I am.
To whoever downvoted my answer, my answer is now edited and your downvote is no longer locked, please cast votes carefully based on objective facts, and please lift the downvote.
To the OP, the other answer is, objectively, not as efficient as my answer. You didn't post the right information at first, so I only have incorrect information to work with. No matter how good I am, it is impossible for me to solve your problem because "Garbage In, Garbage Out", the other answerer just got lucky and happened to be online when you posted the right information while I was away, and you see the script is not as good as mine, please consider accept my answer instead.
Even shorter code:
Method 1 Golfed
Method 2 Golfed
The regex used by these new methods will matches only the game names, but using it increases runtime by 0.3s compared to the first regex.
This is what I see in console using the second method 1 golfed:
I want to answer this question and help the OP simply because I find the question interesting, the thing OP wanted is fun, I am just a beginner in Programming, and I want to improve my skills by answering questions others have posted with programs, that's all...
P.S. I really want to be a hacker...
有一个非官方的vdf python 模块用于解析 Valve 的 vdf 文件。有了它,解析和写出包含所有名称的 csv 非常简单:
要查看所有可用字段(“appname”除外),请将该
print
行替换为import pprint; pprint.pprint(v); break
像其他 python 模块一样,使用
pip install vdf
.