Skip to content

Instantly share code, notes, and snippets.

@mark05e
Created September 23, 2023 22:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mark05e/e380bbe20049b088b39a14f1baf58b42 to your computer and use it in GitHub Desktop.
Save mark05e/e380bbe20049b088b39a14f1baf58b42 to your computer and use it in GitHub Desktop.
Scans text files in a directory, creates a CSV file with customizable file extensions, and formats the contents in <filename>.<extension>,"<content>" format.
function ConvertTextFilesToCSV {
param (
[string]$outputDirectory = ".",
[string]$customFileExtension = ".wav"
)
# Get the current directory name
$currentDirectory = (Get-Item -Path $outputDirectory).Name
# Define the CSV file name
$csvFileName = Join-Path -Path $outputDirectory -ChildPath "$currentDirectory.csv"
# Initialize an empty array to store the file contents
$fileContents = @()
# Loop through all text files in the current directory
Get-ChildItem -Path $outputDirectory -Filter *.text | ForEach-Object {
$filenameWithoutExtension = [System.IO.Path]::GetFileNameWithoutExtension($_.Name)
$content = Get-Content $_.FullName
$fileContents += "$filenameWithoutExtension$customFileExtension,""" + $content + """"
}
# Write the file contents to the CSV file
$fileContents | Out-File -FilePath $csvFileName -Encoding utf8
Write-Host "CSV file created: $csvFileName"
}
# Call the function with optional parameters (output directory and custom file extension)
ConvertTextFilesToCSV -outputDirectory "C:\Your\Output\Directory" -customFileExtension ".mp3"
# ChatGPT Prompt:
# ---------------
# The task is to create a PowerShell script that performs the following actions:
#
# - Scans all text files in the current directory.
# - Retrieves the current directory name.
# - Provides the option to specify a custom file extension (default is ".wav").
# - Generates a CSV file named after the current directory.
# - Copies the contents of each text file into the CSV file in the format "<filename>.<customFileExtension>,"<file content>."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment