0

I am trying to make a path directory generic for all users using batch file. The code generates a popup and it asks the user to input the folder. The directory is echoed on the terminal. I am trying to pass the this output directory as an argument in another function 'folder' so that the application starts running. But I am not able to pass the argument.

@echo off
Title Browse4Folder
Color 0A
Call :Browse4Folder "Choose source folder to scan" "c:\scripts"
echo You have chosen this location "%Location%"
pause & exit
::***************************************************************************
:Browse4Folder
set Location=
set vbs="%temp%\_.vbs"
set cmd="%temp%\_.cmd"
for %%f in (%vbs% %cmd%) do if exist %%f del %%f
for %%g in ("vbs cmd") do if defined %%g set %%g=
(
    echo set shell=WScript.CreateObject("Shell.Application"^) 
    echo set f=shell.BrowseForFolder(0,"%~1",0,"%~2"^) 
    echo if typename(f^)="Nothing" Then  
    echo wscript.echo "set Location=Dialog Cancelled" 
    echo WScript.Quit(1^)
    echo end if 
    echo set fs=f.Items(^):set fi=fs.Item(^) 
    echo p=fi.Path:wscript.echo "set Location=" ^& p
)>%vbs%
cscript //nologo %vbs% > %cmd%
for /f "delims=" %%a in (%cmd%) do %%a
for %%f in (%vbs% %cmd%) do if exist %%f del /f /q %%f
for %%g in ("vbs cmd") do if defined %%g set %%g=
PAUSE


Call :Folder Location
:Folder
Set T32_PATH=%Location%
ECHO T32_PATH=%Location%


cd ..\Appl
START %Location%\t32mtc.exe -c ..\cm3550a_vecm\vlab_mcd.t32 -s ..\cm3550a_vecm\vlab_mcd_config.cmm
ECHO T32_PATH=%Location%
PAUSE

I have to pass the argument to line START %Location%\t32mtc.exe -c ..\cm3550a_vecm\vlab_mcd.t32 -s ..\cm3550a_vecm\vlab_mcd_config.cmm How shall I do it.

Compo
  • 36,585
  • 5
  • 27
  • 39
ARR
  • 41
  • 4
  • 1
    Would help were you to say what your argument is, but assuming the argument's value is in the variable `argument`, then `START "Window Title : "%Location%\t32mtc.exe" -c ..\cm3550a_vecm\vlab_mcd.t32 -s ..\cm3550a_vecm\vlab_mcd_config.cmm %argument%` might be a start. The first quoted argument will be the window title. The text `Window Title` may be omitted, but not the quotes. Quoting the executable name is required if the full-filename contains separators. I've no idea what `..\cm ... onfig.cmm` does. – Magoo Mar 06 '21 at 11:25
  • 1
    Just launch `t32mtc.exe` from VBS like this: https://stackoverflow.com/questions/1340355/launch-programs-whose-path-contains-spaces – Jack White Mar 06 '21 at 11:53
  • ARR, I have rolled back your last edit, all you have done by removing that information, is to make your question less answerable, and my existing answer submission less related to the question at the time it was answered. If there is something specifically causing you an issue with one or more of the example codes I have already provided, and which you've already stated works, please explain that as an edit to your question, instead of just removing important parts of it, _(thus making my answer less relevant)_. – Compo Mar 06 '21 at 21:20

1 Answers1

1

To use a GUI dialog box for the end user to provide their T32 directory, I'd choose to leverage either PowerShell, JScript, or (your chosen) VBScript.

Here's a quick and basic example of each, (none of which write to files):

leveraging :

@Set "T32_PATH="
@For /F Delims^=^ EOL^= %%G In ('^"
    "%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile ^
     "(New-Object -COM 'Shell.Application').BrowseForFolder(0,'Select your T32 directory.',1,0).Self.Path"
^"') Do @If Exist "%%G\t32mtc.exe" Set "T32_PATH=%%G"
@If Defined T32_PATH Start "" "%T32_PATH%\t32mtc.exe" …

leveraging :

0</* :
@Set "T32_PATH="
@For /F "Delims=" %%G In (
    '""%SystemRoot%\System32\cscript.exe" //E:JScript //NoLogo "%~f0" 2>Nul|Find /V """'
) Do @If Exist "%%G\t32mtc.exe" Set "T32_PATH=%%G"
@If Defined T32_PATH Start "" "%T32_PATH%\t32mtc.exe" …

@Exit /B */0;
var Folder=new ActiveXObject('Shell.Application').BrowseForFolder(0,'Select your T32 directory.',1,'0');
try{new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).Write(Folder.Self.Path)};catch(e){};close();

leveraging :

<!-- :
@Set "T32_PATH="
@For /F "Delims=" %%G In (
    '"%SystemRoot%\System32\cscript.exe //NoLogo "%~f0?.wsf" 2>Nul|Find /V """'
) Do @If Exist "%%G\t32mtc.exe" Set "T32_PATH=%%G"
@If Defined T32_PATH Start "" "%T32_PATH%\t32mtc.exe" …

@Exit /B
-->
<Job><Script Language="VBScript">
Set objFolder = CreateObject("Shell.Application").BrowseForFolder(0,"Select your T32 directory.",1,0)
WScript.Echo objFolder.Self.Path
</Script></Job>

To hopefully better show you that the methodology doesn't change, here's an example which uses something more like the structure of your posted example.

<!-- :
@Echo Off
SetLocal EnableExtensions DisabledelayedExpansion
Title Browse4Folder
Color 0A

:GetT32Parent
Set "T32_FILE=t32mtc.exe"
Set "T32_PATH="
Call :Browse4Folder "Select your T32 directory." "C:\scripts" "T32_PATH"
If Not Defined T32_PATH (Echo Error: Please try again.
    Pause
    GoTo GetT32Parent)
If Not Exist "%T32_PATH%\%T32_FILE%" (
    Echo The chosen directory does not contain %T32_FILE%.
    Echo Error: Please try again.
    Pause
    GoTo GetT32Parent)
Echo You have chosen this location "%T32_PATH%"
Pause
Rem Your T32 based code goes here.
Start "" "%T32_PATH%\%T32_FILE%" … 

Rem Any other code goes here.

Exit /B

:Browse4Folder
If "%~3" == "" (Set "sVariable=location") Else Set "sVariable=%~3"
If "%~2" == "" (Set "vRootFolder=%CD%") Else Set "vRootFolder=%~2"
If "%~1" == "" (Set "sTitle=Please select your directory.") Else Set "sTitle=%~1"
Set "%sVariable%="
For /F "Delims=" %%G In ('%SystemRoot%\System32\cscript.exe //NoLogo "%~f0?.wsf"
 "%sTitle%" "%vRootFolder%" 2^>Nul') Do Set "%sVariable%=%%G"
GoTo :EOF
-->
<Job><Script Language="VBScript">
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.BrowseForFolder(0,WScript.Arguments.Item(0),0,WScript.Arguments.Item(1))
WScript.Echo objFolder.Self.Path
</Script></Job>
Compo
  • 36,585
  • 5
  • 27
  • 39
  • Thanks a lot.This code helps to open trace32 debugger but trace32 not working. – ARR Mar 06 '21 at 15:05
  • Thanks for your help.the code is wooking now.Thanks a lot and apology if any of my comments hurted you . – ARR Mar 06 '21 at 15:55
  • If the code is working for you @AnkitaRayon, is there a particular reason why you've decided not a accept my answer? – Compo Mar 06 '21 at 16:06
  • Yes actually your code is too good to be honest and I won't be able to defend the logic to my trainer since you have used powershell and I'm not familiar with it. – ARR Mar 06 '21 at 16:14
  • I only used PowerShell in one of my examples @AnkitaRayon! I have changed my submission to include a VBscript based answer too, Is that satisfactory? Also, at no point did you inform us that this is homework/assignment. Please note, that you cannot expect me to write code which looks and works to the exact level you need in order to be able to pass it off as your own. – Compo Mar 06 '21 at 17:16
  • I will also add that it really doesn't matter what you use as your PowerShell, JScript, or VBScript code, the important part of the above submission, is that the method of running the leveraged code does not change, abd the method of propagating a variable using the selected directory is also exactly the same. If you're incapable of reading and understandsing how to use `set`, `for /f` loop, or `if`, then you're not going to get the type of help you need, as you can't expect us to provide a personal coaching service to you. Open a Command Prompt window, and read the help information of each. – Compo Mar 06 '21 at 17:25
  • I don't believe that I am @ARR, _(name changed)_, I'm simply trying to ascertain what your issue is with one or more of the working solutions I've submitted, with regards you accepting the answer. Which part of passing the directory from the dialog box, (your specific question), are you having difficulty with? All three of the examples above do it in exactly the same way, they use a `for /f` loop which runs the alternative language code, and `set`s the returned output, `if` the target executable `exist`s within it, to a variable. It then runs a command using the `defined` variable. – Compo Mar 06 '21 at 18:02