Mattia Minervini
asked on
SImple batch to switch config.xml OFFLINE or ONLINE
hi
i have an application that can work in ONLINE mode and OFFLINE mode
When application starts, it read a config.xml where it find parameters to start offline or online
My goal is make two small batch file for my user so it can change to OFFLINE and ONLINE without make complex change on xml file
something like put 3 file in directory C:\application:
Config.xml (really used)
ConfigONLINE.xml
ConfigOFFLINE.xml
ONLINE and OFFLINE is only a copy oh the same file with different parameters
My problem is not make mistake with file in use, so batch file has to rename and manage file in the right way!
Checking some condition in order to understand if now is ONLINE or OFFLINE
Example -> two batch rename other with _NOUSED, and two batch when start check if any files are named with _NOUSED in order to understand which is used!
Please help! Thanks!
M
i have an application that can work in ONLINE mode and OFFLINE mode
When application starts, it read a config.xml where it find parameters to start offline or online
My goal is make two small batch file for my user so it can change to OFFLINE and ONLINE without make complex change on xml file
something like put 3 file in directory C:\application:
Config.xml (really used)
ConfigONLINE.xml
ConfigOFFLINE.xml
ONLINE and OFFLINE is only a copy oh the same file with different parameters
My problem is not make mistake with file in use, so batch file has to rename and manage file in the right way!
Checking some condition in order to understand if now is ONLINE or OFFLINE
Example -> two batch rename other with _NOUSED, and two batch when start check if any files are named with _NOUSED in order to understand which is used!
Please help! Thanks!
M
ASKER
i can't change application
user know they have to run batch with application closed
Being a config file read on startup, if they run batch witj applicatione started, there is no problem (application not change in real time from online to offline, but only on a second launch)
user know they have to run batch with application closed
Being a config file read on startup, if they run batch witj applicatione started, there is no problem (application not change in real time from online to offline, but only on a second launch)
E.g. (untested)
As batch (untested):
p.s. in line 47 of the PS script, we need a Copy-Item instead of Move-Item.
<#
.SYNOPSIS
Application mode launcher.
.DESCRIPTION
Start the application using the correct mode depended configuration file.
.PARAMETER Offline
Copy the OFFLINE mode configuration and start application.
.PARAMETER Online
Copy the ONLINE mode configuration and start application.
.EXAMPLE
{NameOfPowerShellScipt} -Online
{NameOfPowerShellScipt} -Offline
#>
Param(
[switch] $Offline = $False,
[switch] $Online = $False,
)
#Requires -Version 5.0
Set-StrictMode -Version 2
Function ScriptMain() {
If (-Not ($Offline -Or $Online)) {
Get-Help $Script:PSCommandPath -Detailed
Exit 0
}
# Adjust paths, file names and mode indcators.
$APPLICATION_PATH = 'C:\Program Files\Application\Application.exe'
$DESTINATION_PATH = 'C:\Temp\config.xml'
$SEMAPHORE_PARTIAL_PATH = 'C:\Temp\Mode.'
$SOURCE_BASE_PATH = 'C:\Temp\'
If ($Offline) {
$SemaphorePath = $SEMAPHORE_PARTIAL_PATH + 'Offline'
$SourcePath = $SOURCE_BASE_PATH + 'OFFLINE.XML'
}
If ($Online) {
$SemaphorePath = $SEMAPHORE_PARTIAL_PATH + 'Online'
$SourcePath = $SOURCE_BASE_PATH + 'ONLINE.XML'
}
# Delete old semaphore, copy required configuration, create matching semaphore, start application.
Remove-Item -Path $SEMAPHORE_PARTIAL_PATH + '*'
Copy-Item -Path $SourcePath -Destination $DESTINATION_PATH -Force
New-Item -Path SemaphorePath -ItemType File
Start-Process $APPLICATION_PATH -PassThru
}
ScriptMain
Run it e.g. as PowerShell.exe PathToScript\script.ps1 -Offine.As batch (untested):
@Echo Off
SetLocal
Set APPLICATION_PATH=C:\Program Files\Application\Application.exe
Set DESTINATION_PATH="C:\Temp\config.xml"
Set EXECUTE=0
Set MODE=%~1
Set SEMAPHORE_PARTIAL_PATH="C:\Temp\Mode."
Set SOURCE_BASE_PATH="C:\Temp\"
If [%MODE%] Equ [Offline] (
Set EXECUTE=1
Set %SemaphorePath%="%SEMAPHORE_PARTIAL_PATH%Offline"
Set %SourcePath%="%SOURCE_BASE_PATH%OFFLINE.XML"
)
If [%MODE%] Equ [Online] (
Set EXECUTE=1
Set %SemaphorePath%="%SEMAPHORE_PARTIAL_PATH%Online"
Set %SourcePath%="%SOURCE_BASE_PATH%ONLINE.XML"
)
If %EXECUTE% Equ 1 (
Rem Delete old semaphore, copy required configuration, create matching semaphore, start application.
Del "$SEMAPHORE_PARTIAL_PATH*"
Copy "$SourcePath" "$DESTINATION_PATH"
Echo "Config copied." >> %SemaphorePath%
Start "Your Application" %APPLICATION_PATH%
)
EndLocal
Run as e.g. cmd.exe batch.cmd offline.p.s. in line 47 of the PS script, we need a Copy-Item instead of Move-Item.
ASKER
I really appreciate your answer, now i've just solved in this brutal way (see jpeg)
but i will test and stock your code for further use
Thanks!
but i will test and stock your code for further use
Thanks!
Seems like you could have two folders, one with a copy of the files for OFFLINE mode, one with a copy for ONLINE mode. And then a simple text file in the base folder of these that contains either "ONLINE" or "OFFLINE" on a single line, indicating the current mode. Then a small BAT script file could look at that text file, see what mode we are in, copy the files from the other mode subfolder to the production files location, and rewrite the "control" text file with the new mode.
Naturally this all assumes you can do this "on the fly" without any other actions related to "stopping the application", etc. And the set of files will not all be copied at the exact same instant, but if they aren't too large it should be pretty close.
If you think this works I could work up the small BAT file, unless you can handle that?
»bp
Naturally this all assumes you can do this "on the fly" without any other actions related to "stopping the application", etc. And the set of files will not all be copied at the exact same instant, but if they aren't too large it should be pretty close.
If you think this works I could work up the small BAT file, unless you can handle that?
»bp
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Besides that, to do this securely, you need to
1) ensure that no instance of this application is running
2) copy your desired mode file
3) start the application
Problems are, when it needs to be waterproof:
It can be hard to detect and killing all running instances. Here everything depends on the actual application you're talking about.
This kind of processing must be sequential. Thus you need to ensure that this batch is run as singleton.
When you want to keep it simple and the possible problems from above are none, then the approach is for Run OFFLINE and Run ONLINE
1) delete existing semaphores
2) copy your desired mode file
3) create a semaphore with your mode as part of its name
4) start the application
For Toggle mode and Run:
1) read semaphore, detected old mode
2) delete old mode semaphore
3) copy the new mode file
4) create new mode semaphore
5) start the application
Renaming is not necessary.
But when you control that application source: Change the application to support mode switching itself.