Send a file to Powershell Studio exe (form): show name and extension

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 4 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
stevens
Posts: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

Send a file to Powershell Studio exe (form): show name and extension

Post by stevens »

Hi,

Had posted viewtopic.php?f=21&t=14044 regarding opening a Windows file, f.e. examplefile.XMind via a Powershell Studio form (exe). Result should then be that the form opens and shows
ExampleFile.XMind.

There was an answer
cmd /c assoc /?
cmd /c ftype /?
but honestly didn't have a clue what to do with that, so googled a bit, found nothing and left it. The post is now locked. Therefore a second attempt.

Please advise the steps to take:
1.I create a Powershell form
2.I save it as an exe
3.I open the ExampleFile.XMind with that exe

What should be in that exe/form that shows me the output
ExampleFile.XMind so I can capture it in a variable and work with it?
How do I give the ExampleFile.Xmind to the exe then in the code?
Note: associations of the file .Xmind to Xmind program is none of my worries right now.

Please advise.
J.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Send a file to Powershell Studio exe (form): show name and extension

Post by jvierra »

No idea what you are trying to ask.

To get a file into a variable use:

$myvar = Get-Content ExampleFile.XMind
User avatar
stevens
Posts: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

Re: Send a file to Powershell Studio exe (form): show name and extension

Post by stevens »

>No idea what you are trying to ask.
Sorry bout that :-) Let me try again. Hopefully the screenshot of what I'm trying to do clarifies it.
If not, do let me know!
Example
Example
ExampleExe.png (46.93 KiB) Viewed 3215 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Send a file to Powershell Studio exe (form): show name and extension

Post by jvierra »

When you open a file that way the file name is passed as $args[0] or as the first "Param".
User avatar
stevens
Posts: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

Re: Send a file to Powershell Studio exe (form): show name and extension

Post by stevens »

Thanks, tried this:

$form1_Load={
#TODO: Initialize Form Controls here
$labelFileYouOpenedWithExe.Text = $args[0]
}
But the label in the form doesn't show the filename when opened the ExampleFile.XMind with the exe (generated from PSStudio).
Should I change the output options?
outputoptions
outputoptions
exeoutputoptions.png (79.22 KiB) Viewed 3190 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Send a file to Powershell Studio exe (form): show name and extension

Post by jvierra »

Taking all defaults build this:
Attachments
Test-FormArgs.psf
(9.07 KiB) Downloaded 107 times
User avatar
stevens
Posts: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

Re: Send a file to Powershell Studio exe (form): show name and extension

Post by stevens »

Thanks! Will check right away.
User avatar
stevens
Posts: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

Re: Send a file to Powershell Studio exe (form): show name and extension

Post by stevens »

Works great, thanks!!!
However, I cannot detect the extension correctly, though it is there.

$FullPathOfFile = [System.IO.Path]::GetFullPath($commandarg)
$FileName = $(split-path $FullPathOfFile -Leaf)
$Global:Extension = $([System.IO.Path]::GetExtension($FileName).tostring()).trim()
$textboxFileExtension.Text = $Extension


if ($Extension -eq '.xmind') { $textboxResult.Text = 'XMind is the program you could open .XMind with'}
else {$textboxResult.Text = 'no program found'}

Please advise on this (it just shows "no program found"}.
Thanks!
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Send a file to Powershell Studio exe (form): show name and extension

Post by jvierra »

This is the correct way to do this in PowerShell.

Code: Select all

# get file information
$file = [io.fileinfo]$commandarg
$file.FullName
$file.Name
$file.Basename
$file.DirectoryName
$file.Extension

#or
([io.fileinfo]$commandarg).Extension

# get directory details
$file.Directory.FullName

# get all properties:
$file | select *
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Send a file to Powershell Studio exe (form): show name and extension

Post by jvierra »

Code: Select all

if (([io.fileinfo]$commandarg).Extension -eq '.xmind') {
    $textboxResult.Text = 'XMind is the program you could open .XMind with'
} else {
    $textboxResult.Text = 'no program found'
}
you have just demonstrated why you should almost never use interim variables. They make code harder to understand, maintain and debug. It is a no-no in all programming languages.
This topic is 4 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked