Jump to content

Read all file names in a directory?


---
 Share

Recommended Posts

I want to make a parametric program, that I can edit entirely outside of the program...

Is there a function that simply will give me all the file names in a directory? Basically, can I read these 3 files and get them in a list?

or is this going to be something that needs done with getParameterNamed() and a loop?

image.thumb.png.b5eb508114373b7070065c4136114a60.png

Edited
Link to comment
Share on other sites

Im sure these is, I will search when I have some time.. 

 

Question : if you're making the parametric program, do you just want to check to make sure these 3 files exist ?

  

fileExists()

Is it important for the program to know if there are more files or you just want to check to make sure ?

Edited
Link to comment
Share on other sites

I am not exactly sure of your needs, but this is a Powershell script that will read the contents of each text file in the folder and then append it into a single text file. Is that what you are trying to do?

 

# Define the directory containing the text files
$sourceDirectory = "C:\Path\To\Your\TextFiles"
# Define the output file where all contents will be combined
$outputFile = "C:\Path\To\Your\CombinedOutput.txt"

# Check if the output file already exists and remove it to avoid appending to old data
if (Test-Path $outputFile) {
    Remove-Item $outputFile
}

# Get all .txt files in the specified directory
$textFiles = Get-ChildItem -Path $sourceDirectory -Filter "*.txt"

# Loop through each text file and append its content to the output file
foreach ($file in $textFiles) {
    # Read the content of the text file
    $content = Get-Content -Path $file.FullName
    
    # Append the content to the output file
    Add-Content -Path $outputFile -Value $content
    
    # Optionally, add a newline for separation between files
    Add-Content -Path $outputFile -Value "`n"
}

Write-Host "All text file contents have been combined into $outputFile"

 

Link to comment
Share on other sites

 Share

×
×
  • Create New...