How to run all .EXE file within a folder spontaneously?

Microsoft Windows
Post Reply
User avatar
MigrationUser
Posts: 336
Joined: 2021-Jul-12, 1:37 pm
Contact:

How to run all .EXE file within a folder spontaneously?

Post by MigrationUser »

09 May 2010 20:15
arash

I've copied many of Windows XP Hotfixes into a folder and now I wonder if it is possible to make a Batchfile that
could recognize all of them individually and execute them spontaneously one after another?
By now I use a batch which contains all the files name in separate lines that end up with"/wait /passive" switch.

----------------------------

#2 10 May 2010 00:06
RG


The FOR command is what you need here. I have an example below. Remove some of the stuff noted if not necessary.

Code: Select all

@ECHO OFF
TITLE %~nx0
REM Line below and POPD at end needed for Vista Runas
PUSHD "%~dp0"

REM Leave this out if you want.
IF "%CD%"=="%systemroot%" (
   COLOR CF
   ECHO.
   ECHO ERROR! This program must be executed from a local drive.
   PAUSE
   EXIT
   )

REM Leave this out if admin not required
VER | FIND "Version 6.0." > nul
IF %ERRORLEVEL% == 0 (
   REM Do OPENFILES to check for administrative privileges
   OPENFILES > nul
   IF ERRORLEVEL 1 (
      COLOR CF
      ECHO.Right click on this bat file and select 'Run as administrator'.
      PAUSE
      EXIT
      )
   )

ECHO.The following updates will be installed in the order shown:
ECHO.

FOR %%A IN ("*.*") DO ECHO.%%A
ECHO.
ECHO.Press CTRL C to exit or
PAUSE
FOR %%A IN ("*.*") DO "%%A"

PAUSE
POPD
Windows Shell Scripting and InstallShield

----------------------------

#3 10 May 2010 17:16
arash


wow...
It works perfectly :clap: :clap: :clap:
That's awesome, Thanx a lot dear RG.
But is it possible to execute each file under "/wait /passive" swith to automatise the entire setup process?
You know it's a bit annoying to manually proceed almost 250 Windows XP Hotfixes :wall:

----------------------------

#4 10 May 2010 17:49
RG


If those are arguments that you pass to the hotfixes, then just change
FOR %%A IN ("*.*") DO "%%A"
to
FOR %%A IN ("*.*") DO "%%A" "/wait /passive

You might also want to change *.* to *.exe (assuming all files are .exe) so that you don't accidently attempt to execute something that you obviously did not intend to.

Last edited by RG (10 May 2010 17:51)

Windows Shell Scripting and InstallShield

----------------------------

#5 10 May 2010 23:58
arash
RG wrote:

If those are arguments that you pass to the hotfixes, then just change
FOR %%A IN ("*.*") DO "%%A"
to
FOR %%A IN ("*.*") DO "%%A" "/wait /passive

You might also want to change *.* to *.exe (assuming all files are .exe) so that you don't accidently attempt to execute something that you obviously did not intend to.
Dear RG, I've changed the code as you said,
FOR %%A IN ("*.*") DO "%%A"
to
FOR %%A IN ("*.exe") DO "%%A" /passive
and now it works perfectly. :clap: :clap: :clap:
By your help I can install all 250 hotfixes now just by 1 click, just 1 click, amazing.
I would like to express my heartful appreciation and thanks to you for that great job.

But would you mind making even another improvement? It echoes and then executes files chaotically
rather than by name order!
Is it possible to inject a code that enable batchfile to execute files by ascending name order?

Last edited by arash (11 May 2010 00:18)

----------------------------

#6 11 May 2010 01:13
RG


arash,
Glad it does the job for you. Copy/Paste lines below so you don't miss anything.
Change first FOR line to:
FOR /F %%A IN ('DIR /B *.*') DO ECHO.%%A
Change second FOR line to
FOR /F %%A IN ('DIR /B *.*') DO "%%A" /passive
This will use the lines from the output of the DIR command. Use /B for bare format (no header and summary).
If you want you could add a space and /ON
after the /B on both FOR lines
This will force sorting alphabetically by name, but I think it should come out that way anyway.
See DIR /?

Last edited by RG (11 May 2010 01:17)

Windows Shell Scripting and InstallShield

----------------------------

#7 11 May 2010 10:42
bluesxman


I realise I'm a bit late to the party, but I happen to have written just such a script a few years back. Being a few years old it exhibits some coding habits/methods that I have since refined, but I still use it myself today so it should be sound. It has the same basic functionality as RG's, but fancied up a little for user friendliness:

Code: Select all

@echo off

setlocal enabledelayedexpansion enableextensions

set date=%date:/=%
set time=%time::=%
set dttm=%date%-%time: =0%
set date=
set time=

set donedir=%~dp0\Done\
set tempfile=%temp%\%~n0.tmp
set logfile=%userprofile%\Desktop\%~n0@%dttm%.log

set count=0
set done=0
set p.done=
set p.count=
set error=0

fsutil fsinfo drivetype %~d0 2>nul | find /i "CD-ROM" >nul && set mv=REM

type nul > "%tempfile%"
type nul > "%logfile%"

for /f "usebackq tokens=*" %%x in (`dir /on /b "*KB*.exe"`) do (
    echo:"%%~x">>"%tempfile%"
    set /a count+=1
)

call :div
call :banner-s Attempting to install %count% patches ...

%mv% mkdir "%donedir%" 2>nul

call :pad count

for /f "usebackq tokens=*" %%x in ("%tempfile%") do (

    set /a done+=1

    call :pad done

    call :div
    call :banner [!p.done!!done!/!p.count!!count!] Installing %%~x ...

    set errorlevel=0
    set errorlevel=

    start /min /b /w "%%~x" "%%~x" -q -z

    call :tee Done.
%mv%    move "%%~x" "%donedir%"

)

call :div
call :beep 2

call :banner-s Run complete.
call :div

echo:
echo:Please reboot.

del "%tempfile%"
pause>nul

:::::::::
goto :EOF
:::::::::
****************************************************************************************
:::::::::
:banner-s
:::::::::

call :banner %* [%date% %time: =0%]

:::::::::
goto :EOF
:::::::::
****************************************************************************************
:::::::::
:banner
:::::::::

for %%b in ("call :tee" "title") do (%%~b %*)

:::::::::
goto :EOF
:::::::::
****************************************************************************************
:::::::::
:tee
:::::::::

for %%t in ("con" "%logfile%") do (echo:%* >>%%t)

:::::::::
goto :EOF
:::::::::
****************************************************************************************
:::::::::
:div
:::::::::

call :tee ------------------------------------------------------------------------------

:::::::::
goto :EOF
:::::::::
****************************************************************************************
:::::::::
:pad
:::::::::

if !%~1! LSS 100 set p.%~1=0
if !%~1! LSS 10 set p.%~1=00

:::::::::
goto :EOF
:::::::::
****************************************************************************************
:::::::::
:beep
:::::::::

for /l %%b in (%~1,-1,0) do (
    start /w /min "" cmd /c echo:<BELL Character - {Edited as the original breaks RSS}>
)

:::::::::
goto :EOF
:::::::::
****************************************************************************************
All patches are installed silently.

Last edited by Simon Sheppard (04 Jun 2010 00:10)

cmd | *sh | ruby | chef

----------------------------

#8 11 May 2010 11:00
arash
RG wrote:

arash,
Glad it does the job for you. Copy/Paste lines below so you don't miss anything.
Change first FOR line to:
FOR /F %%A IN ('DIR /B *.*') DO ECHO.%%A
Change second FOR line to
FOR /F %%A IN ('DIR /B *.*') DO "%%A" /passive
This will use the lines from the output of the DIR command. Use /B for bare format (no header and summary).
If you want you could add a space and /ON
after the /B on both FOR lines
This will force sorting alphabetically by name, but I think it should come out that way anyway.
See DIR /?
So, the bottom section of the code should looks like this

Code: Select all

ECHO.The following updates will be installed in the order shown:
ECHO.

FOR /F %%A IN ('DIR /B /ON *.exe') DO ECHO. %%A
ECHO.
ECHO.Press CTRL+C to exit or
PAUSE
FOR /F %%A IN ('DIR /B /ON *.exe') DO  %%A /passive /norestart


PAUSE
POPD
But there is another issue we have to take into consideration, it ignores any files which contains spaces in their names sad
I almost tried out every DIR arguments to cure the problem but I failed quite badly sad sad
Dear RG, would you solve this problem too? big_smile

----------------------------

#9 11 May 2010 11:16
RG


arash,
You missed the quotes around the %%A in the second FOR line
FOR /F %%A IN ('DIR /B /ON *.exe') DO "%%A" /passive /norestart
It would be a good idea to make that
FOR /F %%A IN ('DIR /B /ON KB*.exe') DO "%%A" /passive /norestart
as Bluesxman did so that you don't accidently execute some other .exe

Windows Shell Scripting and InstallShield

----------------------------

#10 11 May 2010 12:04
arash
RG wrote:

arash,
You missed the quotes around the %%A in the second FOR line
FOR /F %%A IN ('DIR /B /ON *.exe') DO "%%A" /passive /norestart
It would be a good idea to make that
FOR /F %%A IN ('DIR /B /ON KB*.exe') DO "%%A" /passive /norestart
as Bluesxman did so that you don't accidently execute some other .exe
Dear RG
I tried that out but it makes no diffrence at all if you put quotes around the %%A in this case.
For example I've got a file which name is "WindowsXP Installer 4.5-KB942288.exe"
but the batch shows it's name as "WindowsXP" and does not able to run it and then sends an error.
So I should remove all spaces in all files names manually which is not a clever idea.
I'm baffled why FOR /F %%A IN ('DIR /B /ON KB*.exe') DO "%%A" /passive /norestart doesn't work
whereas simple dir /b command in native dos would make it?

----------------------------

#11 11 May 2010 12:18
arash


Dear Bluesxman
That's awesome, it works quite nicely, you did it excellent too. :clap: :clap: :clap:
But would you modified it a little bit just to cover the following items:
1. it leaves all .exe files intact, I mean not to send them into Done folder
2. create log beside the batchfile, not on desktop
Thanx a lot mate

Last edited by arash (11 May 2010 14:45)

----------------------------

#12 11 May 2010 13:32
RG


arash,
My bad. Try this.
FOR /F "tokens=*" %%A IN ('DIR /B /ON KB*.exe') DO "%%A" /passive /norestart

Windows Shell Scripting and InstallShield

----------------------------

#13 11 May 2010 14:52
arash
RG wrote:

arash,
My bad. Try this.
FOR /F "tokens=*" %%A IN ('DIR /B /ON KB*.exe') DO "%%A" /passive /norestart
Sorry Dear RG, but it doesn't work either. Still files with spaces whithin their names are not supported sad
However I'd appreciate all your efforts RG.

----------------------------

#14 11 May 2010 15:42
RG


arash,
I shouldn't post without testing! The first FOR line should be changed to
FOR /F "tokens=*" %%A IN ('DIR /B /ON KB.exe') DO ECHO. %%A

Last edited by RG (11 May 2010 15:43)

Windows Shell Scripting and InstallShield

----------------------------

#15 11 May 2010 17:16
bluesxman
arash wrote:

Dear Bluesxman
That's awesome, it works quite nicely, you did it excellent too. :clap: :clap: :clap:
But would you modified it a little bit just to cover the following items:
1. it leaves all .exe files intact, I mean not to send them into Done folder
2. create log beside the batchfile, not on desktop
Thanx a lot mate
1. That so I don't try installing something I've already done. Not strictly necessary, but is a time saver. The change is easy to effect. This line:

Code: Select all

fsutil fsinfo drivetype %~d0 2>nul | find /i "CD-ROM" >nul && set mv=REM
Becomes:

Code: Select all

set mv=REM
2. Also easy to acheive. This line:

Code: Select all

set logfile=%userprofile%\Desktop\%~n0@%dttm%.log
Becomes:

Code: Select all

set logfile=%~dpn0@%dttm%.log
cmd | *sh | ruby | chef

----------------------------

#16 11 May 2010 17:36
arash
RG wrote:

arash,
I shouldn't post without testing! The first FOR line should be changed to
FOR /F "tokens=*" %%A IN ('DIR /B /ON KB.exe') DO ECHO. %%A
I did so and now it works, actually it works marvellously good :clap: :clap: :clap:
I am very delighted by the result. lol
Thank you dear RG, Thank you, Thank you.

----------------------------

#17 11 May 2010 18:18
arash


Dear Bluesxman
I'd apreciate your efforts too. :clap: :clap:
With your kind help, I've got not only one but two perfect solution for one of my biggest challenges. lol

----------------------------

#18 16 May 2010 22:42
arash


Hello guys
I've just noticed that somthing is wrong the following code

Code: Select all

FOR /F %%A IN ('DIR /B /ON *.exe') DO ECHO. %%A
This code works brilliantly, I mean it sorts all EXE files by name order until you put in "/s" swith to access subfolders too, then all things go mad, the batch ignores "/on" swith and sorts files by the date of creation not name!!! why?? :pc:

----------------------------

#19 17 May 2010 01:39
RG


arash,
Try this.

Code: Select all

FOR /F "tokens=*" %%A IN ('DIR /B /S /ON *.exe') DO ECHO. %%A
It works fine on my Vista machine. You need the "tokens=*" so that the entire line becomes 1 token. Without it you will have trouble if there are any spaces or other delimiters in the folder/filenames.
As far as sorting.... are you on an NTFS drive? I have had trouble with sorting files on FAT32 drives.

Windows Shell Scripting and InstallShield

----------------------------

#20 17 May 2010 08:33
arash


Dear RG
I'm using windows XP and all of my drives are on FAT32, and there are spaces in subfolders name too.
Whenever you insert "/s" swith in "DIR" command, the batch does not apply "/o:n" swith even if you have already put it in "DIR" command.

Last edited by arash (17 May 2010 08:38)

----------------------------

#21 17 May 2010 21:46
arash


Dear RG
I do really appreciate your generous help.
Thanx a million

----------------------------

#22 20 May 2010 17:43
RG


arash,
Don't know what to tell you. Got my XP box back today and it seems to sort just fine using the above commands on NTFS or FAT32 drives.
Post Reply