0

I get a file chooser from another discussion and i want to change InitialDirectory value. Now is:

<# :
...
setlocal
for /f "delims=" %%I in ('powershell -noprofile "iex (${%~f0} | out-string)"')
...
goto :EOF
: #>
Add-Type -AssemblyName System.Windows.Forms
$f = new-object Windows.Forms.OpenFileDialog
$f.InitialDirectory = pwd
$f.Filter = "ucsdb backup (ucsdb.*)|ucsdb.*"
$f.ShowHelp = $false
$f.Multiselect = $false
[void]$f.ShowDialog()
if ($f.Multiselect) { $f.FileNames } else { $f.FileName }

and it open the current directory but i want to open a subfolder. How can i write Batch "%cd%\UCSM_Files\" in InitialDirectory?

Sorry for my bad english

Blank517
  • 23
  • 3

3 Answers3

1

You can do it several ways, really. The shortest way would be to evaluate pwd within $() in a string like this:

$f.InitialDirectory = "$(pwd)\UCSM_Files"

Or you could use string formatting.

$f.InitialDirectory = "{0}\UCSM_Files" -f (pwd)
rojo
  • 24,000
  • 5
  • 55
  • 101
0

From the variable syntax it looks like you are trying to do this in PowerShell already. If so, you could use the Join-Path cmdlet (and the $PWD automatic variable):

$f.InitialDirectory = Join-Path -Path $PWD -ChildPath UCSM_Files
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • (Updated discussion code) I've modified '$f.InitialDirectory = pwd' to '$f.InitialDirectory = Join-Path -Path $PWD -ChildPath UCSM_Files' but now open Documents folder – Blank517 Mar 31 '16 at 22:16
0

You could try setting an environment variable in your batch then use it in powershell:

<# :
...
set "initialdir=%~dp0"
...
#>
$f.InitialDirectory = "$($env:initialdir)UCSM_Files"
...

I also noticed you disable multiselect but then check for it anyway:

$f.Multiselect = $false
if ($f.Multiselect) { $f.FileNames } else { $f.FileName }
xXhRQ8sD2L7Z
  • 1,686
  • 1
  • 14
  • 16
  • If you're curious, [this is the source](http://stackoverflow.com/a/15885133/1683264) of OP's script. The reason for the `if ($f.Multiselect)` line is so the person borrowing my code only has to change one value to toggle multiple file selection. Actually, upon reflection, I probably could've just used `$f.FileNames` for either condition, regardless of whether `$f.Multiselect` is true or false. I was probably still thinking in terms of C# during my last edit though. :) – rojo Apr 01 '16 at 00:10