Batch - Get System Language of current running System - string

I am trying to get the current setted system-language under use of wmic,
but i don´t know why my code here is kind of "broken"...
The readback info from wmiccontains a lot of spaces and CRLF´s.
Therefore i wanted to cut all that out in the loop, but the syntax is not proper in my example...
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
:MAIN
setlocal
call :Get_System_Language "lang"
echo lang=%lang%
pause
exit /b 0
:Get_System_Language
setlocal
::Windows Language Code Identifier
::1031 German Germany
::2057 English United Kingdom
::1033 English United States
set "language=NUL"
set /a num_languages=3
for /f "tokens=1,2 delims==" %%a in ('wmic os get OSLanguage /Value') do (
if "%%a" equ "OSLanguage" (
set "item_tmp=%%b"
set "value=!item_tmp:=!"
if "!value!" equ "1031" (
set "language=german_germany"
)
if "!value!" equ "2057" (
set "language=english_british"
)
if "!value!" equ "1033" (
set "language=english_american"
)
(endlocal
if "%~1" neq "" (set "%~1=!language!")
)
exit /b 0
)
)
exit /b 1
Do you see the Problem?
Thank´s a lot ;-)

Split by = then one more loop to remove CRLF
Example:
#echo off
for /f "tokens=2*delims==" %%i in ('wmic os get OSLanguage /value') do for %%f in (%%i) do (
if "%%~f" == "1031" set "Lang=german_germany"
if "%%~f" == "2057" set "Lang=english_british"
if "%%~f" == "1033" set "Lang=english_american"
)
echo %Lang%

Whilst not a direct answer to your question, I have decided to post this as yourself or future users may find it useful in a similar situation.
#For /F "Delims==" %%G In ("(Set $) 2>NUL") Do #Set "%%G="
#For /F "Tokens=1,* Delims=: " %%G In ('
%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -Command
"[CultureInfo]::InstalledUICulture | Select-Object"^
" LCID, KeyboardLayoutId, Name, DisplayName, ThreeLetterWindowsLanguageName"'
) Do #Set "$%%G=%%H"
#(Set $) 2>NUL
#Pause
The last two lines are included to hopefully display the names of any variables defined, and their values. The first line is to ensure that none of those are predefined before enumerating.

Related

Looping through a string in batch files (enableDelayedExpansion)

I hope you can help me with my code below... I am already using enableDelayedExpansion but I wonder why the output is still all the same with my previously used parameters:
For example if i run my script with parameters SERVERS:ALL, I was expecting it to output SERVERS and ALL, but it is always displaying the first parameters when I run them for the first time. Thoughts?
set flag=false
setlocal enableDelayedExpansion
IF [%1] == [] (
set flag=false
) else (
set string=%1
echo String: !string!
set flag=true
for %%x in (%string::= %) do (
echo VAL is: %%x
endlocal
)
)
:END
popd
echo Ending %jobname% ... %check%
exit %check%
Here is a way to do it without delayed expansion. I left out the flags because they serve no noticable purpose.
#echo off
if not "%~1"=="" (
echo.input string=%~1
call :ParseString "%~1"
)
goto :eof
:ParseString
rem %1=string
for /f "tokens=1* delims=:" %%a in ('echo(%~1') do (
echo VAL is: %%a
call :ParseString "%%b"
)
goto :eof

How do you sort list of strings in batch?

Hi i have a script that searches for certain directories based on a string provided by the user, he then presents the directories meeting the user.s inout and offers to cd to them.
#ECHO off
setlocal enableextensions
:start
set scriptname=%0%
set PAUSE_ON_ERROR=yes
rem #
rem ###### SECTION TO CUSTOMIZE #####
rem #
rem #
rem # This is the list of directories where instances of TECSYS iTopia installed under
rem # JBoss are located.
rem #
set dir_to_search_in=C: C:\TecsysDev\iTopiaControlPanel\trunk
rem #
rem ###### END SECTION TO CUSTOMIZE #####
rem #
set argNb=0
for %%x in (%*) do Set /A argNb+=1
if %argNb% GTR 1 (
goto :showUsage
) else if %argNb% == 0 (
set ENV_NAME=*
) else (
set ENV_NAME=*%~1*
)
:MainBlock
set scriptname=%0%
cd /d %~dp0
for /f "usebackq delims=" %%D in (`cd`) do set scriptdir=%%D
for /f "usebackq delims=" %%D in (`cd`) do set CURRENT_DIR=%%D
cd "%scriptdir%"
for /f "usebackq delims=" %%D in (`cd`) do set JBOSS_DIR=%%D
set JBOSS_DIR=%JBOSS_DIR%\..\..\..\..\..
cd "%JBOSS_DIR%"
for /f "usebackq delims=" %%D in (`cd`) do set JBOSS_DIR=%%D
cd "%CURRENT_DIR%"
rem Make sure that we have the findstr utility installed.
set findstr_found=
for /f "usebackq" %%f in (`echo x ^| findstr x 2^>nul`) do set findstr_found=%%f
if "X%findstr_found%" == "X" (
echo The findstr utility is not found.
goto pauseforError
)
call :getJbossVersion
rem prepare the list of special JBoss environments to exclude
if /i "%JBOSS_VERSION%" lss "5" (
set env_to_exclude=all default minimal
) else if /i "%JBOSS_VERSION%" lss "6" (
set env_to_exclude=all default minimal standard web
) else (
set env_to_exclude=all default minimal jbossweb-standalone standard
)
rem find the environment directories
for %%f in (%dir_to_search_in%) do (
for /f "delims=" %%G in ('dir /b /ad "%%~f\jboss*"') do (
if exist "%%f\%%G\server\%ENV_NAME%" (
for /f "delims=" %%H in ('dir /b /ad "%%~f\%%~G\server\%ENV_NAME%"') do (
call :concat %%~f\%%~G\server\%%~H
)
)
)
)
if "X%environment_home_list%" == "X" (
echo Invalid %1 environment requested.
goto :pauseforError
)
rem display the folders
setlocal enabledelayedexpansion
rem checking if a unique instance of the environment exists
Set Count=0
for %%t in (%environment_home_list%) do (
Set /A Count+=1
)
if %count% == 1 (
for /f "tokens=1 delims= " %%d in ("%environment_home_list%") do (
set chosenEnv=%%d
goto :cdToEnv
)
)
:chooseEnvironment
Set Count=1
echo.
echo Please choose an environment:
echo.
echo 0^) Exit
for %%z in (%env_home_list%) do (
echo !Count!^) %%z
Set /A Count+=1
)
rem reading user input
SET /P userChoice=
set /a Test=userChoice
if !Test! EQU 0 (
if not !userChoice! EQU 0 (
echo Please enter a valid number
goto :chooseEnvironment
)
)
rem checking if the user wants to exit
if %userChoice% EQU 0 (
goto :END
)
Set /A Count-=1
rem making sure the input is valid
if %userChoice% LSS 1 (
echo Please enter a valid choice
goto :chooseEnvironment
)
if %userChoice% GTR %count% (
echo Please enter a valid choice
goto :chooseEnvironment
)
rem matching the user input with the right element from the environment list
for /f "tokens=%userChoice% delims= " %%d in ("%environment_home_list%") do (
set chosenEnv=%%d
)
:cdToEnv
rem taking the environment name to change the prompt
for /f "usebackq delims=" %%D in (`dir /b %chosenEnv%*`) do set chosenEnvName=%%D
rem setting the right path to cd to.
set chosenEnvHome=%chosenEnv%\deploy\%chosenEnvName%.ear
if not exist %chosenEnvHome% (
set chosenEnvHome=%chosenEnv%\deploy\%chosenEnvName%.war
)
if not exist %chosenEnvHome% (
set chosenEnvHome=%chosenEnv%
)
set prompt=%chosenEnvName%^>
cmd /k cd /d %chosenEnvHome%
goto :End
:concat
rem defining our list of apps installed excluding special jboss folders
set is_env_to_exclude=
for %%L in (%env_to_exclude%) do (
for /f "usebackq delims=" %%U in (`echo %1 ^| findstr %%L`) do (
set is_env_to_exclude=yes
)
)
if "X%is_env_to_exclude%" == "X" (
set environment_home_list=%1 %environment_home_list%
set env_home_list=%~n1 %env_home_list%
)
goto :eof
:getJbossVersion
for /f "usebackq tokens=2" %%v in (`echo. ^| %JBOSS_DIR%\bin\run.bat -V ^| ^
findstr "JBoss" ^| findstr /i /v "BootStrap"`) do (
set JBOSS_VERSION=%%v
)
goto :EOF
:showUsage
echo Usage: tish [environment]
echo ^|
echo +--- installed environment
:pauseforError
if "%PAUSE_ON_ERROR%" == "yes" pause
:End
My only problem is that i want the folders placed in env_home_list to be displayed alphabatically, icannot do the sort directly in the dir command, because i want to order alphabetically all the folder found and not group them by their parent folder. And i insist that i am not using a file here but only a list.
You may use the fact that Batch environment variables are automatically keept in sorted order by their names, so you need to insert the value of the variable inside the name of the variable. I suggest you to do that using the standard array naming convention and insert the value as subscript, inside the brackets:
#echo off
setlocal EnableDelayedExpansion
set list=b c a
rem Separate list elements into an array, use X as a dummy value for array elements
for %%a in (%list%) do (
set elem[%%a]=X
)
rem Process the elements in sorted order:
for /F "tokens=2 delims=[]" %%a in ('set elem[') do echo %%a
Note that this method eliminate duplicated elements and keeps just the last value. If this is a problem, you need to also insert a counter after the value (inside the brackets).

character manipulation in file cmd

this is file.txt
hello
hai
So i wanna create a program that will read this file. and then replace every ALPHABET with the succeeding one. keeping all numbers and other symbols intact.
so the file.txt would become
ifmmp
ibj
current have tried reading every \n and then reading every character in the line, but echoing them would result in it being in different lines
ie
for /f "delims=" %%i in ('^<%path% findstr /n "^"') do (
set "line=%%i"
setlocal enabledelayedexpansion
set "line=!line:*:=!"
set "num=-1"
:loop
set /a num=num+1
call set "name2=%%line:~%num%,1%%"
if defined name2 (
rem set /a name2+=1 this statement wont work
echo %name2%
goto :loop )
)
but then the output i get is
echo is off
e
l
l
o
any ideas?
The set /a command does only work with numbers, you can try this:
#echo off&setlocal
set "alfa=abcdefghijklmnopqrstuvwxyza"
set "test=hello hai"
set /a num=0
:loop
call set "char=%%test:~%num%,1%%"
if not defined char goto:eof
call set "alf1=%%alfa:*%char%=%%"
if "%char%" neq " " echo %alf1:~0,1%
set /a num+=1
goto:loop
..output is:
i
f
m
m
p
i
b
j
Here is a ROT13 batch file - what you are describing is a ROT1 translation. Maybe this will work for you as it is, or give you ideas and you can modify it.
#echo off
:: by aacini
:: program works as a filter, that is, it read lines from keyboard (stdin) and send output to screen (stdout). "type filetext.txt|rot13.bat" or "rot13.bat < filetext.txt"
:: with d benham update
#echo off
:: simple rot-13 conversion filter program
setlocal disableDelayedExpansion
set lower=abcdefghijklmnopqrstuvwxyz
set upper=ABCDEFGHIJKLMNOPQRSTUVWXYZ
for /l %%a in (0,1,25) do (
set /a "rot13=(%%a+13)%%26"
setlocal enableDelayedExpansion
for %%b in (!rot13!) do for /f "tokens=1-4" %%A in (
"!lower:~%%a,1! !lower:~%%b,1! !upper:~%%a,1! !upper:~%%b,1!"
) do (
endlocal
set "lower%%A=%%B"
set "upper%%C=%%D"
)
)
for /f "delims=" %%a in ('findstr /n "^"') do (
set "line=%%a"
setlocal enableDelayedExpansion
set "line=!line:*:=!"
set output=
if defined line (
set /a len=0
for /l %%b in (12,-1,0) do (
set /a "len|=1<<%%b"
for %%c in (!len!) do if "!line:~%%c,1!" equ "" set /a "len&=~1<<%%b"
)
for /l %%b in (0,1,!len!) do (
set "char=!line:~%%b,1!"
if defined lower!char! for /f delims^=^ eol^= %%c in ("!char!") do (
if "!lower:%%c=%%c!" neq "!lower!" (
set "char=!upper%%c!"
) else set "char=!lower%%c!"
)
set "output=!output!!char!"
)
)
echo(!output!
endlocal
)

Extract section of a text file

I am trying to write a batch file that will extract lines 6000 to 6999 in a give text file. From googling I have come accross the following code - however this is giving me a blank output file.
#echo off
SetLocal EnableDelayedExpansion
type nul > nodedata.txt
set StartText=6000
set EndText=7000
set Flag=0
for /f "tokens=* delims=" %%a in ('type out.txt') do (
if /i "%StartText%" EQU "%%a" (set Flag=1)
if /i "%EndText%" EQU "%%a" (set Flag=0)
if !Flag! EQU 1 echo %%a >> nodedata1.txt
)
Any ideas as to where I am going wrong?
Here is a quick and simple pure batch solution
for /l %%a in (6000,1,6999) do (
more C:\file.txt +%%a >>C:\extracted.txt
)
You should install unxutils and then see answers for this question... Windows is just not made for text processing...
A Windows user...
This is a Batch solution that run faster...
#echo off
SetLocal EnableDelayedExpansion
set count=0
(for /F "skip=5999 delims=" %%a in (out.txt) do (
echo %%a
set /A count+=1
if !count! equ 1000 goto endLoop
)
) > nodedata1.txt
:endLoop

Using DOS batch script: find a line in a properties file and replace text

Trying to replace a string in a properties file on a certain line by using a batch file. I know that this can be done WITHOUT the use of a temp file, as I have seen it before, but forgotten how to do it.
I know that if I have a var.properties file that contains this:
CLASSPATH=bsh.jar;other.jar
VARTEST=dummy
ANOTHERVAR=default
I am trying to update the CLASSPATH value in the .properties file without changing the order of the properties file.
This is a properties file and so I believe the answer would be related to using:
for /f "tokens=1,2* delims==" %%i in (var.properties) do (
#echo Key=%%i Val=%%j
)
Instead of findstr use find with the /v and /i switches on "classpath". This will OMIT returning the line with classpath in it, then you can echo what you want in the file along w/VARTEST=dummy
SET NEWVAL=CLASSPATH=test.jar
SET FILE=think.properties
FOR /F "USEBACKQ tokens=*" %%A IN (`TYPE "%FILE%" ^|FIND /V /I "classpath"`) DO (
ECHO CLASSPATH=%NEWVAL%>>"%FILE%"
ECHO %%A>>"%FILE%"
)
EDIT:
SETLOCAL ENABLEDELAYEDEXPANSION
SET NEWVAL=test.jar
SET OLDFILE=OLD_think.properties
SET NEWFILE=think.properties
SET COUNT=1
MOVE "%NEWFILE%" "%OLDFILE%"
FOR /F "USEBACKQ tokens=*" %%A IN (`TYPE "%OLDFILE%" ^|FIND /C /I "classpath"`) DO (
SET LINE=%%A
)
FOR /F "USEBACKQ tokens=*" %%A IN (`FIND /V "" ^<"%OLDFILE%"`) DO (
IF %COUNT% NEQ %LINE% (ECHO %%A>>"%NEWFILE%") ELSE (ECHO %NEWVAL%>>"%NEWFILE%")
SET /a COUNT=!COUNT!+1
)
Basically states,
rename think.properties to OLD_think.properties
read OLD_think.properties and find the line number with string
"classpath" in it and set it to variable LINE
Find all lines in OLD_think.properties, and echo them into think.properties. once you reach the line where your "CLASSPATH" string existed, it inserts the new line you wanted to replace it with, and everything else stays the same.
I finally broke down and accepted a method using a "temp" file. Using delayed expansion with the '!' character solved my question. Much of this success was due to input by mecaflash .
You can call this script with: CALL Script.bat PropKey NewPropValue Filename
#echo off
:: script for updating property files
SETLOCAL EnableExtensions
SETLOCAL EnableDelayedExpansion
if "%3"=="" (
ECHO Script will optionally accept 3 args: PropKey PropVal File
SET PROPKEY=UseCompression
SET PROPVAL=false
SET FILE=config.properties
) ELSE (
SET PROPKEY=%1
SET PROPVAL=%2
SET FILE=%3
)
FINDSTR /B %PROPKEY% %FILE% >nul
IF %ERRORLEVEL% EQU 1 GOTO nowork
MOVE /Y "%FILE%" "%FILE%.bak"
FOR /F "USEBACKQ tokens=*" %%A IN (`TYPE "%FILE%.bak" ^|FIND /N /I "%PROPKEY%"`) DO (
SET LINE=%%A
)
FOR /F "tokens=1,2* delims=]" %%S in ("%LINE%") DO SET LINE=%%S
SET /A LINE=%LINE:~1,6%
SET /A COUNT=1
FOR /F "USEBACKQ tokens=*" %%A IN (`FIND /V "" ^<"%FILE%.bak"`) DO (
IF "!COUNT!" NEQ "%LINE%" (
ECHO %%A>>"%FILE%"
) ELSE (
ECHO %PROPKEY%=%PROPVAL%>>"%FILE%"
ECHO Updated %FILE% with value %PROPKEY%=%PROPVAL%
)
SET /A COUNT+=1
)
GOTO end
:nowork
echo Didn't find matching string %PROPKEY% in %FILE%. No work to do.
pause
:end

Resources