1

The following code is supposed to install some .exe files from a folder. However, the for loop doesn't seem to work. I have also tried !pathexe!

If 64 bit computer, set directory to be current batch directory/files/exe.
Then install all EXE found in that directory.

cls
echo.
setlocal enableDelayedExpansion
if %PROCESSOR_ARCHITECTURE%==AMD64 set arch=x64
(
    set filesexe=files/exe
    set pathexe=%~dp0!filesexe!
    echo !pathexe!
    setlocal disableDelayedExpansion
    for  "%pathexe%" %%m in ("*.exe") do (
        start "" /w  %%m
    )
)

setlocal enableDelayedExpansion
if %PROCESSOR_ARCHITECTURE%==x86 set arch=x86
(
    for /F "tokens=4-5 delims=[.] " %%A in ('ver') do set ver=%%A.%%B
    set filesexe=files/86/exe
    set pathexe=%~dp0!filesexe!
    echo !pathexe!
    setlocal disableDelayedExpansion
    for  "%pathexe%" %%m in ("*.exe") do (
        start "" /w  %%m
    )
)
Mofi
  • 46,139
  • 17
  • 80
  • 143
Kiera Smith
  • 301
  • 1
  • 5
  • 15

1 Answers1

1

Kiera Smith, this question is similar to this one posted also by you.

Your posted code has some mistakes and unused code making it really hard to answer it. However, I try it nevertheless.

First, everything between round brackets is a block. cmd.exe parses an entire block and expands all variable references within percent signs already on parsing the block. Therefore %pathexe% in second for loop is expanded already before defined at all, i.e. replaced by an empty string.

Second, environment variable arch is not used anywhere although there is an extra if condition to define it.

It is unclear if the block within most outer ( and ) should be the block for the if condition. This is currently not the case as the if condition is just for set arch=x64. Opening ( of an if block must be on same line as keyword if after the condition and set arch=x64 would be moved into the block and not outside.

Version of Windows assigned to variable ver is not further processed at all.

/r on second for loop results in searching recursive for *.exe in specified folder and all subfolders. Is that really wanted?

Do you have ever run your batch file from within a command prompt window and have used in first line echo on instead of echo off?

This would make it possible to debug what command line interpreter really interprets on executing the batch file.


Edit after a providing a more complete code:

It looks like your entire code could be reduced to following lines:

@echo off
cls
echo.
setlocal DisableDelayedExpansion
if "%ProgramFiles(x86)%"=="" (set "arch=\86") else (set "arch=")
set "pathexe=%~dp0files%arch%\exe"
for %%I in ("%pathexe%\*.exe") do start "Running %%~nI" /wait "%%I"
endlocal

Or even more reduced:

@echo off
cls
echo.
if "%ProgramFiles(x86)%"=="" (set "arch=\86") else (set "arch=")
set "pathexe=%~dp0files%arch%\exe"
for %%I in ("%pathexe%\*.exe") do start "Running %%~nI" /wait "%%I"

The environment variable arch is either initialized with the string \86 or removed if existing at all depending on existence of environment variable ProgramFiles(x86) which exists only on Windows x64.

The processor architecture does not matter for installing x86 or x64 executables. It matters which version of Windows is processing the batch file. It is possible to use on a computer with x64 processor nevertheless Windows x86 if more then 4 GB is not installed and x86 drivers are available also for the hardware components of the computer. See also Why %processor_architecture% always returns x86 instead of AMD64?

The path to the EXE files is:

  • path of the batch files referenced with %~dp0 and ending always with a backslash,
  • plus fixed directory name files,
  • plus string \86 on Windows x86 (backslash as directory separator + directory name 86) or nothing on Windows x64,
  • plus a backslash as directory separator and directory name exe.

Then each .exe file in this directory is started with command start and batch file processing is halted always until each started application terminated itself.

Community
  • 1
  • 1
Mofi
  • 46,139
  • 17
  • 80
  • 143
  • Thanks so much @Mofi. Works great. Question though, how can i now pass command line arguments to the install path. For example, I want to run: `I:\pt\files\exe\firts.exe /?` – Kiera Smith Jul 26 '15 at 16:56
  • also, how do I print the value of `%%~nI` to the scree? echo `%%~nI` is not working – Kiera Smith Jul 26 '15 at 17:42
  • For passing parameters to started executable append the parameters after `"%%I"`, for example `"%%I" /?`. If you want to pass parameters passed to the batch file itself, use `"%%I" %*`. Run in a command prompt window `call /?` for details on parameters referencing. – Mofi Jul 27 '15 at 05:45
  • First, read [this answer](http://stackoverflow.com/a/24725044/3074564) explaining the different methods of running executables from within a batch file. In which window do you want to see output of `echo %%~nI`? You can output it in the window of the batch file or in the window opened by command __start__ with title `Running %%~nI` in case of started executable is a console and not a GUI application. BTW: If all executables to start are console applications, `start "Running %%~nI" /wait` is not necessary at all. – Mofi Jul 27 '15 at 05:53