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: 416
Meble kuchenne na zamówienie - na wymiar - Wrocław
Joined: Wed Jun 15, 2016 2:26 am

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: 14444
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 »

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

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 1189 times

jvierra
Posts: 14444
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 »

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

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

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 1164 times

jvierra
Posts: 14444
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 »

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

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

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

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: 14444
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 »

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: 14444
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 »

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