0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

結合.ps1(3)

Posted at
sample
param (
    [string]$outputFile,
    [string]$inputFiles
)

# 引数のチェック
if (-not $outputFile -or -not $inputFiles) {
    Write-Host "Usage: script.ps1 -outputFile outputfile.txt -inputFiles inputfile1.txt,inputfile2.txt"
    exit 1
}

# 出力ファイルの初期化
if (Test-Path $outputFile) {
    Remove-Item $outputFile
}

# 入力ファイル名をカンマで分割して配列に格納
$inputFileArray = $inputFiles -split ','

# 配列の内容を処理
$firstFile = $true
foreach ($inputFile in $inputFileArray) {
    if (-not (Test-Path $inputFile)) {
        Write-Host "Error: Input file $inputFile not found."
        exit 1
    }

    # 最初のファイルはそのまま全ての行をコピー
    if ($firstFile) {
        Get-Content $inputFile | Add-Content $outputFile
        $firstFile = $false
    } else {
        # 2つ目以降のファイルは1行目をスキップしてコピー
        $lines = Get-Content $inputFile
        if ($lines.Length -gt 1) {
            $lines[1..$lines.Length] | Add-Content $outputFile
        }
    }
}

Write-Host "Files combined successfully into $outputFile."

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?