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.
Locked
User avatar
stevens
Posts: 351
Joined: Wed Jun 15, 2016 2:26 am

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

Post by stevens » Mon Aug 05, 2019 9:14 am

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.

User avatar
jvierra
Posts: 13803
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Mon Aug 05, 2019 10:00 am

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: 351
Joined: Wed Jun 15, 2016 2:26 am

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

Post by stevens » Tue Aug 06, 2019 1:32 am

>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!
ExampleExe.png
Example
ExampleExe.png (46.93 KiB) Viewed 421 times

User avatar
jvierra
Posts: 13803
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Tue Aug 06, 2019 4:05 am

When you open a file that way the file name is passed as $args[0] or as the first "Param".

User avatar
stevens
Posts: 351
Joined: Wed Jun 15, 2016 2:26 am

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

Post by stevens » Wed Aug 07, 2019 12:02 am

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?
exeoutputoptions.png
outputoptions
exeoutputoptions.png (79.22 KiB) Viewed 396 times

User avatar
jvierra
Posts: 13803
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Wed Aug 07, 2019 5:49 am

Taking all defaults build this:
Attachments
Test-FormArgs.psf
(9.07 KiB) Downloaded 9 times

User avatar
stevens
Posts: 351
Joined: Wed Jun 15, 2016 2:26 am

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

Post by stevens » Wed Aug 07, 2019 7:42 am

Thanks! Will check right away.

User avatar
stevens
Posts: 351
Joined: Wed Jun 15, 2016 2:26 am

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

Post by stevens » Wed Aug 07, 2019 9:24 am

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!

User avatar
jvierra
Posts: 13803
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Wed Aug 07, 2019 9:40 am

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 *

User avatar
jvierra
Posts: 13803
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Wed Aug 07, 2019 9:45 am

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.

Locked