Page 1 of 1
Send a file to Powershell Studio exe (form): show name and extension
Posted: Mon Aug 05, 2019 9:14 am
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.
Re: Send a file to Powershell Studio exe (form): show name and extension
Posted: Mon Aug 05, 2019 10:00 am
by jvierra
No idea what you are trying to ask.
To get a file into a variable use:
$myvar = Get-Content ExampleFile.XMind
Re: Send a file to Powershell Studio exe (form): show name and extension
Posted: Tue Aug 06, 2019 1:32 am
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
- ExampleExe.png (46.93 KiB) Viewed 3216 times
Re: Send a file to Powershell Studio exe (form): show name and extension
Posted: Tue Aug 06, 2019 4:05 am
by jvierra
When you open a file that way the file name is passed as $args[0] or as the first "Param".
Re: Send a file to Powershell Studio exe (form): show name and extension
Posted: Wed Aug 07, 2019 12:02 am
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
- exeoutputoptions.png (79.22 KiB) Viewed 3191 times
Re: Send a file to Powershell Studio exe (form): show name and extension
Posted: Wed Aug 07, 2019 5:49 am
by jvierra
Taking all defaults build this:
Re: Send a file to Powershell Studio exe (form): show name and extension
Posted: Wed Aug 07, 2019 7:42 am
by stevens
Thanks! Will check right away.
Re: Send a file to Powershell Studio exe (form): show name and extension
Posted: Wed Aug 07, 2019 9:24 am
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!
Re: Send a file to Powershell Studio exe (form): show name and extension
Posted: Wed Aug 07, 2019 9:40 am
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 *
Re: Send a file to Powershell Studio exe (form): show name and extension
Posted: Wed Aug 07, 2019 9:45 am
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.