none
Powershell display and OPEN files RRS feed

  • Question

  • All,

    found pre-written script that will open a file explorer and allow one to see the files in a default directory.  It was built using Sapien Primal forms.  My question is, I know I can open a standard windows explorer with a simple ii .  

    However I would like to be able to "lock down" the window so that the user would only see files and folders with no way to browse to other folders or drive letters, etc.  The below code opens a browser but I cannot figure out how to open the files when the user double clicks.  Any help is greatly appreciated.

    function Refresh-ListView {
        param ([string]$path)
        $script:previousItem = (Get-Item $path).PSParentPath
        $listView1.Items.clear()
        $counter=0
        Get-ChildItem $path | % {
            $listView1.Items.Add($_.name)
            if ($_.PSIsContainer) {
                $listView1.Items[$counter].ImageIndex = 0
                $listView1.Items[$counter].Tag = "Folder"
            } else {
                $listView1.Items[$counter].ImageIndex = 1
                $listView1.Items[$counter].Tag = "File"
            } 
                $listView1.Items[$counter].Name = $_.FullName
                $counter += 1
        }
        $form1.Refresh()
    }
     
    #Generated Form Function
    function GenerateForm {
    ########################################################################
    # Code Generated By: SAPIEN Technologies PrimalForms (Community Edition) v1.0.7.0
    # Generated By: Ravikanth_Chaganti
    ########################################################################
     
    #region Import the Assemblies
    [reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
    [reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
    #endregion
     
    #region Generated Form Objects
    $form1 = New-Object System.Windows.Forms.Form
    $button2 = New-Object System.Windows.Forms.Button
    $button1 = New-Object System.Windows.Forms.Button
    $listView1 = New-Object System.Windows.Forms.ListView
    $InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
    #endregion Generated Form Objects
     
    #region ImageList for nodes
    $global:imageList = new-Object System.Windows.Forms.ImageList
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Width = 32
    $System_Drawing_Size.Height = 32
    $global:imageList.ImageSize = $System_Drawing_Size
    $image1 = [System.Drawing.Image]::FromFile("H:\Powershell_scripts\folder.ico")
    $image2 = [System.Drawing.Image]::FromFile("H:\Powershell_scripts\file.ico")
     
    $global:imageList.Images.Add("folder",$image1)
    $global:imageList.Images.Add("file",$image2)
    #endregion
     
    #----------------------------------------------
    #Generated Event Script Blocks
    #----------------------------------------------
    #Provide Custom Code for events specified in PrimalForms.
    $button1_OnClick= 
    {
        if ($script:previousItem) {
            Refresh-ListView $script:previousItem
        } else {
            [System.Windows.Forms.MessageBox]::Show("Nothing to go back")
        }
    }
     
    $doubleClick= 
    {
        if ($listView1.SelectedItems[0].tag -eq "File") {
            [System.Windows.Forms.MessageBox]::Show("I dont know what to do here")
        } else {
            Refresh-ListView $listView1.SelectedItems[0].Name
        }
    }
     
    $button2_OnClick= 
    {
        $form1.Close()
    }
     
    $OnLoadForm_StateCorrection=
    {
        $form1.WindowState = $InitialFormWindowState
        $listView1.LargeImageList = $imageList
        Refresh-ListView "h:\"
    }
     
    #----------------------------------------------
    #region Generated Form Code
    $form1.Text = "PoSh Browser"
    $form1.Name = "form1"
    $form1.DataBindings.DefaultDataSourceUpdateMode = 0
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Width = 689
    $System_Drawing_Size.Height = 499
    $form1.ClientSize = $System_Drawing_Size
     
    $button2.TabIndex = 2
    $button2.Name = "button2"
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Width = 75
    $System_Drawing_Size.Height = 23
    $button2.Size = $System_Drawing_Size
    $button2.UseVisualStyleBackColor = $True
     
    $button2.Text = "Close"
     
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 602
    $System_Drawing_Point.Y = 459
    $button2.Location = $System_Drawing_Point
    $button2.DataBindings.DefaultDataSourceUpdateMode = 0
    $button2.add_Click($button2_OnClick)
     
    $form1.Controls.Add($button2)
     
    $button1.TabIndex = 1
    $button1.Name = "button1"
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Width = 75
    $System_Drawing_Size.Height = 23
    $button1.Size = $System_Drawing_Size
    $button1.UseVisualStyleBackColor = $True
     
    $button1.Text = "Back"
     
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 13
    $System_Drawing_Point.Y = 459
    $button1.Location = $System_Drawing_Point
    $button1.DataBindings.DefaultDataSourceUpdateMode = 0
    $button1.add_Click($button1_OnClick)
     
    $form1.Controls.Add($button1)
     
    $listView1.UseCompatibleStateImageBehavior = $False
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Width = 664
    $System_Drawing_Size.Height = 440
    $listView1.Size = $System_Drawing_Size
    $listView1.DataBindings.DefaultDataSourceUpdateMode = 0
    $listView1.Name = "listView1"
    $listView1.View = 0
    $listView1.TabIndex = 0
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 13
    $System_Drawing_Point.Y = 13
    $listView1.Location = $System_Drawing_Point
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Width = 32
    $System_Drawing_Size.Height = 32
    $listView1.TileSize = $System_Drawing_Size
     
    $listView1.add_DoubleClick($doubleClick)
     
    $form1.Controls.Add($listView1)
     
    #endregion Generated Form Code
     
    #Save the initial state of the form
    $InitialFormWindowState = $form1.WindowState
    #Init the OnLoad event to correct the initial state of the form
    $form1.add_Load($OnLoadForm_StateCorrection)
    #Show the Form
    $form1.ShowDialog()| Out-Null
     
    } #End Function
     
    #Call the Function
    GenerateForm

     

    Thursday, October 29, 2015 9:27 PM

Answers

All replies

  • The OpenFileDialog can, to some degree, limit the view.  Outside of that it is not possible to hide WIndows from a user without replacing 'Explorer".

    \_(ツ)_/

    Friday, October 30, 2015 12:32 AM
  • how would I open documents that a user double clicks on using the script I posted?
    Friday, October 30, 2015 12:38 AM
  • What does "Open" mean?  Every kind of document has many ways to "open".

    In your code a user clicks on a control in a form.  What happens is up the programmer to decide on and implement.


    \_(ツ)_/

    Friday, October 30, 2015 1:28 AM
  • yes, when the user double clicks on a word doc, open word, click on excel doc, open excel, etc.
    Friday, October 30, 2015 1:32 AM
  • Go to a prompt in PowerShell.  type the full path name of almost any document and it will open.

    now assume that $textbox1 has thee path of the word document:
    $button_click={
          $textbox1.Text
    }

    Now you have opened the word document.


    \_(ツ)_/


    • Edited by jrv Friday, October 30, 2015 1:37 AM
    Friday, October 30, 2015 1:36 AM
  • ok so are you saying I need to add a text box to my form?  At this point it just opens a file explorer that shows all the folders and files.  When double clicked I want the file to open.  Sorry, I am not much at this level of PS.
    Friday, October 30, 2015 2:23 AM
  • No.  I am just using a textbox as an example.  All you need to do is state the object that contains the string that is the full path to the document.in regular PowerShell CLI.

    ifwe have any variable with the pathname of a document like this:
    $mydoc='c:\path\document.docx'

    We can open it like this:

    $mydoc

    Open up PowerShell and try it.


    \_(ツ)_/

    Friday, October 30, 2015 2:30 AM
  • ok thank you..I just gotta figure out how to get the path into a variable based on the user double clicking a file
    Friday, October 30, 2015 2:44 AM
  • ok thank you..I just gotta figure out how to get the path into a variable based on the user double clicking a file
    Friday, October 30, 2015 2:45 AM
  • Ok - I understand that I can simply run c:\path\document.xls and it will open the appropriate application.  I am having trouble figuring out how to trap what the filename is that the user will be double-clicking on in the form I run in my code so what I can launch word, excel, notepad, whatever.

    $doubleClick= 
    {
        if ($listView1.SelectedItems[0].tag -eq "File") { 
            I DON"T KNOW WHAT TO DO HERE TO LAUNCH THE FILE THAT THE USER SELECTS
        } else {
            Refresh-ListView $listView1.SelectedItems[0].Name
        }
    }

    Monday, November 2, 2015 7:03 PM
  • You can try the call operator:

    http://ss64.com/ps/call.html


    Monday, November 2, 2015 7:14 PM
  • Ok - I understand that I can simply run c:\path\document.xls and it will open the appropriate application.  I am having trouble figuring out how to trap what the filename is that the user will be double-clicking on in the form I run in my code so what I can launch word, excel, notepad, whatever.

    $doubleClick= 
    {
        if ($listView1.SelectedItems[0].tag -eq "File") { 
            I DON"T KNOW WHAT TO DO HERE TO LAUNCH THE FILE THAT THE USER SELECTS
        } else {
            Refresh-ListView $listView1.SelectedItems[0].Name
        }
    }

    What do you mean by "launch the file.  Just launch it:

    . $listView1.SelectedItem

    That is all assuming the item has the full name of the file.  The code is badly written and does not give you much flexibility.


    \_(ツ)_/

    Monday, November 2, 2015 7:20 PM
  • Here are two lines of PowerShell code that do what you are trying to do.

    $files=dir *.* -include *.txt,*.xls*,*.pdf,*.doc* -file|select name,fullname
    $files|Out-GridView -OutputMode Single|?{$_}|%{. $_.Fullname}


    \_(ツ)_/

    Monday, November 2, 2015 7:27 PM
  • Here is a quick and dirty exercise in how to code up a simple GUI to select files: "'

    <#
    The following is hand coded  use double-click to select
    #>
    $folder = 'c:\test\*'
    $files=dir $folder -include *.txt, *.xls*, *.pdf, *.doc* -file | select name, fullname
    Add-Type -AssemblyName System.Windows.Forms
    $form = New-Object System.Windows.Forms.Form
    $form.StartPosition='CenterScreen'
    $lb = New-Object System.Windows.Forms.ListBox
    $form.Controls.Add($lb)
    $lb.Dock = 'Fill'
    $lb.DataSource = [system.Collections.ArrayList]$files
    $lb.DisplayMember='Name'
    $lb.add_MouseDoubleClick({
    	. $lb.SelectedItem.Fullname
    })
    $form.ShowDialog()



    \_(ツ)_/




    • Edited by jrv Monday, November 2, 2015 8:02 PM
    Monday, November 2, 2015 7:42 PM
  • Edit two lines and we have a variation for another use:

    <#
    The following is hand coded  use double-click to select
    #>
    $folder = 'c:\test\*'
    $files=dir $folder -include *.txt, *.xls*, *.pdf, *.doc* -file | select name, fullname
    Add-Type -AssemblyName System.Windows.Forms
    $form = New-Object System.Windows.Forms.Form
    $form.Size='300,600'
    $form.StartPosition='CenterScreen'
    $lb = New-Object System.Windows.Forms.ListBox
    $form.Controls.Add($lb)
    $lb.Dock = 'Fill'
    $lb.DataSource = [system.Collections.ArrayList]$files
    $lb.DisplayMember='Name'
    $lb.add_MouseDoubleClick({
    	. $lb.SelectedItem.Fullname
    	$form.Close()
    })
    $form.ShowDialog()
    $lb.SelectedItem
    
    
    


    \_(ツ)_/

    Monday, November 2, 2015 7:43 PM
  • Thank you so much for your help.  I learned a lot about lb's just by your script.  However I was asked to make it "pretty" for the end users.  I have the below code.  This does list out the file names. I think I can alter the variable to have it output the folders as well. I also would like it to show the folders as folder icons and the files as a different icon. I would also want to be able to use the back button to back out of a folder. My initial post had that functionality but I could not double click the files to open the files in the appropriate application (i.e...doc with word, xls with excel, etc)  Can you assist?
    function GenerateForm {
    [reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
    [reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
    #endregion
     
    #region Generated Form Objects
    $form1 = New-Object System.Windows.Forms.Form
    $listBox1 = New-Object System.Windows.Forms.ListBox
    $pictureBox1 = New-Object System.Windows.Forms.PictureBox
    $CLOSE = New-Object System.Windows.Forms.Button
    $BACK = New-Object System.Windows.Forms.Button
    $InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
    $form1.Controls.Add($listBox1)
    $listBox1.DataSource = [system.Collections.ArrayList]$files
    $listBox1.DisplayMember='Name'
    $listBox1.add_MouseDoubleClick({
     . $lb.SelectedItem.Fullname
    })
    #$form.ShowDialog()
    $CLOSE_OnClick=
    {
    $form1.Close()
    }
    $BACK_OnClick=
    {
    }
    $handler_listBox1_SelectedIndexChanged=
    {
    }
    $OnLoadForm_StateCorrection=
    {#Correct the initial state of the form to prevent the .Net maximized form issue
     $form1.WindowState = $InitialFormWindowState
    }
    #region Generated Form Code
    $form1.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon('C:\Program Files\SAPIEN Technologies, Inc\PrimalForms\PrimalForms_Doc.ico')
    $form1.BackColor = [System.Drawing.Color]::FromArgb(255,236,233,216)
    $form1.Text = " File Browser"
    $form1.Name = "form1"
    $form1.DataBindings.DefaultDataSourceUpdateMode = 0
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Width = 922
    $System_Drawing_Size.Height = 774
    $form1.ClientSize = $System_Drawing_Size
     
    $listBox1.FormattingEnabled = $True
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Width = 922
    $System_Drawing_Size.Height = 667
    $listBox1.Size = $System_Drawing_Size
    $listBox1.DataBindings.DefaultDataSourceUpdateMode = 0
    $listBox1.Name = "listBox1"
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 0
    $System_Drawing_Point.Y = 13
    $listBox1.Location = $System_Drawing_Point
    $listBox1.TabIndex = 4
    $listBox1.add_SelectedIndexChanged($handler_listBox1_SelectedIndexChanged)
    $form1.Controls.Add($listBox1)
    $pictureBox1.TabIndex = 3
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Width = 354
    $System_Drawing_Size.Height = 93
    $pictureBox1.Size = $System_Drawing_Size
    $pictureBox1.BackgroundImage = [System.Drawing.Image]::FromFile('C:)
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 287
    $System_Drawing_Point.Y = 683
    $pictureBox1.Location = $System_Drawing_Point
    $pictureBox1.TabStop = $False
    $pictureBox1.Name = "pictureBox1"
    $pictureBox1.DataBindings.DefaultDataSourceUpdateMode = 0
     
    $form1.Controls.Add($pictureBox1)
    $CLOSE.TabIndex = 2
    $CLOSE.Name = "CLOSE"
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Width = 113
    $System_Drawing_Size.Height = 73
    $CLOSE.Size = $System_Drawing_Size
    $CLOSE.UseVisualStyleBackColor = $True
     
    $CLOSE.Text = "CLOSE"
    $CLOSE.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",8.25,1,3,0)
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 797
    $System_Drawing_Point.Y = 689
    $CLOSE.Location = $System_Drawing_Point
    $CLOSE.DataBindings.DefaultDataSourceUpdateMode = 0
    $CLOSE.add_Click($CLOSE_OnClick)
    $form1.Controls.Add($CLOSE)
    $BACK.TabIndex = 1
    $BACK.Name = "BACK"
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Width = 111
    $System_Drawing_Size.Height = 73
    $BACK.Size = $System_Drawing_Size
    $BACK.UseVisualStyleBackColor = $True
     
    $BACK.Text = "BACK"
    $BACK.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",8.25,1,3,0)
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 12
    $System_Drawing_Point.Y = 689
    $BACK.Location = $System_Drawing_Point
    $BACK.DataBindings.DefaultDataSourceUpdateMode = 0
    $BACK.add_Click($BACK_OnClick)
    $form1.Controls.Add($BACK)
    #endregion Generated Form Code
     
    #Save the initial state of the form
    $InitialFormWindowState = $form1.WindowState
    #Init the OnLoad event to correct the initial state of the form
    $form1.add_Load($OnLoadForm_StateCorrection)
    #Show the Form
    $form1.ShowDialog()| Out-Null
     
    } #End Function
     
    #Call the Function
    GenerateForm

    • Edited by tellmeonce Wednesday, November 4, 2015 10:03 PM
    Wednesday, November 4, 2015 8:31 PM
  • An I recommend a course in Windows Forms development for beginners?  Teaching you how to use develop forms is outside of the scope of this forum.

    You can also look here: https://www.sapien.com/forums/viewtopic.php?f=21&t=6451


    \_(ツ)_/

    Wednesday, November 4, 2015 8:42 PM
  • no you cannot recommend that.  But thanks for the offer.  I will figure it out.
    Wednesday, November 4, 2015 8:45 PM
  • Did you even look at the links. They are a pretty complete tutorial on how to design with controls.

    \_(ツ)_/

    Wednesday, November 4, 2015 8:50 PM
  • yes I have been to that website.  I'm just in a very limited time frame right now.  Thanks again for your help
    Wednesday, November 4, 2015 9:12 PM
  • I'm just in a very limited time frame right now.

    Surely you can understand why that is a very presumptuous statement. We all have limited time. I am sure you can understand that redesigning your script to meet your specifications is not at the top of someone else's priority list (particularly when you expect it for free!).


    -- Bill Stewart [Bill_Stewart]

    Wednesday, November 4, 2015 9:43 PM
    Moderator
  • as I stated before, I appreciated the help.  The attitude given back from you is not understood.  Can you show me where I asked for it to be on someone's priority list at all?  I ASKED for help.  If you are not interested then please do not respond to the thread.  You just wasted your time.  I thanked jrv for his help.

    So please take your comments elsewhere.  thanks.

    Wednesday, November 4, 2015 9:56 PM
  • Hey everybody - cool it.  I don't think it was meant the way tellme worded it. It was just driven by frustration.

    Sorry tellme.  I am not blowing you off.  If you have specific questions about forms we would be happy to help.  We just can't give you an interactive tutorial. You will get a tutorial in bits if you can figure out how to ask the right x113uesitons.

    A not about ListView.  kip in in favor of DataGridView.  Listview drives even experience programmers nuts because it is a non-standard model.  DOn't get me wrong.  It is very good and very flexible but very hard to program,.

    The Sapien blog has numerous posts on how to manage a Listview and images.


    \_(ツ)_/

    Wednesday, November 4, 2015 10:26 PM
  • thank you again jrv.
    Wednesday, November 4, 2015 10:47 PM
  • thank you again jrv.

    You are welcome.

    Bill is not trying to be rude. He is just trying to prevent expectations from getting out of control.  Too many come here to badger the volunteers as to get free scripts or just to be a pain. As a moderator Bill tries to set expectations and to prevent chaos in a very busy forum.  I assure you he meant no personal attack.

    Good luck "Forming".


    \_(ツ)_/

    Wednesday, November 4, 2015 11:21 PM