Batch Script not Executing filename + Parameters

Microsoft Windows
Post Reply
SNIPE
Posts: 3
Joined: 2023-Feb-17, 6:57 pm

Batch Script not Executing filename + Parameters

Post by SNIPE »

My batch script asks the user if he wants to run the program LegoScript.exe If the user hits 'N' or 'n' then it terminates the program, however if the user types 'Y' or 'y' then it should echo the string "legoscript.exe" and then place the cursor right after it on the same line, so that the user can type additional arguments, and then run the program with the provided arguments, however rather than running the program with the provided arguments, it just shows me a blinking caret indefinitely

Here is the batch script:

Code: Select all

@echo off
setlocal enabledelayedexpansion
set "base_path=%userprofile%\\desktop\\LegoScript\\"
for /d %%d in ("%base_path%\V*") do (
    set "latest=%%d"
)
set "LATEST=!latest!"
echo Latest folder is: %LATEST%
"%base_path%\\MINGW\\Bin\\gcc.exe" "%latest%\\LegoScript.c" -o  "%latest%\\LegoScript.exe"
set /p run=Do you want to run LegoScript.exe? (y/n): 
if /i "%run%"=="y" (
@cmd /k ^"cmd /c "echo|set /p="LegoScript.exe "&set /p "arg1= "&& set /p "arg2= "&& set /p "arg3= "&& set /p "arg4= "&& start "" "%latest%\\LegoScript.exe" %arg1% %arg2% %arg3% %arg4%
) else if /i "%run%"=="n" (
    exit
) else (
    echo Invalid input. Please enter y or n.
    pause >nul
    call %0
)
Anyone care to figure out how to fix this?

I tried adding extra double quotes and escape characters ('^') but no luck.
User avatar
Simon Sheppard
Posts: 190
Joined: 2021-Jul-10, 7:46 pm
Contact:

Re: Batch Script not Executing filename + Parameters

Post by Simon Sheppard »

I don't think there is any easy way to do what you want, echoing out the values will always include a line return/termination.

Some options to consider:

1) Capture all the values from the user into variables and construct & run the command.

2) Use the CLIP command to store all the items in the clipboard and then have the user manually paste that phrase at the command line.

3) Forget about using a script, manually type it out and then use history / F7 to modify the parameters.
SNIPE
Posts: 3
Joined: 2023-Feb-17, 6:57 pm

Re: Batch Script not Executing filename + Parameters

Post by SNIPE »

Hi,

I did not know about F7 / fn+F7 in CMD until today!.

I decided to instead make some embedded VBS script in my batch file that opens a 'browse for file' dialog rather than do as I said in my original post
however I still would like for it to, after it asks for me to browse for the file, echo gcc %thefilename%.c -o %thefileame.exe and then execute that.

Except that win11 and probably prior versions don't support the ActiveX component: 'Accounts.CommonDialog'. it can only make a browse for folder dialog using WScript.run.

However the old mshya trick still works:

Code: Select all

Set wShell=CreateObject("WScript.Shell")
Set oExec=wShell.Exec("mshta.exe ""about:<input type=file id=FILE><script>FILE.click();new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).WriteLine(FILE.value);close();resizeTo(0,0);</script>""")
sFileSelected = oExec.StdOut.ReadLine
wscript.echo sFileSelected
Regards, S
Rekrul
Posts: 52
Joined: 2021-Aug-15, 11:29 pm

Re: Batch Script not Executing filename + Parameters

Post by Rekrul »

SNIPE wrote: 2023-Feb-22, 7:03 am however I still would like for it to, after it asks for me to browse for the file, echo gcc %thefilename%.c -o %thefileame.exe and then execute that.
I'm curious: Why do you want to print the command to the actual command prompt and then have the user press a key to execute it?

I understand the idea, but you can do almost the same thing from inside the script.

Something like;

Code: Select all

set defaultparameters=-a -b -c

echo Current command line is: gcc %parameters% %filename%.c -o %filename%.exe
echo.
echo Just press Enter to execute this command, or enter additional parameters.
echo Enter a * as the first character to blank the existing parameters.
echo.

set /p userparameters=Enter Parameters:
if "%userparameters:~0,1%"=="*" (
set defaultparameters=
set userparameters=%userparameters:~1%
)

gcc %defaultparameters% %userparameters% %filename%.c -o %filename%.exe
If the user just presses Enter, the default value of %parameters% will be used. If they enter new parameters, those will be appended to the existing ones. If they enter a star, the default parameters will be blanked and only what they enter will be used.

I don't know much (OK, anything) about compiling programs, however, if I were doing it on a regular basis, and there were certain parameters that I found myself using frequently, I would probably make a menu-driven script using the Choice command, with there being a key to toggle each parameter on/off, indicated by a star next to it, and then when the compile option is chosen, it would issue the command and include all the parameters that the user has chosen. If a parameter also needs additional info, such as a numeric value, the script would ask for that as well when that parameter is toggled on.

I've done stuff like this for scripts that convert files.
SNIPE
Posts: 3
Joined: 2023-Feb-17, 6:57 pm

Re: Batch Script not Executing filename + Parameters

Post by SNIPE »

The reason I did it was because I had several folders named v1,v2,v3 etc so while my code that detected the latest folder worked, It was for the CLI part of my C project, so I want to to use the same makefile.bat to work with the graphical part of my C project, which meant that I decided to manually type the filename but later realized that I can just use a browse dialog box would be better however this still had the same problem that after I was selecting the file from the dialog, cmd still could not echo gcc and then echo the filename right after that, on the same line but also execute it at the same time.

Anyhow, I made my make file in C instead, now it lets much more smarter and has its own parameter syntax.

I also just remembered that I (think) that I kinda solved the batch problem with my Disable-WFP-XP.bat that I made years ago.
Here is the code in case anyone else is stuck in the same rut as I was:

Code: Select all

echo S 100 8000 74 00 5C 00 4F >tmp.$$
echo quit >>tmp.$$
set act=type tmp.$$ ^^^| debug %file%
set addr=
echo patching %file%
for /f "tokens=2 delims=:" %%A IN ('%act%') do set addr=%%A
if "%addr%" equ "" (
  echo failed to find address, bad file? already patched?
  pause
  exit /b 2
)
echo E %addr% 74 00 00 00 4F >tmp.$$
echo W >>tmp.$$
echo Q >>tmp.$$
type tmp.$$ | debug %file%
del tmp.$$
pause
Correct me if I'm wrong but I think this does the same thing more or less (echo and then execute).

Regards, Snipe
Post Reply