Renaming the current process of a ClickOnce application programmatically - vb.net

I have a ClickOnce application called "Ulti", and I need to run three of this application at once due to my project requirements. During the course of the application, there is a possibility that one or more copies of the application will close due to certain requirements being met.
I have a .bat file scheduled to run every 10 minutes; if not all three copies of the application are running, it should open another instance of the application. However because all three processes of the application are all named "Ulti", my .bat file is unable to differentiate whether or not there are 3 copies of it running, and if not which one is shut down.
As such, is it possible to rename the process of a ClickOnce application dynamically via code at runtime?

You can't rename a process name in runtime. But if your bat file is processes starter you can pass some "Fake" parameter like "1", "2" etc, to your application. It hasn't any effect for your application, but you can differentiate your process by parse command line.
To get process command line parameters you can use WMIC /?. Here is an example:
#echo off
SET PROCESSNAME=notepad.exe
SET PROCESSDIR=C:\Windows
SET PROCESSFULLPATH=%PROCESSDIR%\%PROCESSNAME%
SET WMIC=C:\Windows\System32\wbem\WMIC.exe
SET PID1=1
SET PID2=2
SET PID3=3
REM Prepare
taskkill /IM notepad.exe
CLS
REM 3 Fake notepads
start /b /d "%PROCESSDIR%" %PROCESSNAME% %PID1%
start /b /d "%PROCESSDIR%" %PROCESSNAME% %PID2%
start /b /d "%PROCESSDIR%" %PROCESSNAME% %PID3%
echo You have a time for manual kill some notepad
pause
REM Restart if not founded 1 or 2 or 3 process.
REM 2nd loop doesn't working
REM Process 1
SET PROCESSID=%PID1%
SET KILLED=1
FOR /F "tokens=2" %%x IN ('%WMIC% /OUTPUT:STDOUT process where Name^="%PROCESSNAME%" get Commandline ^|find "%PROCESSNAME%"') DO (
IF "%%x"=="%PROCESSID%" (
SET KILLED=0
)
)
IF %KILLED%==1 (
echo Process %PROCESSID% killed. Restarting...
REM Restart dead copy with %KILLED% index
start /b /d "%PROCESSDIR%" %PROCESSNAME% %PROCESSID%
)
REM Process 2
SET PROCESSID=%PID2%
SET KILLED=1
FOR /F "tokens=2" %%x IN ('%WMIC% /OUTPUT:STDOUT process where Name^="%PROCESSNAME%" get Commandline ^|find "%PROCESSNAME%"') DO (
IF "%%x"=="%PROCESSID%" (
SET KILLED=0
)
)
IF %KILLED%==1 (
echo Process %PROCESSID% killed. Restarting...
REM Restart dead copy with %KILLED% index
start /b /d "%PROCESSDIR%" %PROCESSNAME% %PROCESSID%
)
REM Process 3
SET PROCESSID=%PID3%
SET KILLED=1
FOR /F "tokens=2" %%x IN ('%WMIC% /OUTPUT:STDOUT process where Name^="%PROCESSNAME%" get Commandline ^|find "%PROCESSNAME%"') DO (
IF "%%x"=="%PROCESSID%" (
SET KILLED=0
)
)
IF %KILLED%==1 (
echo Process %PROCESSID% killed. Restarting...
REM Restart dead copy with %KILLED% index
start /b /d "%PROCESSDIR%" %PROCESSNAME% %PROCESSID%
)

Related

batch file to start Catalina.bat file

I want to create batch file to start/stop catalina.bat file on window server.
#echo off
cls
cd D:\apache-tomcat-7.0.75-windows-x86\apache-tomcat-7.0.75\bin
catalina.bat start
this is what I create but not working.
If you type CD /? at the command prompt you'll note it has a /D option for changing drives.
You could therefore try:
#Echo Off
ClS
CD /D "D:\apache-tomcat-7.0.75-windows-x86\apache-tomcat-7.0.75\bin"
catalina.bat start <args>
If you don't need to have your working directory as the \bin location you could just use:
#Echo Off
ClS
"D:\apache-tomcat-7.0.75-windows-x86\apache-tomcat-7.0.75\bin\catalina.bat" start <args>
To stop it, repeat the last line, ending it with stop instead of start
Edit
If you really do need to use it, and given that you said the Call command works, you could useā€¦
Either:
#Echo Off
ClS
Rem start it
Call "D:\apache-tomcat-7.0.75-windows-x86\apache-tomcat-7.0.75\bin\catalina.bat" start <args>
Rem Do some other stuff
Timeout 120 >Nul
Rem stop it
Call "D:\apache-tomcat-7.0.75-windows-x86\apache-tomcat-7.0.75\bin\catalina.bat" stop
Or:
#Echo Off
ClS
Rem Make \bin directory current
CD /D "D:\apache-tomcat-7.0.75-windows-x86\apache-tomcat-7.0.75\bin"
Rem start it
Call catalina.bat start <args>
Rem Do some other stuff
Timeout 120 >Nul
Rem stop it
Call catalina.bat stop
If you want to create a .bat file to start your Tomcat, then here it goes:
Declare the JAVA_HOME & CATALINA _HOME path in the system environment variables.
Create a file in Notepad with the .bat extension and put the following code within the respective Tomcat directory:
set JPDA_ADDRESS=8000
set JPDA_TRANSPORT=dt_socket
C:
cd C:\Program Files\Apache Software Foundation\Tomcat 9.0 //Enter Your Tomacat Path
call .\bin\catalina jpda start
Check out the image attached for more detailed explanation.

Batch scripting for executing SQL scripts in a directory

I want to develop a batch script which will execute each .sql SQL scripts present in the folder where the batch file is placed to and record the logs to a filename_sqloutput.txt file .
The condition is : If any script gives any error message like column name incorrect , or table name etc. The script execution should stop immediately and further scripts should not executed .
I tried with the below code: But its not working as even if the script is giving errors in the output file . The script execution is not stopping ..
Need your help !!!
#echo off
for /f %%a IN ('dir /b *.sql') do (call sqlcmd -S AMRVSP000000318 -i %%a -o"%%~na_sqloutput.txt"
findstr "Msg" %%~na_sqloutput.txt >nul & if %errorlevel% EQU 1 (exit) else (echo Successfully executed %%~na_sqloutput.txt)
)
pause
FINDSTR will set %ERRORLEVEL% as follows:
0 (False) a match is found in at least one line of at least one file.
1 (True) if a match is not found in any line of any file, (or if the file is not found at all).
2 Wrong syntax
An invalid switch will only print an error message in error stream.
Moreover, follow EnableDelayedExpansion article and rewrite the script to more readable structure:
#echo off
SETLOCAL EnableExtensions EnableDelayedExpansion
for /f %%a IN ('dir /b *.sql') do (
call sqlcmd -S AMRVSP000000318 -i %%a -o"%%~na_sqloutput.txt"
findstr "Msg" "%%~na_sqloutput.txt" 2>NUL
if !errorlevel! EQU 0 (
echo error occured %%~na_sqloutput.txt
rem pause to see error
pause
exit /B
) else (
echo Successfully executed %%~na_sqloutput.txt
)
)
pause
.... if errorlevel 1 (exit...
%errorlevel% is the initial value of errorlevel as it stands when the for is encountered.
See endless SO articles about using delayed expansion as another solution.
This syntax means "if the current errorlevel is 1 or greater dothis else dothat"

Populate a batch from a file

I have been trying to work this 1 out it's a fairly simple batch but I'm pretty much lost, the searches got my close but none seemed to work so I'm hoping somebody can help with this.
I have a batch file, it is far from optimized but basically what I am trying to do now is use the ping I have running in the batch which is written to check.csv should then populate the individual IP's at the beginning (hope that makes sense).
in a nut shell
The variables at the beginning should be populated from check.csv
Here is the code
'#echo off
rem set the individual IP addresses
Rem these are manually set at present, I want these set automatically from check.csv which is created at the end of this file at present but will be moved infront..
set sarah=10.1.14.106
set richard=10.1.15.135
set kathh=10.1.12.79
edited out a number of these to reduce the code on screen for you, but they are all the same format, name & IP
rem set the individual profile name
set n1=sharman
set n2=rburrell
removed more of the same (just duplicates the above code for a different user)
rem set the computer name
set sarahPC=PB7B237
set richardPC=PB1VAL9
removed more of the same
REM set the main install paths
set M3="C:\Users\dclare\Documents\VW Compiler\Installers\M3.exe"
set H3="C:\Users\dclare\Documents\VW Compiler\Installers\H3.exe"
set MooresInst="C:\Users\dclare\Documents\VW Compiler\Catalogues\PD PS\Catalogue"
Rem menu
#echo off
cls
:start
echo.
echo 1 Ping
echo 2 List
echo 3 DBase Installers
echo 4 EXE Installers
echo 5 End
echo.
#CHOICE /C:12345
if errorlevel 5 goto End
if errorlevel 4 goto EXEInstallers
if errorlevel 3 goto DBase
if errorlevel 2 goto List
if errorlevel 1 goto Ping
goto End
:End
Pause
Exit
:EXEInstallers
echo EXE Installers
echo.
echo Upload folder
Xcopy %M3% "S:\# All Public\Information Technology\CAD\VWorlds" /s /y /q
Xcopy %H3% "S:\# All Public\Information Technology\CAD\VWorlds" /s /y /q
echo.
echo %sarah%
Xcopy %M3% "\\%sarah%\c$\Users\%n1%\Desktop\" /s /y /q
Xcopy %H3% "\\%sarah%\c$\Users\%n1%\Desktop\" /s /y /q
echo.
echo %richard%
Xcopy %M3% "\\%richard%\c$\Users\%n2%\Desktop\" /s /y /q
Xcopy %H3% "\\%richard%\c$\Users\%n2%\Desktop\" /s /y /q
echo.
removed more of the same (just duplicates the above code for a different user)
goto Start
:DBase
echo Database Installers
echo.
echo %sarah%
Xcopy %MooresInst% "\\%sarah%\c$\Virtual Worlds\Catalogue" /s /y /q
echo.
echo %richard%
Xcopy %MooresInst% "\\%richard%\c$\Virtual Worlds\Catalogue" /s /y /q
echo.
removed more of the same (just duplicates the above code for a different user)
echo
Goto Start
:List
echo.
echo This is a list of the IP's used currently, check against any that fail.
echo.
echo Name Puter IP
echo sarah %sarahPC%%sarah%
echo richard %richardPC% %richard%
echo kathh %kathhPC% %kathh%
echo amarie %amariePC% %amarie%
removed more of the same (just duplicates the above code for a different user)
echo.
Pause
Goto Start
:Ping
#echo off
if exist "C:\Users\dclare\Documents\VW Compiler\Copy to Desktop\Results.csv" Del /s /q "C:\Users\dclare\Documents\VW Compiler\Copy to Desktop\Results.csv"
Echo Pinging list...
set ComputerList=list.txt
pause
Echo Computername,IP Address>Final.csv
setlocal enabledelayedexpansion
for /f "usebackq tokens=*" %%A in ("%ComputerList%") do (
for /f "tokens=3" %%B in ('ping -n 1 -l 1 %%A ^|findstr Reply ^|^| echo Not found Failed:') do (
set IPadd=%%B
echo %%A,!IPadd:~0, -1!>>Check.csv
))
pause
Goto Start'
here is the check.csv file contents
PB1VAL9 10.1.15.135
PB7B218 Failed
PB1VAL8 10.1.15.111
PB7B210 10.1.5.253
removed more of the same (just duplicates the above code for a different user)
Try this routine out to set your variables to ip addresses:
echo off
for /f "usebackq tokens=1,2" %%a in ("check.csv") (
echo setting %%a=%%b
set "%%a=%%b"
)
With this ping routine it shows if it is online or offline. I had to guess what you were trying to do as we don't have your files to test it.
Give us a sample of Check.csv if you want to populate a set of variables with the IP addresses inside it.
:Ping
#echo off
Del /s /q "C:\Users\dclare\Documents\VW Compiler\Copy to Desktop\Results.csv" 2>nul
Echo Pinging list...
set "ComputerList=list.txt"
pause
Echo Computername,IP Address>Final.csv
for /f "usebackq delims=" %%A in ("%ComputerList%") do (
ping -n 1 -l 1 %%A >nul
if not errorlevel 1 (
>>Check.csv echo %%A,online
) else (
>>Check.csv echo %%A,offline
)
)
pause
Goto Start

Batch file which saves user input path with spaces to a text file. (win 7)

First of all i am a noob in programming.
I am trying to make a batch file which takes an installed directory of a program as user input when run for the first time (means it should not ask for the directory the second time it is run). By searching for various scripts, i reached till here,
#echo off
Echo =============================================
echo Directory
Echo =============================================
setlocal enableextensions enabledelayedexpansion
set /p mypath=Please specify install directory;
Echo %mypath% ----was what you typed
pause
echo start>temp.txt
echo %mypath%>>temp.txt
echo \programfolder\program.exe>>temp.txt
echo -argument -argument>>temp.txt
setlocal enabledelayedexpansion
set FINAL=
for /f %%a in (temp.txt) do (
set FINAL=!FINAL!%%a
)
echo %FINAL%>input.txt
del /q temp.txt
Pause
start "<input.txt"
This saves the input path in the "input.txt" text file, and runs the program the next time it is launched.
I want the text file to have the saved path as "start driveletter:\foldername\foldername with spaces\programfolder\program.exe" -arguments
However the "start", "program folder", "program.exe" and "-arguments" are fixed.
The user input path should get saved in- %mypath%.
The does what you asked, I think:
#echo off
if exist "go.bat" go.bat
set /p "mypath=Please specify install directory; "
Echo "%mypath%" ----was what you typed
pause
>"go.bat" echo #start "" "%mypath%\programfolder\program.exe" -argument -argument

Batch file that returns folder size

I'm having space issues on my Vista machine and need to figure out what's taking up so much space.
I would like to write a simple batch file that returns all folders under C: and the size of each folder.
The dir command doesn't appear to return folder size.
Unfortunately we don't have admin rights and can't install a third party application and we have other users in our group that also need this information.
I'd have a look at this thread for some clues as to how to achieve the directory size:
Batch File To Display Directory Size
Otherwise:
dirsize:
#echo off
setLocal EnableDelayedExpansion
set /a value=0
set /a sum=0
FOR /R %1 %%I IN (*) DO (
set /a value=%%~zI/1024
set /a sum=!sum!+!value!
)
#echo %CD%:!sum! k
AllDirSize:
echo off
set WORKING_DIRECTORY=%cd%
for /f "delims=" %%a in ('dir /a:D /D /B /S') do (
echo off
cd %%a
"%WORKING_DIRECTORY%"\dirsize "%%a"
cd %WORKING_DIRECTORY%
)
Use it: ALLDIRSIZE > C:\temp\FileContainingFolderSizes.txt
Which is taken from the excellent Richard Bishop testing forums: http://www.bish.co.uk/forum/index.php?topic=58.0
Not exactly answering your question, but if you have GUI access I'd suggest using TreeSize:
http://www.jam-software.com/freeware/index.shtml
If you prefer command line use du command from Unix utils:
http://unxutils.sourceforge.net/