我的这个脚本无法正确应用于子文件夹。它应该在文件夹“Txt_ENG”内创建所有与子文件夹相关的文件。
示例:
我在:
G:\Games\Files here
文件 abc.csv 位于:
G:\Games\Files here\current\csv
需要在以下位置创建文件 abc.txt:
G:\Games\Files here\Txt_ENG\current\csv
它在哪里为我创造了它:
G:\Games\Files here\Txt_ENG\Games\Files here\current\csv
代码:
param(
$FileName = '*.csv', # File-name filter
$SourceDir = $PWD, # Input-files dir.
$OutDir = $PWD, # Output-files dir.
$ColumnIndex = 3, # 1-based index of the col. of interest
$OutFileBaseNameSuffix = "_column$ColumnIndex"
)
# Define the destination folder
$destinationFolder = Join-Path -Path $OutDir -ChildPath "Txt_ENG"
# Create the destination folder if it doesn't exist already
if (-not (Test-Path -Path $destinationFolder)) {
New-Item -Path $destinationFolder -ItemType Directory | Out-Null
}
foreach ($csvFile in Get-ChildItem -LiteralPath $SourceDir -Recurse -Filter $FileName -File) {
# Import all rows from the current file
$rows = Import-Csv $csvFile.FullName
# Determine the name of the column of interest with the specified index.
# Subtract -1 from $ColumnIndex since array indices start from *0*.
$colName = ($rows[0].psobject.Properties.Name)[$ColumnIndex-1]
# Construct the destination path while maintaining the subfolder structure
$relativePath = $csvFile.FullName.Substring($SourceDir.Length + 1)
$destinationPath = Join-Path -Path $destinationFolder -ChildPath $relativePath
# Create the destination folder if it doesn't exist already
$destinationDirectory = Split-Path -Path $destinationPath -Parent
if (-not (Test-Path -Path $destinationDirectory)) {
New-Item -Path $destinationDirectory -ItemType Directory | Out-Null
}
# Determine the output file path and name
$outFile = Join-Path $destinationDirectory ('{0}.txt' -f $csvFile.BaseName)
# Write the entire file, as UTF-8 without BOM
$null =
Set-Content -Path $outFile -Value (
$colName + "`n" + ($rows.$colName -join "`n") + "`n"
) -Encoding UTF8
}
$SourceDir.Length
是错误的,$PWD
是PathInfo
类型的对象而不是字符串。 $SourceDir.Length
将是 1
,所以:
# using C:\ instead of G:\ to avoid error
$destination = 'C:\Games\Files here\Txt_ENG'
$filePath = 'C:\Games\Files here\current\csv\abc.csv'
Join-Path $destination $filePath.Substring(1 + 1)
# Outputs the unexpected path:
# C:\Games\Files here\Txt_ENG\Games\Files here\current\csv\abc.csv
您想要做的是
$SourceDir.Path.Length + 1
或 $SourceDir.ToString().Length + 1
:
$relativePath = $csvFile.FullName.Substring($SourceDir.Path.Length + 1)