Author Topic: VBSCRIPT - Select folder/file to save to  (Read 5627 times)

MatthiasVDE

  • EA User
  • **
  • Posts: 196
  • Karma: +1/-0
    • View Profile
VBSCRIPT - Select folder/file to save to
« on: September 13, 2022, 06:28:31 pm »
I use this function to open a file, but now I want to adapt it to select a file or folder to save to. For that I need to change the script but I'm not familiar with those activeX stuf.

Code: [Select]
function selectFilePath()
dim wShell, oExec, sFileSelected
set wShell=CreateObject("WScript.Shell")
set oExec=wShell.Exec("mshta.exe ""about:[b]<input type=file id=FILE><script>FILE.click();new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).WriteLine(FILE.value);close();resizeTo(0,0);</script>[/b]""")
sFileSelected = oExec.StdOut.ReadLine
selectFilePath = sFileSelected
end function

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13404
  • Karma: +567/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: VBSCRIPT - Select folder/file to save to
« Reply #1 on: September 13, 2022, 07:51:05 pm »
Seems like you are taking the complicated path. ???

This is the function I use in my FileSystemFolder utility class: https://github.com/GeertBellekens/Enterprise-Architect-VBScript-Library/blob/master/Framework/Utils/FileSystemFolder.vbs

Code: [Select]
public function getUserSelectedFolder(startPath)
dim folder, shell
Set shell  = CreateObject( "Shell.Application" )
if len(startPath) > 0 then
Set folder = shell.BrowseForFolder( 0, "Select Folder", 0,startPath)
else
Set folder = shell.BrowseForFolder( 0, "Select Folder", 0)
end if
if not folder is nothing then
set getUserSelectedFolder = New FileSystemFolder
getUserSelectedFolder.FullPath = folder.Self.Path
Session.Output "folder.Self.Path: " & folder.Self.Path
else
set getUserSelectedFolder = Nothing
end if
end function

Geert

OpenIT Solutions

  • EA User
  • **
  • Posts: 555
  • Karma: +9/-1
    • View Profile
Re: VBSCRIPT - Select folder/file to save to
« Reply #2 on: September 14, 2022, 12:41:04 am »
In vbscript its actually potentially even easier as the project interface provides this method:

Dim prj As EA.Project

Set prj = Repository.GetProjectInterface()
fullPath = prj.GetFileNameDialog (filename, "*.docx", 1, 0, "", 1) + ".docx"

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13404
  • Karma: +567/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: VBSCRIPT - Select folder/file to save to
« Reply #3 on: September 14, 2022, 01:09:32 am »
In vbscript its actually potentially even easier as the project interface provides this method:

Dim prj As EA.Project

Set prj = Repository.GetProjectInterface()
fullPath = prj.GetFileNameDialog (filename, "*.docx", 1, 0, "", 1) + ".docx"
Yeah, that's OK for files, but not when you want your user to select a folder.

Geert

MatthiasVDE

  • EA User
  • **
  • Posts: 196
  • Karma: +1/-0
    • View Profile
Re: VBSCRIPT - Select folder/file to save to
« Reply #4 on: September 15, 2022, 04:25:45 pm »
Seems like you are taking the complicated path. ???

This is the function I use in my FileSystemFolder utility class: https://github.com/GeertBellekens/Enterprise-Architect-VBScript-Library/blob/master/Framework/Utils/FileSystemFolder.vbs

Code: [Select]
public function getUserSelectedFolder(startPath)
dim folder, shell
Set shell  = CreateObject( "Shell.Application" )
if len(startPath) > 0 then
Set folder = shell.BrowseForFolder( 0, "Select Folder", 0,startPath)
else
Set folder = shell.BrowseForFolder( 0, "Select Folder", 0)
end if
if not folder is nothing then
set getUserSelectedFolder = New FileSystemFolder
getUserSelectedFolder.FullPath = folder.Self.Path
Session.Output "folder.Self.Path: " & folder.Self.Path
else
set getUserSelectedFolder = Nothing
end if
end function

Geert

Hi Geert,

I used that one, but the UI is different and not easy to navigate.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13404
  • Karma: +567/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: VBSCRIPT - Select folder/file to save to
« Reply #5 on: September 15, 2022, 05:34:16 pm »
There is an alternative call CommonOpenFileDialog, but I've only found references on how to use that using C#.

I'm not sure if you can use that from VBScript, but it might be worth a question in Stackoverflow to find out.

Geert

andersd7

  • EA User
  • **
  • Posts: 26
  • Karma: +1/-0
    • View Profile
Re: VBSCRIPT - Select folder/file to save to
« Reply #6 on: September 18, 2022, 05:32:35 pm »
The following routine exports the contents of a note and saves it to a file.. in this case it is specific for PlantUML PUML files.. but it does prompt you for a folder.

https://github.com/gobravedave/Enterprise-Architect/blob/master/Diagram%20Scripts/Export-PlantUML-Script.vbs

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13404
  • Karma: +567/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: VBSCRIPT - Select folder/file to save to
« Reply #7 on: September 18, 2022, 06:55:05 pm »
The following routine exports the contents of a note and saves it to a file.. in this case it is specific for PlantUML PUML files.. but it does prompt you for a folder.

https://github.com/gobravedave/Enterprise-Architect/blob/master/Diagram%20Scripts/Export-PlantUML-Script.vbs
Do you mean the line

Code: [Select]
PlantUMLfn = project.GetFileNameDialog (currentDiagram.Name & ".puml", "PlantUML Files|*.pu;*.puml", 1, OFN_OVERWRITEPROMPT ,"", 1)
That will ask the user to select a (new or existing) file, not a folder. I don't think you can click on a folder and then OK, without having a filename in the filename field.

Geert

andersd7

  • EA User
  • **
  • Posts: 26
  • Karma: +1/-0
    • View Profile
Re: VBSCRIPT - Select folder/file to save to
« Reply #8 on: September 18, 2022, 09:37:57 pm »
agree Geert.. you need a filename to save the file.

sorry I must of misunderstood the original question.

I want to adapt it to select a file or folder to save to.