使用 Powershell 创建自定义 CSV 文件列表

Make a custom CSV List of files with Powershell

我正在尝试制作一个包含文件列表的 CSV 文件,我会用它来制作另一个程序的 .cpk 文件。

CSV 中的列表应仅包含输出文件夹中的文件:

    <root folder>
     makecsv.ps1
        Output (the files)

CSV的CSV格式很简单:

    "<full path to file>", "<relative path>", <line number, starting with 0>, Uncompress
    

输出示例为:

    "F:/Aemulus/Output/battle/cutin/bct_p_bc0004.dds", "/battle/cutin/bct_p_bc0004.dds", 0, Uncompress
    "F:/Aemulus/Output/sound/motionse/em0000.dat", "/sound/motionse/em0000.dat", 1, Uncompress
    "F:/Aemulus/Output/script/field/fscr0150_002_100.bf", "/script/field/fscr0150_002_100.bf", 2, Uncompress
    

我会为此编写一个程序,但有一个私人原因说明为什么我需要为此使用 Powershell 脚本,虽然我 可以 输出简单的 csv 文件,但我完全不知道我应该如何处理 this.

这应该可以做到。但我强烈建议您查看 documentation 中的命令及其参数,以便了解这段代码的工作原理。另外,鼓励您提出尽可能多的问题,因为您需要了解它。

# the "script:" scope prefex is required, so we can change the value later
$script:lineNumber = 0

# get the full path of the folder, so we can use it to build the relative path later
$rootPath = Resolve-Path -Path "Output"

# get all files in folder and subfolers
# then build the desired output using "calculated properties"
# and finally export to csv file
Get-ChildItem -Path $rootPath -File -Recurse | Select-Object -Property @{
    Name = "FullPath"
    Expression = "FullName"
}, @{
    Name = "RelativePath"
    Expression = { $_.FullName.Replace($rootPath, "")}
}, @{
    Name = "LineNumber"
    Expression = {($script:lineNumber++)}
}, @{
    Name = "Uncompress"
    Expression = {"Uncompress"}
} | Export-Csv -Path "export.csv" -NoTypeInformation -Delimiter ","