i want to chnage the proxy of icedeagon in each profile using a script - batch-file

I'm trying to write code that applies a proxy each time I open the icedragon browser.
I have written a batch file but it's not applying it when I check in advance==>network==>setting
#echo off
rem Prompt for the start and end numbers
set /p start=Enter start number:
set /p end=Enter end number:
rem Change to the IceDragon program directory
cd "C:\Program Files (x86)\Comodo\IceDragon"
rem Define the loop counter
set /a i=%start%
rem Check if the proxy file exists
if not exist proxy.txt (
echo Proxy file not found
goto end
)
:loop
rem Check if the loop counter has reached the end number
if %i% == %end% (
echo Loop reached the end number
goto end
)
rem Read the proxy from the file
set /p proxy=<proxy.txt
rem Set the HTTP_PROXY environment variable
set HTTP_PROXY=%proxy%
rem Display the current number and proxy
echo Running the command with number %i% and proxy %HTTP_PROXY%
rem Check if the proxy is working
ping -n 1 google.com > nul
if not errorlevel 1 (
rem Run the IceDragon program with the -no-remote and current number options
start "" "icedragon.exe" -no-remote -P %i%
) else (
echo Proxy %HTTP_PROXY% not working, moving on to next proxy
)
rem Wait for 2 seconds
ping -n 3 127.0.0.1 > nul
rem Increment the loop counter
set /a i+=1
rem Repeat the loop
goto loop
:end
pause
You also need a proxy file in the same directory, and have a working proxy.

Related

Batch File - Hosts file editor - prevent duplicate entries - delete previously added entries

Okay here is what I got so far.
This is meant to add websites to block in the hosts file, as well as allow the user to delete the entries when they want to. When trying to add a website to block sometimes it creates a new line then puts the entry on the line before it. This is not what I want. I want it to create a new line then add the entry on that line. For some reason it works sometimes and other times it don't work at all. I get an error message that says Find parameter is incorrect. I am using the Find command to see if the entries is already in the hosts file. If it is I want it to avoid adding it. If it is not then I want to add it. When I try to delete a entry the batch just crashes, so I am not really sure what I am doing wrong here. I am trying to find the entry and replace it with nothing. What I really want to do is delete the entire line so that I don't end up with a lot of blank lines.
Any help is greatly appreciated. Thanks!
#echo off
TITLE Modifying your HOSTS file
COLOR F0
:LOOP
cls
SET "CHOICE="
ECHO Choose 1 to block a website
ECHO Choose 2 remove a blocked website
ECHO Choose 3 to exit
SET /P CHOICE= selection %CHOICE%
GOTO %CHOICE%
:1
cls
SET /P WEBSITE=Enter the name of the website to add:
SET HOME= 127.0.0.1
SET NEWLINE=^& echo.
SET BLOCKEDSITE=%HOME% %WEBSITE%
FIND /C /I %BLOCKEDSITE% %WINDIR%\system32\drivers\etc\hosts
IF %ERRORLEVEL% NEQ 0 ECHO %NEWLINE%^%BLOCKEDSITE%>>%WINDIR%\System32\drivers\etc\hosts
ECHO Website blocked
ping 127.0.0.1 -n 5 > nul
GOTO LOOP
:2
cls
SET /P WEBSITE=Enter the name of the website to remove:
SETLOCAL ENABLEEXTENTIONS DISABLEDELAYEDEXPANSION
SET "HOME= 127.0.0.1 "
SET "BLOCKEDSITE=%HOME% %WEBSITE%"
SET "REPLACE="
SET "HOSTSFILE=%WINDIR%\system32\drivers\etc\hosts"
FOR /F "DELIMS=" %%i IN ('TYPE "%HOSTSFILE%" ^& BREAK ^> "%HOSTSFILE%" ')
DO
(
SET "LINE=%%i"
SETLOCAL ENABLEDELAYEDEXPANSION
>>"%HOSTSFILE%" echo(!LINE:%BLOCKEDSITE%=%REPLACE%!
ENDLOCAL
)
ECHO Website unblocked
GOTO LOOP
:3
EXIT
Please note that the term website is misleading when referring to the entries of the hosts file. The entries of hosts file are used for custom mappings of DNS host names to IP addresses, and any host name that is present in the file does not necessarily hosts a website. Using the term website may lead to the false impression that something like http://www.example.com can be added to hosts file which is not true.
Skipping a host if it is already present in the hosts file:
The problem with your usage of find is that %BLOCKEDSITE% has embedded spaces so you should enclose it quotes and use:
FIND /C /I "%BLOCKEDSITE%" %WINDIR%\system32\drivers\etc\hosts
But it has another problem: Because of its dependency on the exact spacing between the IP address and host name which is mandated by %BLOCKEDSITE% It only works for the entries that are added by your batch file. Additionally the user may have commented out (disabled) an entry by placing # in the begging of the line that contains the entry, and your batch code will skip adding the host even if the entry is disabled.
This can be resolved by using findstr with its regex syntax. for example:
findstr /IRC:"^ *127\.0\.0\.1 *example\.com *$" "%WINDIR%\system32\drivers\etc\hosts"
Removing an entry from the hosts file:
In the FOR loop you just have to skip writing the lines that contains the specified entry:
if "!Line!"=="!LINE:%BLOCKEDSITE%=!" echo(!Line!>>"%HOSTSFILE%"
But again it is not accurate and is suffering from the same problems that are mentioned earlier for skipping adding the entry. Again By using findstr you can easily remove the lines that contain the unwanted entry:
findstr /VIRC:"^ *127\.0\.0\.1 *example\.com *$" "%HOSTSFILE%" > "%HOSTSFILE%.tmp"
del "%HOSTSFILE%"
ren "%HOSTSFILE%.tmp" "hosts"
With above mentioned points the script can be rewritten like this:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
title Modifying your HOSTS file
color F0
set "HOSTSFILE=%WINDIR%\system32\drivers\etc\hosts"
set "HOME=127.0.0.1"
set "PROMPT_TEXT=Enter the host name to"
set "ACTION_TEXT[1]=add"
set "ACTION_TEXT[2]=remove"
set "FindEmptyLine=^ *$"
set "NewLineAppended="
cls
setlocal EnableDelayedExpansion
:LOOP
echo,
echo 1. Block a host
echo 2. Remove a blocked host
echo 3. Exit
choice /C "123" /N /M "Choose an item [1, 2, 3]: "
set "Item=%errorlevel%"
goto choice%Item%
:choice0 // User Pressed CTRL-C
:choice3
exit /b
:choice1
call :Common
set "HostEntry=!HOME! !HOST!"
findstr /IRC:"!FindEntry!" "!HOSTSFILE!"> nul && (
echo The host !HOST! is already blocked, No action taken.
) || (
if not defined NewLineAppended (
REM This will append a new line ONLY if the file does not end by LF character
type "!HOSTSFILE!" | findstr $ > "!HOSTSFILE!.tmp" && (
del "!HOSTSFILE!"
ren "!HOSTSFILE!.tmp" "hosts"
set "NewLineAppended=1"
)
)
echo !HostEntry!>>"!HOSTSFILE!"
echo The host !HOST! blocked
)
goto LOOP
:choice2
call :Common
findstr /VIR /C:"!FindEntry!" /C:"!FindEmptyLine!" "!HOSTSFILE!">"!HOSTSFILE!.tmp" && (
del "!HOSTSFILE!"
ren "!HOSTSFILE!.tmp" "hosts"
echo The host !HOST! unblocked
)
goto LOOP
:Common
set "HOST="
set /P "HOST=!PROMPT_TEXT! !ACTION_TEXT[%Item%]! (e.g. example.com): "
if not defined HOST (
(goto)2>nul
goto LOOP
)
set "FindEntry=^^ *!HOME! *!HOST! *$"
set "FindEntry=!FindEntry:.=\.!"
exit /b

Generate war from play application

I'm trying to generate a war file in play application.
I am using starter java project: play-java-starter-example. Play version 2.6.2 in Windows.
I added the plugin play2war in project/plugins.sbt:
addSbtPlugin("com.github.play2war" % "play2-war-plugin" % "1.4.0")
After that I ran the following commands:
C:\project_name>sbt
[project_name]$ dist
It generates a zip file as it's supposed to.
The next step according to the official Doc is to execute a .bat file inside target/universal/[project_name]/bin
Im stuck at this step, execution of the script gives the following message:
console output
Here is the content of the .bat file generated by the dist command:
#REM play-java-starter-example launcher script
#REM
#REM Environment:
#REM JAVA_HOME - location of a JDK home dir (optional if java on path)
#REM CFG_OPTS - JVM options (optional)
#REM Configuration:
#REM PLAY_JAVA_STARTER_EXAMPLE_config.txt found in the PLAY_JAVA_STARTER_EXAMPLE_HOME.
#setlocal enabledelayedexpansion
#echo off
if "%PLAY_JAVA_STARTER_EXAMPLE_HOME%"=="" set "PLAY_JAVA_STARTER_EXAMPLE_HOME=%~dp0\\.."
set "APP_LIB_DIR=%PLAY_JAVA_STARTER_EXAMPLE_HOME%\lib\"
rem Detect if we were double clicked, although theoretically A user could
rem manually run cmd /c
for %%x in (!cmdcmdline!) do if %%~x==/c set DOUBLECLICKED=1
rem FIRST we load the config file of extra options.
set "CFG_FILE=%PLAY_JAVA_STARTER_EXAMPLE_HOME%\PLAY_JAVA_STARTER_EXAMPLE_config.txt"
set CFG_OPTS=
if exist "%CFG_FILE%" (
FOR /F "tokens=* eol=# usebackq delims=" %%i IN ("%CFG_FILE%") DO (
set DO_NOT_REUSE_ME=%%i
rem ZOMG (Part #2) WE use !! here to delay the expansion of
rem CFG_OPTS, otherwise it remains "" for this loop.
set CFG_OPTS=!CFG_OPTS! !DO_NOT_REUSE_ME!
)
)
rem We use the value of the JAVACMD environment variable if defined
set _JAVACMD=%JAVACMD%
if "%_JAVACMD%"=="" (
if not "%JAVA_HOME%"=="" (
if exist "%JAVA_HOME%\bin\java.exe" set "_JAVACMD=%JAVA_HOME%\bin\java.exe"
)
)
if "%_JAVACMD%"=="" set _JAVACMD=java
rem Detect if this java is ok to use.
for /F %%j in ('"%_JAVACMD%" -version 2^>^&1') do (
if %%~j==java set JAVAINSTALLED=1
if %%~j==openjdk set JAVAINSTALLED=1
)
rem BAT has no logical or, so we do it OLD SCHOOL! Oppan Redmond Style
set JAVAOK=true
if not defined JAVAINSTALLED set JAVAOK=false
if "%JAVAOK%"=="false" (
echo.
echo A Java JDK is not installed or can't be found.
if not "%JAVA_HOME%"=="" (
echo JAVA_HOME = "%JAVA_HOME%"
)
echo.
echo Please go to
echo http://www.oracle.com/technetwork/java/javase/downloads/index.html
echo and download a valid Java JDK and install before running play-java-starter-example.
echo.
echo If you think this message is in error, please check
echo your environment variables to see if "java.exe" and "javac.exe" are
echo available via JAVA_HOME or PATH.
echo.
if defined DOUBLECLICKED pause
exit /B 1
)
rem We use the value of the JAVA_OPTS environment variable if defined, rather than the config.
set _JAVA_OPTS=%JAVA_OPTS%
if "!_JAVA_OPTS!"=="" set _JAVA_OPTS=!CFG_OPTS!
rem We keep in _JAVA_PARAMS all -J-prefixed and -D-prefixed arguments
rem "-J" is stripped, "-D" is left as is, and everything is appended to JAVA_OPTS
set _JAVA_PARAMS=
set _APP_ARGS=
:param_loop
call set _PARAM1=%%1
set "_TEST_PARAM=%~1"
if ["!_PARAM1!"]==[""] goto param_afterloop
rem ignore arguments that do not start with '-'
if "%_TEST_PARAM:~0,1%"=="-" goto param_java_check
set _APP_ARGS=!_APP_ARGS! !_PARAM1!
shift
goto param_loop
:param_java_check
if "!_TEST_PARAM:~0,2!"=="-J" (
rem strip -J prefix
set _JAVA_PARAMS=!_JAVA_PARAMS! !_TEST_PARAM:~2!
shift
goto param_loop
)
if "!_TEST_PARAM:~0,2!"=="-D" (
rem test if this was double-quoted property "-Dprop=42"
for /F "delims== tokens=1,*" %%G in ("!_TEST_PARAM!") DO (
if not ["%%H"] == [""] (
set _JAVA_PARAMS=!_JAVA_PARAMS! !_PARAM1!
) else if [%2] neq [] (
rem it was a normal property: -Dprop=42 or -Drop="42"
call set _PARAM1=%%1=%%2
set _JAVA_PARAMS=!_JAVA_PARAMS! !_PARAM1!
shift
)
)
) else (
if "!_TEST_PARAM!"=="-main" (
call set CUSTOM_MAIN_CLASS=%%2
shift
) else (
set _APP_ARGS=!_APP_ARGS! !_PARAM1!
)
)
shift
goto param_loop
:param_afterloop
set _JAVA_OPTS=!_JAVA_OPTS! !_JAVA_PARAMS!
:run
set "APP_CLASSPATH=%APP_LIB_DIR%\..\conf\;%APP_LIB_DIR%\play-java-starter-example.play-java-starter-example-1.0-SNAPSHOT-sans-externalized.jar;%APP_LIB_DIR%\org.scala-lang.scala-library-2.12.2.jar;%APP_LIB_DIR%\com.typesafe.play.twirl-api_2.12-1.3.3.jar;%APP_LIB_DIR%\org.scala-lang.modules.scala-xml_2.12-1.0.6.jar;%APP_LIB_DIR%\com.typesafe.play.play-server_2.12-2.6.2.jar;%APP_LIB_DIR%\com.typesafe.play.play_2.12-2.6.2.jar;%APP_LIB_DIR%\com.typesafe.play.build-link-2.6.2.jar;%APP_LIB_DIR%\com.typesafe.play.play-exceptions-2.6.2.jar;%APP_LIB_DIR%\com.typesafe.play.play-netty-utils-2.6.2.jar;%APP_LIB_DIR%\org.slf4j.slf4j-api-1.7.25.jar;%APP_LIB_DIR%\org.slf4j.jul-to-slf4j-1.7.25.jar;%APP_LIB_DIR%\org.slf4j.jcl-over-slf4j-1.7.25.jar;%APP_LIB_DIR%\com.typesafe.play.play-streams_2.12-2.6.2.jar;%APP_LIB_DIR%\org.reactivestreams.reactive-streams-1.0.0.jar;%APP_LIB_DIR%\com.typesafe.akka.akka-stream_2.12-2.5.3.jar;%APP_LIB_DIR%\com.typesafe.akka.akka-actor_2.12-2.5.3.jar;%APP_LIB_DIR%\com.typesafe.config-1.3.1.jar;%APP_LIB_DIR%\org.scala-lang.modules.scala-java8-compat_2.12-0.8.0.jar;%APP_LIB_DIR%\com.typesafe.ssl-config-core_2.12-0.2.1.jar;%APP_LIB_DIR%\org.scala-lang.modules.scala-parser-combinators_2.12-1.0.6.jar;%APP_LIB_DIR%\com.typesafe.akka.akka-slf4j_2.12-2.5.3.jar;%APP_LIB_DIR%\com.fasterxml.jackson.core.jackson-core-2.8.9.jar;%APP_LIB_DIR%\com.fasterxml.jackson.core.jackson-annotations-2.8.9.jar;%APP_LIB_DIR%\com.fasterxml.jackson.core.jackson-databind-2.8.9.jar;%APP_LIB_DIR%\com.fasterxml.jackson.datatype.jackson-datatype-jdk8-2.8.9.jar;%APP_LIB_DIR%\com.fasterxml.jackson.datatype.jackson-datatype-jsr310-2.8.9.jar;%APP_LIB_DIR%\commons-codec.commons-codec-1.10.jar;%APP_LIB_DIR%\com.typesafe.play.play-json_2.12-2.6.2.jar;%APP_LIB_DIR%\com.typesafe.play.play-functional_2.12-2.6.2.jar;%APP_LIB_DIR%\org.scala-lang.scala-reflect-2.12.2.jar;%APP_LIB_DIR%\org.typelevel.macro-compat_2.12-1.1.1.jar;%APP_LIB_DIR%\joda-time.joda-time-2.9.9.jar;%APP_LIB_DIR%\com.google.guava.guava-22.0.jar;%APP_LIB_DIR%\com.google.code.findbugs.jsr305-1.3.9.jar;%APP_LIB_DIR%\com.google.errorprone.error_prone_annotations-2.0.18.jar;%APP_LIB_DIR%\com.google.j2objc.j2objc-annotations-1.1.jar;%APP_LIB_DIR%\org.codehaus.mojo.animal-sniffer-annotations-1.14.jar;%APP_LIB_DIR%\io.jsonwebtoken.jjwt-0.7.0.jar;%APP_LIB_DIR%\org.apache.commons.commons-lang3-3.6.jar;%APP_LIB_DIR%\javax.transaction.jta-1.1.jar;%APP_LIB_DIR%\javax.inject.javax.inject-1.jar;%APP_LIB_DIR%\com.typesafe.play.play-java-forms_2.12-2.6.2.jar;%APP_LIB_DIR%\com.typesafe.play.play-java_2.12-2.6.2.jar;%APP_LIB_DIR%\org.reflections.reflections-0.9.11.jar;%APP_LIB_DIR%\org.javassist.javassist-3.21.0-GA.jar;%APP_LIB_DIR%\net.jodah.typetools-0.5.0.jar;%APP_LIB_DIR%\org.hibernate.hibernate-validator-5.4.1.Final.jar;%APP_LIB_DIR%\javax.validation.validation-api-1.1.0.Final.jar;%APP_LIB_DIR%\org.jboss.logging.jboss-logging-3.3.0.Final.jar;%APP_LIB_DIR%\com.fasterxml.classmate-1.3.1.jar;%APP_LIB_DIR%\org.springframework.spring-context-4.3.9.RELEASE.jar;%APP_LIB_DIR%\org.springframework.spring-core-4.3.9.RELEASE.jar;%APP_LIB_DIR%\org.springframework.spring-beans-4.3.9.RELEASE.jar;%APP_LIB_DIR%\com.typesafe.play.filters-helpers_2.12-2.6.2.jar;%APP_LIB_DIR%\com.typesafe.play.play-logback_2.12-2.6.2.jar;%APP_LIB_DIR%\ch.qos.logback.logback-classic-1.2.3.jar;%APP_LIB_DIR%\ch.qos.logback.logback-core-1.2.3.jar;%APP_LIB_DIR%\com.typesafe.play.play-akka-http-server_2.12-2.6.2.jar;%APP_LIB_DIR%\com.typesafe.akka.akka-http-core_2.12-10.0.9.jar;%APP_LIB_DIR%\com.typesafe.akka.akka-parsing_2.12-10.0.9.jar;%APP_LIB_DIR%\com.typesafe.play.play-guice_2.12-2.6.2.jar;%APP_LIB_DIR%\com.google.inject.guice-4.1.0.jar;%APP_LIB_DIR%\aopalliance.aopalliance-1.0.jar;%APP_LIB_DIR%\com.google.inject.extensions.guice-assistedinject-4.1.0.jar;%APP_LIB_DIR%\com.h2database.h2-1.4.194.jar;%APP_LIB_DIR%\play-java-starter-example.play-java-starter-example-1.0-SNAPSHOT-assets.jar"
set "APP_MAIN_CLASS=play.core.server.ProdServerStart"
if defined CUSTOM_MAIN_CLASS (
set MAIN_CLASS=!CUSTOM_MAIN_CLASS!
) else (
set MAIN_CLASS=!APP_MAIN_CLASS!
)
rem Call the application and pass all arguments unchanged.
"%_JAVACMD%" !_JAVA_OPTS! !PLAY_JAVA_STARTER_EXAMPLE_OPTS! -cp "%APP_CLASSPATH%" %MAIN_CLASS% !_APP_ARGS!
#endlocal
:end
exit /B %ERRORLEVEL%
I figured it out.
Here are the steps:
I added the plugin play2war in project/plugins.sbt:
addSbtPlugin("com.github.play2war" % "play2-war-plugin" % "1.4.0")
add in build.sbt
import com.github.play2war.plugin._
libraryDependencies ++= Seq( "com.github.play2war" % "play2-war_2.9.1" % "0.8.2" )
Play2WarPlugin.play2WarSettings
Play2WarKeys.servletVersion := "3.1"
execute command sbt war
Except the war generated is quit strange, it contains only jar files, is it normal ?

'for' loop variable not releasing on loop iterations

Been wrecking my brain all night trying to figure out why this isn't working, but one of my variables isn't releasing on the next iteration of my loop and I can't figure out why... The first pass of the loop seems to work fine, but the next iteration, the first variable gets locked and the script connects to the system that's already been configured.
I've been staring at this for a while now and no matter how I approach it, it still behaves badly. :/ The purpose is to read a text-string of a given file, and use it to modify (via Find and Replace (fnr.exe)) another file with several instances of the required data. I didn't have alot of luck with 'findstr' replacing so many instances of the text required so I went with a tool I've used before that seemed to work really well in it's previous scripting application...
Truth be told, I find myself stumbling with even the most basic code a lot of times, so any kind soul willing to impart some wisdom/assistance would be greatly appreciated!
Thanks in advance...
#ECHO ON
setlocal enabledelayedexpansion
> "%~dp0report.log" ECHO Batch Script executed on %DATE% at %TIME%
rem read computer list line by line and do
FOR /F %%A in (%~dp0workstations.txt) do (
SET lwn=
SET WKSTN=%%A
rem connect to workstation and read lwn.txt file
pushd "\\%WKSTN%\c$\"
IF ERRORLEVEL 0 (
FOR /F %%I in (\\%wkstn%\c$\support\lwn.txt) DO (
SET LWN=%%I
%~dp0fnr.exe --cl --dir "\\%WKSTN%\c$\support\folder\config" --fileMask "file.xml" --find "21XXXX" --replace "%%I"
IF ERRORLEVEL 0 ECHO Station %LWN%,Workstation %WKSTN%,Completed Successfully >> %~dp0report.log
IF ERRORLEVEL 1 ECHO Station %LWN%,Workstation %WKSTN%, A READ/WRITE ERROR OCCURRED >> %~dp0report.log
echo logwrite error 1 complete
popd
)
)
IF ERRORLEVEL 1 (
ECHO ,,SYSTEM IS OFFLINE >> %~dp0report.log
)
popd
set wkstn=
set lwn=
echo pop d complete
)
msg %username% Script run complete...
eof
The ! notation must be used on all variables that are changed inside the loop.
C:>type looptest.bat
#ECHO OFF
setlocal enabledelayedexpansion
rem read computer list line by line and do
FOR /F %%A in (%~dp0workstations.txt) do (
SET WKSTN=%%A
ECHO WKSTN is set to %WKSTN%
ECHO WKSTN is set to !WKSTN!
pushd "\\!WKSTN!\c$\"
ECHO After PUSHD, ERRORLEVEL is set to %ERRORLEVEL%
ECHO After PUSHD, ERRORLEVEL is set to !ERRORLEVEL!
IF !ERRORLEVEL! NEQ 0 (
ECHO ,,SYSTEM IS OFFLINE
) ELSE (
ECHO Host !WKSTN! is available
)
popd
)
EXIT /B 0
The workstations.txt file contained the following. (I should not give out actual host names.)
LIVEHOST1
DEADHOST1
LIVEHOST2
The output is...
C:>call looptest.bat
WKSTN is set to
WKSTN is set to LIVEHOST1
After PUSHD, ERRORLEVEL is set to 0
After PUSHD, ERRORLEVEL is set to 0
Host LIVEHOST1 is available
WKSTN is set to
WKSTN is set to DEADHOST1
The network path was not found.
After PUSHD, ERRORLEVEL is set to 0
After PUSHD, ERRORLEVEL is set to 1
,,SYSTEM IS OFFLINE
WKSTN is set to
WKSTN is set to LIVEHOST2
After PUSHD, ERRORLEVEL is set to 0
After PUSHD, ERRORLEVEL is set to 0
Host LIVEHOST2 is available
Although your code have several issues, the main one is the use of % instead of ! when you access the value of variables modified inside a for loop (although you already have the "enabledelayedexpansion" part in setlocal command). However, I noted that you sometimes use the FOR replaceable parameter (like in --replace "%%I") and sometimes you use the variable with the same value (%LWN%), so a simpler solution in your case would be to replace every %VAR% with its corresponding %%A for parameter.
I inserted this modification in your code besides a couple small changes that make the code simpler and clearer.
#ECHO ON
setlocal
> "%~dp0report.log" ECHO Batch Script executed on %DATE% at %TIME%
rem Read computer list line by line and do
FOR /F %%A in (%~dp0workstations.txt) do (
rem Connect to workstation and read lwn.txt file
pushd "\\%%A\c$\"
IF NOT ERRORLEVEL 1 (
FOR /F "usebackq" %%I in ("\\%%A\c$\support\lwn.txt") DO (
%~dp0fnr.exe --cl --dir "\\%%A\c$\support\folder\config" --fileMask "file.xml" --find "21XXXX" --replace "%%I"
IF NOT ERRORLEVEL 1 (
ECHO Station %%I,Workstation %%A,Completed Successfully >> %~dp0report.log
) ELSE (
ECHO Station %%I,Workstation %%A, A READ/WRITE ERROR OCCURRED >> %~dp0report.log
echo logwrite error 1 complete
)
)
) ELSE (
ECHO ,,SYSTEM IS OFFLINE >> %~dp0report.log
)
popd
echo pop d complete
)
msg %username% Script run complete...

What is wrong with goto command in for loop?

I have a list of computers that I want to make some things according to tcp connection status.
I'm trying to check tcp connection and if errorlog is "1" so write line to log and skip to next computer.
The problem is that when a computer has no tcp connection the goto skip_action command takes the script to the end and exit and the other computers in the list left unprocessed.
I have also tried to use goto :eof and it terminates the script unexpected.
ipst.txt file:
1 10.1.1.10
3 10.1.3.10
8 10.1.3.10
This is the batch file code:
#echo off
setlocal enabledelayedexpansion
set computerslist=ipst.txt
for /f "usebackq tokens=1,2" %%A in ("%Computerslist%") do (
cls
set Station_Num=%%A
set Comp_IP=%%B
ping !Comp_IP! -n 1 | findstr "TTL"
if !errorlevel!==1 (
echo Station !Station_Num! .... !Comp_IP! ..................... No Communication.>>%log%
goto skip_action
)
echo Getting Administrator Credentials:
net use \\!Comp_IP! /USER:WORKGROUP\****** ******
echo.
xcopy file.txt \\!Comp_IP!\c\temp\
echo Disconneting Session From Remote Computer :
net use \\!Comp_IP! /DELETE /YES
:skip_action
echo end of working on !Station_Num!
)
echo end of script
The problem here is that GOTO cancels the for loop.
But you can simply enclose your action in an ELSE block
for /f "usebackq tokens=1,2" %%A in ("%Computerslist%") do (
cls
set Station_Num=%%A
set Comp_IP=%%B
ping !Comp_IP! -n 1 | findstr "TTL"
if !errorlevel!==1 (
echo Station !Station_Num! .... !Comp_IP! ..................... No Communication.>>%log%
) Else (
echo Getting Administrator Credentials:
net use \\!Comp_IP! /USER:WORKGROUP\****** ******
echo.
xcopy file.txt \\!Comp_IP!\c\temp\
echo Disconneting Session From Remote Computer :
net use \\!Comp_IP! /DELETE /YES
)
echo end of working on !Station_Num!
)
General form of if - else statement
If condition (
Statements
) else (
Statements to be executed if condition is not met
)
Make your call from the loop
You can have a function ping .. and parse variable references to it .. the preceding code could also be rendered as %%a and %%b being the first and second argument respectively -- in your function call ...
Call :pingcomputers %%a %%b
This way you avoid set altogether
:pingcomputers
Rem Pinging computer %~1
Ping %~2 ¦ find "reply"
If errorlevel 1 goto failed
(Actions to be performed if ping wa successful)
Assuming that your script has a label failed and actions to.be performed if error level is not 0 ..
Not much of a script but hope it helps ...

Batch file - get and parse CMD output string

I never wrote a batch file, and now I have to write a batch file that runs a command line and parse its output. (e.g: CMD: Diskpart List volume, Output: list of volumes and the free space, I want to find the volume with the maximum free space)
My questions:
what should I write to get the output?
How can I parse it?
Thanks you all,
DiskPart list volumes will not give you free space, but I assume that was only an example.
This batch will give you drive with maximum capacity on your system.
It uses enough constructs to get you going with your own requirements...
You will need to put the following into a batch file
#echo off
echo ****************************************************
echo *** Give maximum drive size on a given system ****
echo ****************************************************
echo *** This script shows various batch constructs. ****
echo *** Use at your own risk - no warranty given ****
echo *** that's fit for any intended purpose :-)) ****
echo *** or that it's error free ****
echo ****************************************************
echo.
REM All of our vars will be local - we do not want to pollute environment. For more info exec 'help setlocal' from cmdline
REM implied endlocal will be issued at the end, no need to insert it
setlocal
REM name of temp file we will use
set tmpFile=tmp.txt
REM power will store multiplier we are currently working in
set power=0
REM maximum found drive size
set maxSize=0
REM Enable delayed expansion - must be on, otherwise all vars will be expanded on input. For more info exec 'help setlocal' from cmdline
setlocal enabledelayedexpansion
REM enable extensions (on by default). Please see help setlocal
setlocal enableextensions
REM get input from user. For more info exec 'help set' from cmdline
set /P cmd=Give diskpart command you want to run [list volume]
REM set default command if user did not set anything
if NOT DEFINED cmd set cmd=list volume
REM set file to contain command we want to run
echo %cmd%>%tmpFile%
REM Skip 8 first lines, then read each line as token. For more info exec 'help for' from cmdline
REM use of backquote will enable us to use temp file with spaces in name
set ERRORLEVEL=0
for /f "tokens=* skip=8 usebackq" %%i in (`diskpart /s %tmpFile%`) do (
set line=%%i
REM read 5 chars from input token, starting at char 49, convert it to number and assign to env var. For more info exec 'help set' from cmdline
REM This also shows delayed expansion convention (!var!).
set /A tsize=!line:~49,5!*1
REM test for unequality (we want to run body only if given size is not 0). For more info exec 'help if' from cmdline
REM see also other operators used (LSS, GEQ
if !tsize! NEQ 0 (
REM it's not possible to do most obvious (multiplication) as this would overflow.
REM '(' is block character. Look how they are positioned, it has to be this way!
REM Mind also where you can put spaces!
REM 'set a=7' is different to 'set a=7 '
set unit=!line:~54,2!
REM Mind use of '
if '!unit!'==' B' (
if !power! LSS 1 (
set power=1
set maxSize=!tSize!
)
if !power!==1 (
if !tsize! GEQ !maxsize! (
set maxSize=!tsize!
set maxUnit=!unit!
set maxDrive=!line!
)
)
)
if !unit!==KB (
if !power! LSS 3 (
set power=3
set maxSize=!tSize!
)
if !power!==3 (
if !tsize! GEQ !maxsize! (
set maxSize=!tsize!
set maxUnit=!unit!
set maxDrive=!line!
)
)
)
if !unit!==MB (
if !power! LSS 6 (
set power=6
set maxSize=!tSize!
)
if !power!==6 (
if !tsize! GEQ !maxsize! (
set maxSize=!tsize!
set maxUnit=!unit!
set maxDrive=!line!
)
)
)
if !unit!==GB (
if !power! LSS 9 (
set power=9
set maxSize=!tSize!
)
if !power!==9 (
if !tsize! GEQ !maxsize! (
set maxSize=!tsize!
set maxUnit=!unit!
set maxDrive=!line!
)
)
)
)
)
REM cleanup
del %tmpFile% /Q
REM this prints empty line
echo.
echo Your max drive is: !maxSize! !maxUnit!
echo.
echo Drive details:
echo !maxDrive!
Here's the easy beginning bit.
echo list volume > %temp%\temp12345
diskpart /s %temp%\temp12345 > %temp%\diskpart.txt
Doing this in batch is a nightmare, do you have any other options?
Edit: To do it in batch, you need to use FOR.
Check For /? for examples on how to parse data.

Resources