# Solved: Batch File action



## ibo (Nov 2, 2007)

Is it possible to make a batch file in Windows 7 that would be able to do these 4 actions:

1. Change the Appearence theme, to a specified one.

2. Open up the Screen resolution window and leave it open.

3. Launch an application.

4. When application in step 3 is closed by user, then the resolution window closes, and the Appearence theme is changed back to original one.


----------



## ibo (Nov 2, 2007)

Step 2 would be "control desk.cpl" correct? The first half of Step 1 would be "control desk.cpl,,2", but I have no idea about the rest.


----------



## Keebellah (Mar 27, 2008)

the 3d could be start or run or just the "<application path and name>"


----------



## TheOutcaste (Aug 8, 2007)

For the ScreenResolution, use this:
%systemroot%\system32\control.exe desk.cpl,Settings,@Settings
There's no easy way to close the Personalization and Screen Resolution windows, unless you have MS Word installed, then it's easy. Even PowerShell doesn't have an easy way to close those windows, at least not that I've found yet, they aren't listed as processes, so you can't use Close(), Quit(), or Kill()

This works, but *only if you have MS-Word installed*.

The one glitch I've not solved is the application always opens _behind_ the Screen Resolution window. I even tried a 30 second delay after opening the Screen Resolution window, and it still wouldn't open on top, tried both Notepad and Calculator. If you click on another window, like the Command prompt, it will open the application on top, but not if you just click the Screen Resolution window.

```
@Echo Off
:: Save Current THeme
For /F "Tokens=2*" %%I In ('Reg Query "HKCU\Software\Microsoft\Windows\CurrentVersion\Themes" /V CurrentTheme') Do Set _CurrentTheme=%%J
:: These are paths to the Current User;s themes, and the Windows themes. Choose the one you want to use when you builed the _ThemePath variable
:: If you have a different folder where themes are stored, use that path
Set _UserThemPath=%LocalAppData%\Microsoft\Windows\Themes\
Set _WinThemePath=C:\Windows\Resources\Themes\
Set _RunApp=%SystemRoot%\Notepad.exe
Set _ThemeName=characters.theme
Set _ThemePath=%_WinThemePath%%_ThemeName%
:::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Do not Edit below this line
:::::::::::::::::::::::::::::::::::::::::::::::::::::
Call :_Setup
:: Change Theme
rundll32.exe Shell32.dll,Control_RunDLL desk.cpl desk,@Themes /Action:OpenTheme /File:"%_ThemePath%"
:: Close Personalization Window
Cscript //nologo %_VBFile% "%_Win1%"
:: Open Screen Resolution Window
"%systemroot%\system32\control.exe" desk.cpl,Settings,@Settings
:: Start App and wait for it to finish
Start "" /Wait "%_RunApp%"
:: Close Screen Resolution Window
Cscript //nologo %_VBFile% "%_Win2%"
:: Restore original THeme
rundll32.exe Shell32.dll,Control_RunDLL desk.cpl desk,@Themes /Action:OpenTheme /File:"%_CurrentTheme%"
:: Close Personalization Window
Cscript //nologo %_VBFile% "%_Win1%"
Del "%_VBFile%"
Goto :EOF
::::::::::::::::::::::::::::::::::::::::::::::::::::::
::           Subroutines
::::::::::::::::::::::::::::::::::::::::::::::::::::::
:_Setup
Set _Win1=Personalization
Set _Win2=Screen Resolution
Set _VBFile=%Temp%\[email protected]
:: Create VBScript in Temp folder
(Echo.On Error Resume Next
Echo.strTitle = WScript.Arguments.Item^(0^)
Echo.' Create a Word object
Echo.Set objWord = CreateObject^("Word.Application"^)
Echo.' Generate a collection containing the open windows 
Echo.Set colTasks = objWord.Tasks
Echo.' Search for the windows title that was passed as parameter
Echo.For Each objTask In colTasks
Echo.If objTask.Name = strTitle Then colTasks^(objTask.Name^).Close
Echo.Next
Echo.objWord.Quit)>"%_VBFile%"
```
Might find these links useful
Executing Control Panel Items
Windows 7 Control Panel Canonical Names


----------



## ibo (Nov 2, 2007)

When I do the following

```
Set _WinThemePath=C:\Windows\Resources\Themes\aero
```
an error states: "Theme cannot be applied. Try clicking a different theme."

Did I do anything wrong?

Mad skills for you:up:


----------



## TheOutcaste (Aug 8, 2007)

Does that folder actualy contain the *.theme* file for the theme you are trying to load, the one named on the *Set _ThemeName=* line?

On my system, the Aero folder just has support files for the aero theme, the theme itself is in *C:\Windows\Resources\Themes\*
If you are wanting to choose the aero theme, those two lines should read like this:

```
Set _WinThemePath=C:\Windows\Resources\Themes\
Set _ThemeName=aero.theme
```


----------



## ibo (Nov 2, 2007)

Fixed the theme thanks.

How would you delay the program to open 5 seconds later?


----------



## TheOutcaste (Aug 8, 2007)

Add *Timeout /T 5 /NOBREAK* right before the Start command that starts the program.
A keypress will end the timer if you leave off the */NOBREAK* switch. Time can be up to 99,999 seconds (27 hours, 46 minutes, 39 seconds)


```
:: Start App and wait for it to finish
Timeout /T 5 /NOBREAK
Start "" /Wait "%_RunApp%"
```


----------



## ibo (Nov 2, 2007)

Alright, one last thing, and this thread will be marked "Solved".

The application which I am trying to run just won't start.


```
C:\Program Files\Microsoft Games\Age of Empires 2\Age2_x1.exe
```
Me thinks that the application is not the original .exe, which is nowhere to be found. I remember, this game had a couple of expansions, and if you want to play it online, you can switch from the two versions the game has. In the same directory there is a AOCCONFIG, where you actually choose the expansion, and then you run Age2_x1.exe to start the game. I cannot start the application with a batch file - Why?


----------



## TheOutcaste (Aug 8, 2007)

Make sure you didn't add quotes on the *Set _RunApp* line. The quotes will be added on the Start line. We don't want them twice:

```
Set _RunApp=C:\Program Files\Microsoft Games\Age of Empires 2\Age2_x1.exe
Start "" /Wait [COLOR=Red][B]"[/B][/COLOR]%_RunApp%[COLOR=Red][B]"[/B][/COLOR]
```
If you open a Command Prompt (should open in C:\Documents and Settings\<User Name>) and type that line, does the program run?
If not, switch to the Program folder and try it:
Type these two lines:

```
CD "C:\Program Files\Microsoft Games\Age of Empires 2:
Age2_x1.exe
```
If it runs from a Start Menu shortcut, right click the shortcut and click *Properties*. What is in the *Target* box? It may need some switches added to configure the program.


----------



## ibo (Nov 2, 2007)

So, the target is C:\Program Files\Microsoft Games\Age of Empires 2\Age2_x1\Age2_x1.exe

When I run the batch file, the execution does not wait for the program to start - there is a little screen that pops up when the game starts but then the themes just change back ot original.

This is the .txt file as it is:


```
@Echo Off
:: Save Current THeme
For /F "Tokens=2*" %%I In ('Reg Query "HKCU\Software\Microsoft\Windows

\CurrentVersion\Themes" /V CurrentTheme') Do Set _CurrentTheme=%%J
:: These are paths to the Current User;s themes, and the Windows themes. Choose 

the one you want to use when you builed the _ThemePath variable
:: If you have a different folder where themes are stored, use that path
Set _UserThemPath=%LocalAppData%\Microsoft\Windows\Themes\
Set _RunApp=C:\Program Files\Microsoft Games\Age of Empires 

2\Age2_x1\Age2_x1.exe
Start "" /Wait "%_RunApp%" 
Set _WinThemePath=C:\Windows\Resources\Ease of Access Themes\
Set _ThemeName=classic.theme
Set _ThemePath=%_WinThemePath%%_ThemeName%
:::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Do not Edit below this line
:::::::::::::::::::::::::::::::::::::::::::::::::::::
Call :_Setup
:: Change Theme
rundll32.exe Shell32.dll,Control_RunDLL desk.cpl desk,@Themes /Action:OpenTheme 

/File:"%_ThemePath%"
:: Close Personalization Window
Cscript //nologo %_VBFile% "%_Win1%"
:: Open Screen Resolution Window
"%systemroot%\system32\control.exe" desk.cpl,Settings,@Settings
:: Start App and wait for it to finish
Start "" /Wait "%_RunApp%"
:: Close Screen Resolution Window
Cscript //nologo %_VBFile% "%_Win2%"
:: Restore original THeme
rundll32.exe Shell32.dll,Control_RunDLL desk.cpl desk,@Themes /Action:OpenTheme 

/File:"%_CurrentTheme%"
:: Close Personalization Window
Cscript //nologo %_VBFile% "%_Win1%"
Del "%_VBFile%"
Goto :EOF
::::::::::::::::::::::::::::::::::::::::::::::::::::::
::           Subroutines
::::::::::::::::::::::::::::::::::::::::::::::::::::::
:_Setup
Set _Win1=Personalization
Set _Win2=Screen Resolution
Set _VBFile=%Temp%\[email protected]
:: Create VBScript in Temp folder
(Echo.On Error Resume Next
Echo.strTitle = WScript.Arguments.Item^(0^)
Echo.' Create a Word object
Echo.Set objWord = CreateObject^("Word.Application"^)
Echo.' Generate a collection containing the open windows 
Echo.Set colTasks = objWord.Tasks
Echo.' Search for the windows title that was passed as parameter
Echo.For Each objTask In colTasks
Echo.If objTask.Name = strTitle Then colTasks^(objTask.Name^).Close
Echo.Next
Echo.objWord.Quit)>"%_VBFile%"
```
And yes, I can run it perfectly from DOS.


----------



## TheOutcaste (Aug 8, 2007)

Sounds like the Age2_x1.exe file is just a loader that starts the game then exits, so the batch file continues.

Two options come to mind, one is to just add a pause statement, so when you finish the game, you'd switch to the open command prompt and press enter, or you'd need to find the name of the actual process that the game runs.
The batch file can check to see if that process is still running every few seconds, and when it quits it will continue. You'd need to check the list of processes in Task Manager before the game starts, then while it's running to find the Image name of the actual game process.

Then Modify the file to add this, using the correct Image name instead of *Age2_x1.exe*:

```
Start "" /Wait "%_RunApp%"
>Nul Timeout -t 60
:_Loop
Tasklist /FI "IMAGENAME eq [COLOR=Red][B]Age2_x1.exe[/B][/COLOR]"&&Goto _GameOver
>Nul Timeout -t 5
Goto _Loop
:_GameOver
:: Close Screen Resolution Window
Cscript //nologo %_VBFile% "%_Win2%"
```


----------



## Keebellah (Mar 27, 2008)

I suggest you add a pause after the line where you Start the program

I imagine that you are missing a startup parameter, I don't have it installed and cannot execute it here but I did try other programs and I think that you should take a look at that.
If the program does require a startup parameter then you will have to edit the variable:

My vision of how the code should be written:


```
[COLOR="Red"]previous[/COLOR]
Set _RunApp=C:\Program Files\Microsoft Games\Age of Empires 2\Age2_x1\Age2_x1.exe
Start "" /Wait "%_RunApp%" 

[COLOR="red"]my suggestion[/COLOR]
Set _RunApp="C:\Program Files\Microsoft Games\Age of Empires 2\Age2_x1\Age2_x1.exe"  param1  param2 etc...
Start "" /Wait %_RunApp%
```
The fact is that the executable must be surroudend by quotes because of the spaces in the folder names but the parameters must be kept outside the quotes.

The link below shows some startup parameters but I don't know if it's related to your version:
http://aoe.heavengames.com/academy/hotkeysandcommands/cmdline.shtml

I hope some of this helps


----------



## ibo (Nov 2, 2007)

Now, I wanted to go with the second option, since I wanted everything automatically. I do not mind switchingand pressing spacebar, but wanted to try this way first.

Thelauncher is launched twice: first before the theme changes and then after. Still, the game does not run. It also leavs it at classic theme.

I feel like I didn't copy the code correctly. Is it what you meant?


```
@Echo Off
:: Save Current THeme
For /F "Tokens=2*" %%I In ('Reg Query "HKCU\Software\Microsoft\Windows

\CurrentVersion\Themes" /V CurrentTheme') Do Set _CurrentTheme=%%J
:: These are paths to the Current User;s themes, and the Windows themes. Choose 

the one you want to use when you builed the _ThemePath variable
:: If you have a different folder where themes are stored, use that path
Set _UserThemPath=%LocalAppData%\Microsoft\Windows\Themes\
Set _RunApp=C:\Program Files\Microsoft Games\Age of Empires 

2\Age2_x1\Age2_x1.exe
Start "" /Wait "%_RunApp%" 
Set _WinThemePath=C:\Windows\Resources\Ease of Access Themes\
Set _ThemeName=classic.theme
Set _ThemePath=%_WinThemePath%%_ThemeName%
:::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Do not Edit below this line
:::::::::::::::::::::::::::::::::::::::::::::::::::::
Call :_Setup
:: Change Theme
rundll32.exe Shell32.dll,Control_RunDLL desk.cpl desk,@Themes /Action:OpenTheme 

/File:"%_ThemePath%"
:: Close Personalization Window
Cscript //nologo %_VBFile% "%_Win1%"
:: Open Screen Resolution Window
"%systemroot%\system32\control.exe" desk.cpl,Settings,@Settings

:: Start App and wait for it to finish
Start "" /Wait "%_RunApp%"
>Nul Timeout -t 60
:_Loop
Tasklist /FI "IMAGENAME eq age2_x1 *32"&&Goto _GameOver
>Nul Timeout -t 5
Goto _Loop
:_GameOver


:: Close Screen Resolution Window
Cscript //nologo %_VBFile% "%_Win2%"
:: Restore original THeme
rundll32.exe Shell32.dll,Control_RunDLL desk.cpl desk,@Themes /Action:OpenTheme 

/File:"%_CurrentTheme%"
:: Close Personalization Window
Cscript //nologo %_VBFile% "%_Win1%"
Del "%_VBFile%"
Goto :EOF
::::::::::::::::::::::::::::::::::::::::::::::::::::::
::           Subroutines
::::::::::::::::::::::::::::::::::::::::::::::::::::::
:_Setup
Set _Win1=Personalization
Set _Win2=Screen Resolution
Set _VBFile=%Temp%\[email protected]
:: Create VBScript in Temp folder
(Echo.On Error Resume Next
Echo.strTitle = WScript.Arguments.Item^(0^)
Echo.' Create a Word object
Echo.Set objWord = CreateObject^("Word.Application"^)
Echo.' Generate a collection containing the open windows 
Echo.Set colTasks = objWord.Tasks
Echo.' Search for the windows title that was passed as parameter
Echo.For Each objTask In colTasks
Echo.If objTask.Name = strTitle Then colTasks^(objTask.Name^).Close
Echo.Next
Echo.objWord.Quit)>"%_VBFile%"
```


----------



## ibo (Nov 2, 2007)

Keebellah said:


> I suggest you add a pause after the line where you Start the program
> 
> I imagine that you are missing a startup parameter, I don't have it installed and cannot execute it here but I did try other programs and I think that you should take a look at that.
> If the program does require a startup parameter then you will have to edit the variable:
> ...


That is the older game - Age of Empires 1. This is 2. I don't know it they could be the same though.


----------



## TheOutcaste (Aug 8, 2007)

You copied it correctly, but Tasklist doesn't set the errorlevel the way I thought, so it won't work. That's what I get for not actually testing it

The image name shown in Task Manager should have an *.exe* extension. Even with extensions hidden it should still appear. Also the **32* part should not be included, it's not part of the file name, but is displayed by Task Manager to indicate a 32 bit program. Didn't realize you had a 64 bit version of Windows.

So when the game is running, there are no other processes besides the *Age2_x1.exe*?
Does it appear more than once, or does the PID number change? (In task Manage, *View | Choose Columns* if the PID column is not visible)
I'm thinking it may read the AOCCONFIG, then restart itself with a command line switch to load the expansion.

This code should work, it will loop as long as the file specified on the *Set _IM* line appears in Task Manager. May need to adjust the first timeout (the 60) so that the game has time to start if it's restarting the *Age2_x1.exe* file.

```
>Nul Timeout -t 60
Set _Im=Age2_x1.exe
:_Loop
Tasklist /FI "IMAGENAME eq %_Im%"|Findstr /I "%_Im%">Nul||Goto _GameOver
>Nul Timeout -t 5
Goto _Loop
:_GameOver
```
One other thing, the code you posted shows some of the long lines as being separate lines instead of a single line. If you are using Notepad and have Word Wrap turned on, it will show it that way when you copy and paste to the forum. I'd like to double check that none of the lines actually get split, so turn off Word Wrap (*Format | Word Wrap*), then copy and paste to the forum again. Then you can turn Word Wrap back on.


----------



## Keebellah (Mar 27, 2008)

kay, even if it's an older game, have you tried to write a bacth file with the following parameters:


```
Set _RunApp="C:\Program Files\Microsoft Games\Age of Empires 2\Age2_x1\Age2_x1.exe"  param1  param2 etc...
Start "" /Wait %_RunApp%
```
If the game starts then you know that that part works and all you will have to figure out is why the theme doen't change, but I think it does. The only one is that the when you run an executable from a batchfile it will continue the rest of the code lines so that way the theme is changed back to the original one.

I think you will have to write 2 sets of code, 
- one to start a new game, if the game is not already loaded then setup the temporary theme and start the game nothing more
- second game end, code must check if the game is in memory, if it isn't then restore original theme

Our batch file experts will find a way go to.

I hope this puts you on a new, hopefully correct, track


----------



## ibo (Nov 2, 2007)

```
@Echo Off
:: Save Current THeme
For /F "Tokens=2*" %%I In ('Reg Query "HKCU\Software\Microsoft\Windows\CurrentVersion\Themes" /V CurrentTheme') Do Set _CurrentTheme=%%J
:: These are paths to the Current User;s themes, and the Windows themes. Choose the one you want to use when you builed the _ThemePath variable
:: If you have a different folder where themes are stored, use that path
Set _UserThemPath=%LocalAppData%\Microsoft\Windows\Themes\
Set _RunApp=C:\Program Files\Microsoft Games\Age of Empires 2\Age2_x1\Age2_x1.exe
Start "" /Wait "%_RunApp%" 
Set _WinThemePath=C:\Windows\Resources\Ease of Access Themes\
Set _ThemeName=classic.theme
Set _ThemePath=%_WinThemePath%%_ThemeName%
:::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Do not Edit below this line
:::::::::::::::::::::::::::::::::::::::::::::::::::::
Call :_Setup
:: Change Theme
rundll32.exe Shell32.dll,Control_RunDLL desk.cpl desk,@Themes /Action:OpenTheme /File:"%_ThemePath%"
:: Close Personalization Window
Cscript //nologo %_VBFile% "%_Win1%"
:: Open Screen Resolution Window
"%systemroot%\system32\control.exe" desk.cpl,Settings,@Settings

:: Start App and wait for it to finish
Start "" /Wait "%_RunApp%"
>Nul Timeout -t 60
Set _Im=Age2_x1.exe
:_Loop
Tasklist /FI "IMAGENAME eq %_Im%"|Findstr /I "%_Im%">Nul||Goto _GameOver
>Nul Timeout -t 5
Goto _Loop
:_GameOver



:: Close Screen Resolution Window
Cscript //nologo %_VBFile% "%_Win2%"
:: Restore original THeme
rundll32.exe Shell32.dll,Control_RunDLL desk.cpl desk,@Themes /Action:OpenTheme /File:"%_CurrentTheme%"
:: Close Personalization Window
Cscript //nologo %_VBFile% "%_Win1%"
Del "%_VBFile%"
Goto :EOF
::::::::::::::::::::::::::::::::::::::::::::::::::::::
::           Subroutines
::::::::::::::::::::::::::::::::::::::::::::::::::::::
:_Setup
Set _Win1=Personalization
Set _Win2=Screen Resolution
Set _VBFile=%Temp%\[email protected]
:: Create VBScript in Temp folder
(Echo.On Error Resume Next
Echo.strTitle = WScript.Arguments.Item^(0^)
Echo.' Create a Word object
Echo.Set objWord = CreateObject^("Word.Application"^)
Echo.' Generate a collection containing the open windows 
Echo.Set colTasks = objWord.Tasks
Echo.' Search for the windows title that was passed as parameter
Echo.For Each objTask In colTasks
Echo.If objTask.Name = strTitle Then colTasks^(objTask.Name^).Close
Echo.Next
Echo.objWord.Quit)>"%_VBFile%"
```
No change.


----------



## TheOutcaste (Aug 8, 2007)

If you open a Command Prompt, can you start the game by typing this (include the quotes):

```
"C:\Program Files\Microsoft Games\Age of Empires 2\Age2_x1\Age2_x1.exe"
```
If not, type this:

```
CD "C:\Program Files\Microsoft Games\Age of Empires 2\Age2_x1"
Age2_x1.exe
```
Does that start the game?


----------



## ibo (Nov 2, 2007)

Yes, it starts with this.


```
CD "C:\Program Files\Microsoft Games\Age of Empires 2\Age2_x1"
Age2_x1.exe
```
You saw pm?


----------



## TheOutcaste (Aug 8, 2007)

Yes I did.
OK, looks like it needs the working directory changed so it can read the configuration file.
Add the _RunPath variable

```
Set _RunPath=C:\Program Files\Microsoft Games\Age of Empires 2\Age2_x1
Set _RunApp=C:\Program Files\Microsoft Games\Age of Empires 2\Age2_x1\Age2_x1.exe
```
Remove the start command that is right below *Set _RunApp*
Change the Start command farther down in the file to this:

```
Start "" /Wait /D "%_RunPath%" "%_RunApp%"
```
See if that gets this working.

Jerry


----------



## ibo (Nov 2, 2007)

Nicely DONE! Thanks a lot man.


```
>Nul Timeout -t 60
Set _Im=Age2_x1.exe
:_Loop
Tasklist /FI "IMAGENAME eq %_Im%"|Findstr /I "%_Im%">Nul||Goto _GameOver
>Nul Timeout -t 5
Goto _Loop
:_GameOver
```
I'm guessing this is the part of the code that check if the program is still running, correct?
Which number do I have to change ,if I want to modify it, to check more frequently, in order to minimize the time after I have closed the game and it changes themes and closes windows?


----------



## TheOutcaste (Aug 8, 2007)

>Nul Timeout -t *5*

The *5* is 5 seconds, 1 second will be the shortest you can set.

Keep forgetting the Start command doesn't change the working directory without the /D switch, sorry about that.


----------



## ibo (Nov 2, 2007)

Thanks man.


----------

