[
    {
        "role": "user",
        "content": "Craft%20a%20powershell%20script%20that%20has%20the%20following%20variables%20at%20the%20outset%3A%20INPUT%20Folder%2C%20OUTPUT%20Folder%2C%20and%20LOGFILE%20Location.%20This%20script%20will%20employ%20the%20PDFtk%20command%20line%20tool%20to%20merge%20all%20PDF%20files%20in%20the%20INPUT%20Folder%20and%20Place%20the%20resulting%20Merged%20File%20into%20the%20OUTPUT%20Folder.%20The%20Date%20of%20the%20Script%20being%20Run%2C%20Along%20with%20the%20Name%20of%20the%20Output%20Merge%20File%20will%20be%20placed%20in%20a%20Log.txt%20file.%20Appended%20to%20the%20end.%20A%20Hash%20of%20the%20file%20will%20be%20included%20as%20well.%20The%20log.txt%20file%20will%20be%20stored%20in%20the%20LOGFILE%20Location%20folder.%20The%20Merge%20Filename%20will%20use%20the%20name%20of%20the%20first%20file%20in%20the%20INPUT%20Folder%20and%20append%20to%20the%20end%20-MERGE%20prior%20to%20the%20file%20extension."
    },
    {
        "role": "assistant",
        "content": "Here\u2019s a PowerShell script that does what you asked. You can tweak paths and tool location as needed. Be sure that `pdftk.exe` is installed and in your PATH (or give full path to it).\n\n```powershell\n# PowerShell script to merge PDFs using PDFtk,\n# log the operation with date, output filename, and hash.\n\nparam (\n    [string]$InputFolder    = \"C:\\PDF\\Input\",    # Path to folder containing PDFs to merge\n    [string]$OutputFolder   = \"C:\\PDF\\Output\",   # Path to folder where merged PDF will go\n    [string]$LogFileFolder  = \"C:\\PDF\\Logs\"      # Path to folder to store log file\n)\n\n# Make sure folders exist\nif (-not (Test-Path -Path $InputFolder)) {\n    Write-Error \"Input folder '$InputFolder' does not exist.\"\n    exit 1\n}\nif (-not (Test-Path -Path $OutputFolder)) {\n    New-Item -ItemType Directory -Path $OutputFolder -Force | Out-Null\n}\nif (-not (Test-Path -Path $LogFileFolder)) {\n    New-Item -ItemType Directory -Path $LogFileFolder -Force | Out-Null\n}\n\n# Gather PDF files\n$pdfFiles = Get-ChildItem -Path $InputFolder -Filter *.pdf | Sort-Object Name\nif ($pdfFiles.Count -eq 0) {\n    Write-Error \"No PDF files found in '$InputFolder'.\"\n    exit 1\n}\n\n# Determine output filename: use first file's name + \"-MERGE.pdf\"\n$firstFile = $pdfFiles[0].BaseName\n$timestamp = Get-Date -Format \"yyyyMMdd_HHmmss\"\n# optional: include timestamp\u2014or omit if not desired\n$outputFileName = \"{0}-MERGE.pdf\" -f $firstFile\n$outputFilePath = Join-Path $OutputFolder $outputFileName\n\n# Build the PDFtk arguments: list all input files\n$pdfPaths = $pdfFiles | ForEach-Object { $_.FullName }\n# Using PDFtk to merge: pdftk file1.pdf file2.pdf ... cat output merged.pdf\n# If filenames have spaces, wrap in quotes\n$quotedPdfPaths = $pdfPaths | ForEach-Object { '\"' + $_ + '\"' }\n$pdftkArgs = $quotedPdfPaths -join \" \"\n$pdftkCommand = \"pdftk $pdftkArgs cat output `\"$outputFilePath`\"\"\n\n# Run the merge\ntry {\n    & cmd /c $pdftkCommand\n} catch {\n    Write-Error \"Failed to run PDFtk merge: $_\"\n    exit 1\n}\n\n# Compute hash of merged file (SHA256)\n# You can change algorithm if desired (MD5, SHA1, etc.)\ntry {\n    $hashInfo = Get-FileHash -Path $outputFilePath -Algorithm SHA256\n} catch {\n    Write-Error \"Failed to compute hash of '$outputFilePath': $_\"\n    exit 1\n}\n\n# Prepare log file\n$logFilePath = Join-Path $LogFileFolder \"Log.txt\"\n$runDate = Get-Date -Format \"yyyy-MM-dd HH:mm:ss\"\n$logEntry = @\"\nDate:        $runDate\nMergedFile:  $outputFileName\nSHA256Hash:  $($hashInfo.Hash)\n--------END--------\n\"@\n\n# Append to log\ntry {\n    Add-Content -Path $logFilePath -Value $logEntry\n} catch {\n    Write-Error \"Failed to write to log file '$logFilePath': $_\"\n    exit 1\n}\n\nWrite-Output \"Merge completed successfully.\"\nWrite-Output \"Merged file: $outputFilePath\"\nWrite-Output \"Log updated: $logFilePath\"\n```\n\n---\n\n### How it works:\n\n- Variables `InputFolder`, `OutputFolder`, `LogFileFolder` are at top; you can also pass them as parameters.\n- It gathers all `*.pdf` files in `InputFolder`, sorted by name.\n- Takes first file\u2019s base name (filename without extension) to build the merged output name, appending `-MERGE.pdf`.\n- Runs `pdftk` to merge all PDFs (`cat output` command).\n- Computes SHA-256 hash of merged file using `Get-FileHash`.\n- Logs date/time, output filename, and hash to `Log.txt` in log folder. Appends new entry each time. Separates entries with a marker (e.g. `--------END--------`) to distinguish entries.\n- Ensures required folders exist (creating output or logs folder if missing), and checks input folder has at least one PDF.\n\n---\n\n### Possible enhancements / tweaks:\n\n- Include full path or relative paths in log.\n- Use other hash algorithms (e.g. MD5 or SHA1) if preferred.\n- Include number of input files merged, or list the input filenames in log.\n- Incorporate timestamp into output filename, e.g. `$firstFile\u2010MERGE_20260419_123456.pdf`, if you need unique per-run names.\n- Error handling could be made more robust (e.g. checking if output file already exists, handling file locking).\n\nIf you like, I can customize the script (e.g. using timestamped output names, different hash type, or more verbose logging)."
    }
]