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

  • 2 weeks later...

Please sign in to view this quote.

Yes! Although Powershell is blocked at my company.... Love blanket IT security policies...

 

anyway, I did get a fully PCM version working. Some of my own discovery, some from other forum posts over there years. In short, it looks inside the current program directory and inside a folder called PCM, then reads the names of all the .txt files in there, and automatically puts them into an inquireList()...

 

From there you can do anything you like based on the selection.

Now, I can make a completely parametric program, and just teach the others how to make the parameter file - by copying one of the existing ones and updating the name and parameter values - And that new one will automatically show up on the list.

There's about 40 masters, and they will have to be redone on a schedule so this effort is definitely worth it if I can make 1-2 work then pass off the project.
 

directoryPath = getActualInspectionDir() + "\PCM"
directoryContent = directoryPath.asFilename.directoryContentsAsFilenames


//Traps in a loop, forces selection or cancel
repeat
	//Dynamic InquireList() Start 
	GaugeIDList = ""
	GaugeIDList = "inquireList(" + qm() + "Choose Machine Number (Last Entered: )" + qm()
	loopBreak=false
	for i = 1 to directoryContent.size
		GaugeIDList = GaugeIDList +","+ qm() + subStr(getParameterNamed(directoryContent,i).asString,(2+len(directoryPath)),(inStr(getParameterNamed(directoryContent,i).asString,".")-1)) +qm()
	next i
		
	GaugeIDList = GaugeIDList + ")"
	GaugeIDSelection = compute(GaugeIDList)
	//DynamicInquireList() End

	if GaugeIDSelection == "" then
		loopBreak = confirm("Cancel?")
	else
		loopBreak = true
	endif
until loopBreak

 

Link to comment
Share on other sites

Add this at the very beginning of your Powershell script.

 

PowerShell -ExecutionPolicy Bypass -File "C:\Path\To\YourScript.ps1"

 

It changes the Execution Policy for that script only to run regardless of your IT settings.

 

Edited
Link to comment
Share on other sites

They block it via a third party app called Sophos... it's brutal. Only way around is to kill Sophos, but then you start getting unhappy messages from corporate IT..

Please sign in to view this quote.

 

Link to comment
Share on other sites

I can do this on my own PC that's not an issue, as I am an Administrator on it. But it can't be done on the CMM PCs where this code will need to run. They are so ridiculously locked down there's no practical way around it. I keep getting promised that some of the policies are intended to relax once they have all the big security stuff done. We'll see what happens.

 

Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In
 Share

×
×
  • Create New...