Trying to obtain an element in a list by its index, using batch script. Here is the code:
#Echo off
setlocal EnableDelayedExpansion
set acc[0]=default
set acc[1]=Account_2
set acc[2]=Account_3
set acc[3]=Account_4
set acc[4]=Account_5
if exist interator.txt (
set /p i=<interator.txt
echo "read: !i!"
echo "!acc[%i%]!"
REM start cmd /c setx AWS_PROFILE !acc[%i%]!
REM start cmd /k python script.py
set /A i=i+1
(echo !i!)>interator.txt
echo "write: !i!"
) else (
(echo 0)>interator.txt
)
Output Received:
"read: 0"
""
"write: 1"
As setx requires the CMD session to be closed, for affect to take place. I am trying a different approach to automate some regular stuff.
Expected Output:
"read: 0"
"default"
"write: 1"
#Echo off
setlocal EnableDelayedExpansion
set "acc[0]=default
set "acc[1]=Account_2"
set "acc[2]=Account_3"
set "acc[3]=Account_4"
set "acc[4]=Account_5"
if exist q65771965.txt (
set /p i=<q65771965.txt
echo "read: !i!"
FOR %%a IN (acc[!i!]) DO (
ECHO "!%%a!"
echo start cmd /c setx AWS_PROFILE "!%%a!"
echo start cmd /k python script.py
)
set /A i=i+1
(echo !i!)
echo "write: !i!"
) else (
(echo 0)
)
GOTO :EOF
OK - small changes to allow this to work on my test environment:
Changed name of file from interator.txt to q65771965.txt (suits my environment)
Removed updating of data file so the modifications are shown on-screen.
Replaced REM start with ECHO start to show the start commands on-screen.
Subtle syntax-oriented change : Use set "var1=data" for setting values - this avoids problems caused by trailing spaces.
Significant change : insert a for loop to transfer indirect values to a metavariable (%%a) and use these.
Possibly-required : I don't use setx much, but I've some memory of the argument's needing to be "quoted"
The problem is, you used echo "%acc[!i!]%" within a codeblock. You need another layer of parsing, like call echo "%%acc[!i!]%%"
As an alternative, restructure your code, so the critical part isn't in a code block:
#Echo off
setlocal EnableDelayedExpansion
set acc[0]=default
set acc[1]=Account_2
set acc[2]=Account_3
set acc[3]=Account_4
set acc[4]=Account_5
if not exist interator.txt (
(echo 0)>interator.txt
goto :eof
)
set /p i=<interator.txt
echo "read: !i!"
echo "%acc[!i!]%"
set /A i=i+1
(echo !i!)>interator.txt
echo "write: !i!"
(this code is functionally identically to yours, just structured in another way)
(btw: it should probably iterator, not interator - but that's only spelling)
I need help in to provide output that contain a blank space after each of 2 lines from .txt file containing number of lines without any blank in between.
Example :
Suppose a .txt file name as media.txt contains below input
A
B
C
D
.
.
.
Z
I want to represent above input into output like :
A
B
-----
C
D
----
E
F
----
.
.
.
.
I tried by using odd even concept but failing them to represent in above mentioned output.
Here, is my code in batch script:
set flag=1
for /f "tokens=2 delims==" %%a in ('type media.txt^|findstr "output"') do (
if %flag%%%2==0 (
echo %%a
) else (
echo.
echo ------------------
echo %%a
set flag+=1
)
)
Please let me know, where i am doing wrong..
This can easily be done in a batch-file or cmd with PowerShell. If you are on a supported Windows system, PowerShell will be available.
powershell -NoLogo -NoProfile -Command ^
"Get-Content .\media.txt -ReadCount 2 | ForEach-Object { $_; Write-Output '----' }"
You can't use if with a modulus and no % around a variable, and you can't check the current value of a variable in a loop without delayed expansion or some other hoops.
This should do the needful.
#(SETLOCAL ENABLEDELAYEDEXPANSION
ECHO OFF
SET /A "Flag=0"
SET /A "Mod=3"
)
CALL :Main
( ENDLOCAL
EXIT /B
)
:Main
For /f "tokens=2 delims==" %%_ in ('
Type media.txt^|findstr "output"
') do (
SET /A "flag= (flag + 1) % mod"
IF !flag! == 0 (
echo.
echo.------------------
)
echo.%%_
)
I have data in the below manner:
adb.hghgjjk.hkdhdl.Connhhhjj=hjkld\:hjkld\:thin\:hjdkdl\:3000\:abcdefg
I want to extract "abcdefg" from the above line using batch.
Above data can be changing so we need to read from the end of the line to backside and stop near : (colon) delimiter and extract it.
Any sugeestions?
Normally we do not give out code without the user at least making an attempt at trying to write some code and put some effort into researching the problem. Simple philosophy behind that is teaching a man to fish versus giving them the fish.
Give this a try.
#echo off
set "string=adb.hghgjjk.hkdhdl.Connhhhjj=hjkld:hjkld:thin:hjdkdl:3000:abcdefg"
set "first=%string::=" & set "last=%"
echo %last%
pause
If your data will never have a semicolon that you need to keep you could do this as well.
#echo off
set "string=adb.hghgjjk.hkdhdl.Connhhhjj=hjkld:hjkld:thin:hjdkdl:3000:abcdefg"
set "string=%string::=;%"
FOR %%G IN (%string%) do set "last=%%G"
echo %last%
pause
And one more example for good measure. The nice thing about batch files is alot of times there is more than one way to skin a cat.
#echo off
set "string=adb.hghgjjk.hkdhdl.Connhhhjj=hjkld:hjkld:thin:hjdkdl:3000:abcdefg"
:loop
FOR /F "tokens=1* delims=:" %%G IN ("%string%") do (
set "last=%%G"
IF NOT "%%~H"=="" (
SET "string=%%~H"
GOTO loop
)
)
echo %last%
pause
PowerShell can use regex expressions. The result is put into the R variable. If there is no match, R will be nothing.
#ECHO OFF
SET "S=adb.hghgjjk.hkdhdl.Connhhhjj=hjkld\:hjkld\:thin\:hjdkdl\:3000\:abcdefg"
SET "R="
FOR /F %%a IN ('powershell -NoLogo -NoProfile -Command ^
" '%S%' | Where-Object { $_ -match '.*:(.*$)' } | ForEach-Object { $Matches[1] } "') DO (SET "R=%%a")
ECHO R is %R%
I have this block of code:
#ECHO OFF
SET "SRCFOLDER=C:\Users\MyUserName\Desktop\PhotoTests"
SET "TEMPCODE=Hi"
ECHO %TEMPCODE%
ECHO.
FOR /F "tokens=*" %%G IN ('DIR /B %SRCFOLDER%') DO (
ECHO %%G
CALL tooltipInfo.bat %%G 19 | FIND /C "Star" > test.txt
SET /P TEMPCODE=<test.txt
ECHO %TEMPCODE%
ECHO.
)
SET /P TEMPCODE=<test.txt
ECHO %TEMPCODE%
ECHO.
PAUSE
I'm confused by the output as I noticed that the variable in the FOR loop is not overwritten by what I think should be the content of "test.txt", which will vary each time the FOR loop runs.
For the purpose of this example, the file tooltipInfo.bat will echo a text string like "1 Star" or "3 Stars" based on the rating recorded in the file's properties. The FIND statement should result in a "0" or "1" being saved into the test.txt file.
The output is:
Hi
Canon_Locked.JPG
Hi
Nikon_Locked.JPG
Hi
0
Press any key to continue . . .
May I know why the TEMPCODE variable isn't being overwritten in the loop and retains the original value of "Hi". However in the final block of code, it was able to read and echo out the actual content of the file.
This is a common mistake when using FOR and parentheses. The problem is that by default every variable between ( and ) is evaluated when the line is first read, and then re-used for each iteration of the FOR.
If the variable changes during the loop (via SET), then you will need to change % to ! for variables inside the parentheses, and also turn on delayed expansion using setlocal
SETLOCAL enabledelayedexpansion
FOR /F "tokens=*" %%G IN ('DIR /B %SRCFOLDER%') DO (
ECHO %%G
CALL tooltipInfo.bat %%G 19 | FIND /C "Star" > test.txt
SET /P TEMPCODE=<test.txt
ECHO !TEMPCODE!
ECHO.
)
I'm automating some source control software functionality using a dot bat script but given that our svn repos are hosted in a *NIX box, I'm facing the eternal case problem between these two worlds.
Is there any cmd.exe function to convert the value of the Windows system variable %USERNAME% to lower case?
Thanks much in advance!
Well, I was browsing for some syntax and stumbled upon this page. I know its old but I thought I'd take a break and give the brain a little kick.
Here's something a little shorter and manageable. This just "brute forces" all uppercase letters to lowercase letters without regards to whether the actual letter exists in the string or not. Thus the functional loop runs exactly 26 times no matter the length of the string.
Hope this helps someone.
#echo off
cls
setlocal enabledelayedexpansion
REM ***** Modify as necessary for the string source. *****
set "_STRING=%*"
if not defined _STRING set "_STRING=%USERNAME%"
set _STRING
REM ***** Modify as necessary for the string source. *****
set "_UCASE=ABCDEFGHIJKLMNOPQRSTUVWXYZ"
set "_LCASE=abcdefghijklmnopqrstuvwxyz"
for /l %%a in (0,1,25) do (
call set "_FROM=%%_UCASE:~%%a,1%%
call set "_TO=%%_LCASE:~%%a,1%%
call set "_STRING=%%_STRING:!_FROM!=!_TO!%%
)
set _STRING
endlocal
Example:
E:\OS.ADMIN>LCASE.BAT The Quick Fox Jumps Over The Brown Fence.
Result:
_STRING=The Quick Fox Jumps Over The Brown Fence.
_STRING=the quick fox jumps over the brown fence.
a quick google found this...
#echo off
goto :end_remarks
*************************************************************************************
*
*
* authored:Sam Wofford
* Returns lowercase of a string
* 12:13 PM 11/13/02
**************************************************************************************
:end_remarks
setlocal
set errorlevel=-1
if {%1}=={} echo NO ARG GIVEN&call :Help &goto :endit
if {%1}=={/?} call :Help &goto :endit
call :set_LCASE_array a b c d e f g h i j k l m n o p q r s t u v w x y z
:start
set input=%1
set input=%input:"=%
set totparams=0
call :COUNT_PARAMS %input%
call :MAKE_LOWERCASE %input%
set errorlevel=
echo %convertedstring%
endlocal
goto :eof
:endit
echo %errorlevel%
endlocal
goto :eof
:MAKE_LOWERCASE
:nextstring
if {%1}=={} goto :eof
set string=%1
set /a params+=1
set STRINGCONVERTED=
set pos=0
:NEXT_CHAR
set onechar=%%string^:^~%pos%,1%%
for /f "tokens=1,2 delims==" %%a in ('set onechar') do for /f %%c in ('echo %%b') do call :checkit %%c
if not defined STRINGCONVERTED goto :NEXT_CHAR
shift /1
if %params% LSS %totparams% set convertedstring=%convertedstring% &:add one space,but not at end
goto :nextstring
goto :eof
:Help
echo USAGE:%~n0 string OR %~n0 "with spaces"
echo function returns the lowercase of the string or -1 (error)
echo strings with embedded spaces needs to be in quotes Ex. "lower case"
echo in a batch NTscript "for /f %%%%A in ('lcase STRING') do set var=%%%%A"
set errorlevel=
goto :eof
:checkit
set LCFOUND=
if /i {%1}=={echo} set STRINGCONVERTED=Y&goto :eof
set char=%1
for /f "tokens=2 delims=_=" %%A in ('set LCASE_') do call :findit %%A %char%
:skipit
if defined LCFOUND (set convertedstring=%convertedstring%%ucletter%) else (set convertedstring=%convertedstring%%char%)
set /a pos+=1
goto :eof
:set_LCASE_array
:setit
if {%1}=={} goto :eof
set LCASE_%1_=%1
SHIFT /1
goto :setit
:findit
if defined LCFOUND goto :eof
set ucletter=%1
set lcchar=%2
if /i {%ucletter%}=={%lcchar%} set LCFOUND=yes
goto :eof
:COUNT_PARAMS
:COUNTPARAMS
if {%1}=={} goto :eof
set /a totparams+=1
shift /1
goto :COUNTPARAMS
add that as a file (lowercase.cmd) to your path and you should be able to call it as "Lowercase.cmd %Username%", you could pipe it into another command if needed.
download some unix utilities for DOS from http://short.stop.home.att.net/freesoft/unix.htm
and use tr.exe (translate characters)
echo %USERNAME% | tr "[A-Z]" "[a-z]"
I also use a DOS extended cmd replacement named 4NT which has a built in command #lower
echo %#lower[%USERNAME%]
http://www.dzone.com/snippets/lowercasing-string-bat-files
lower.bat
echo>%1
dir /b/l %1>lower.tmp
set /p result=<lower.tmp
echo %result%
cmd
lower "Mein BinnenMajuskel"
result
mein binnenmajuskel
CAUTION: Quick & dirty, but also insecure and dangerous variant. Because you create two files. One called like the given string and another called lower.tmp, which contains the lowered string. What happens if you execute lower "UserName" in a directory, where this file or directory already exists? Especially if you delete this files afterwards ...
Improved version:
echo>%Temp%\%1
dir /b/l %Temp%\%1>%Temp%\lower.tmp
set /p result=<%Temp%\lower.tmp
del %Temp%\%1
del %Temp%\lower.tmp
When a scripting language is installed then that can be used with a FOR to set a variable.
#FOR /F "delims=" %%s IN ('<<some script oneliner>>') DO #set MYVARIABLE=%%s
Reference: For F Loop
Any scripting language can be used if it can convert a string to lowercase and output the result.
An example using Perl 5 :
#FOR /F "delims=" %%s IN ('perl -e "print lc(pop)" %USERNAME%') DO #set USERNAME=%%s
An example using PowerShell :
#FOR /F "delims=" %%s IN ('powershell -command "(get-item env:'USERNAME').Value.ToLower()"') DO #set USERNAME=%%s
These days, odds are that PowerShell is already installed by default.
In my batch file I'm doing a comparsion between %USERNAME% and a CSV file.
The program would not work if user was logged in UperCase username.
Ex:
Login : GB2NOGU // Won't work
Login : gb2nogu // Works
Here I could solve my problem doing a insensitive comparison.
if /i %USERNAME%==gb2nogu (
// Code here
)
The parameter /i tells the cmd to do a insensitive case comparison, so it'll ignore the difference between lowercase and uppercase letters.
Probably this is the fastest way to convert a string to lowercase in batch file as it uses macro and there are no temp files (it saves the produced string in variable called result):
#echo off
set LowerCaseMacro=for /L %%n in (1 1 2) do if %%n==2 (for %%# in (a b c d e f g h i j k l m n o p q r s t u v w x y z) do set "result=!result:%%#=%%#!") else setlocal enableDelayedExpansion ^& set result=
set "string=SOme STrinG WiTH lowerCAse letterS and UPCase leTTErs"
%LowerCaseMacro%%string%
echo %result%
:: UPcase.bat ==> Store in environment variable _UPcase_ the upper case of %1
:: -> Use quotes "" when the first argument has blanks or special characteres
::
:: Adapted from -> http://www.netikka.net/tsneti/info/tscmd039.htm
::
:: Note that the substitution method is case insensitive, which means that
:: while working for this application, it is not useful for all character
:: substitution tasks.
::
:: More concisely, one can capitalize (if you pardon the pun) on the fact
:: that in for and the substitution lower and upper case source are
:: equivalent.
#echo off
:: %~1 -> removes quotes from the first command line argument
:: http://steve-jansen.github.io/guides/windows-batch-scripting/part-2-variables.html
#echo off
::setlocal EnableExtensions
:: echo %_UPcase_%
call :ToUpcaseWithFor "%~1" _UPcase_
:: echo %_UPcase_% _doit_1_
::endlocal & goto :EOF
goto :EOF
::
:: ======================
:ToUpcaseWithFor
setlocal EnableExtensions EnableDelayedExpansion
set var_=%~1
for %%c in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
set var_=!var_:%%c=%%c!
)
endlocal & set %2=%var_%& goto :EOF
:EOF
:: UPcase.bat ==> EOF
This is the same answer /by #It Wasn't Me here
For a predictive character set, this substring Set !var:A=a! works, and only working with predefined substring in bat/cmd.
For this type of task, why not get a little help with c#, which can make it possible to work with unconventional accents and consonants è, È, ä, Ä, ñ, Ñ, ç, Ç etc.
Where the bat/cmd will generate c# code, it will be compiled and executed at run time ....
Which solves possible user inputs, in which the sequence comes with accents and the vowels/consonants are different from the conventional ones [a-z] and/or [A_Z]
#echo off & setlocal EnableDelayedExpansion
cd /d "%~dp0" && title <nul && title ...\%~dpnx0 /// !time:~0,8! !date!
if exist "%tmp%\ToUpLower.cs" 2>nul >nul del /q /f "%tmp%\ToUpLower.cs"
set "_where=%__appdir__%where.exe" && set "_csc=%windir%\Microsoft.NET"
>"%temp%\ToUpLower.cs" (
echo= using System; namespace SUQ1522019 ^{class Program ^{static void Main(string[] args^) ^{
echo= if (args.Length==2 ^&^& args[0].ToLower(^)=="-l"^) ^{Console.WriteLine(args[1].ToLower(^)^);^}
echo= if (args.Length==2 ^&^& args[0].ToLower(^)=="-u"^) ^{Console.WriteLine(args[1].ToUpper(^)^);^}^}^}^}
)
set "_arg=/t:exe /out:"%tmp%\ToUpLower.exe" "%tmp%\ToUpLower.cs" /platform:anycpu "
for /f tokens^=* %%i in ('!_where! /r "!_csc!" "csc.exe"^|findstr /lic:"k\v2\."
')do "%%~i" !_arg! /unsafe+ /w:0 /o /nologo
for /f tokens^=* %%U in ('"%tmp%\ToUpLower.exe" -u %USERNAME%')do set "_up_case=%%U"
for /f tokens^=* %%l in ('"%tmp%\ToUpLower.exe" -l %USERNAME%')do set "_low_case=%%l"
echo/ Your username upcase is: !_up_case!
echo/ Your username lowcase is: !_low_case!
echo/ >nul 2>nul copy "%tmp%\ToUpLower.exe" "."
del /q /f "%tmp%\ToUpLower.*" >nul 2>nul && endlocal & goto :EOF
Outputs for %USERNAME%
Your username upcase is: USERNAME
Your username lowcase is: username
The ToUpLower.cs c# code with no escaping:
using System; namespace SUQ1522019 {class Program {static void Main(string[] args) {
if (args.Length==2 && args[0].ToLower()=="-l") {Console.WriteLine(args[1].ToLower());}
if (args.Length==2 && args[0].ToLower()=="-u") {Console.WriteLine(args[1].ToUpper());}}}}
The ToUpLower.cs c# code with no escaping and indented:
using System
namespace SUQ1522019
{
class Program
{
static void Main(string[] args)
{
if (args.Length==2 && args[0].ToLower()=="-l")
{
Console.WriteLine(args[1].ToLower());
}
if (args.Length==2 && args[0].ToLower()=="-u")
{
Console.WriteLine(args[1].ToUpper());
}
}
}
}
This c# code was compiled/tested on csc.exe versions:
c:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe
c:\Windows\Microsoft.NET\Framework\v3.5\csc.exe
c:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe
c:\Windows\Microsoft.NET\Framework64\v2.0.50727\csc.exe
c:\Windows\Microsoft.NET\Framework64\v3.5\csc.exe
c:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe
This is the command line used to compile the c# code:
c:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe /t:exe /out:"%tmp%\ToUpLower.exe" "%tmp%\ToUpLower.cs" /platform:anycpu /unsafe+ /w:0 /o /nologo
ToUpLower.exe usage UPPER to -> lower
ToUpLower.exe -l STRING
:: or ..
ToUpLower.exe -L STRING
ToUpLower.exe usage lower to -> UPPER
ToUpLower.exe -u string
:: or ..
ToUpLower.exe -U string
To keep ToUpLower.exe, remove echo/ from copy command:
echo/ >nul 2>nul copy "%tmp%\ToUpLower.exe" "."
This command line will copy ToUpLower.exe from %temp% to same the same folder where your bat is running.
Programming Guide C#:
Args
ToLower
ToUpper
Sorry my limited English