Creating a list of files and contents - batch-file

Trying to create a sort of vault. I have several .txt files with the account names containing the amount inside.
echo ______________________>current.txt
::this bit just resets the file ^^
setlocal enabledelayedexpansion
set Counter=1
for /f %%x in (list.txt) do (
set "Line_!Counter!=%%x"
set "temp=%%x"
set /p amountIn=<%%x.txt
set /a Counter+=1
echo %temp% vault contains>>current.txt
)
When I run this I just get " Vault contains " in current.txt. and no errors inside the CMD.
To clarify, Current.txt is my output file that i want the list in, list.txt is a list of the bank accounts, and the directory has all of the bank accounts with numbers in them.

As in my comment:
Echo ______________________>current.txt
SetLocal EnableDelayedExpansion
Set "Counter=1"
For /F %%A In (list.txt) Do (
Set "Line_!Counter!=%%A"
Set "_tmp=%%A"
Set /P "amountIn="<%%A.txt
Set /A Counter +=1
>>current.txt Echo !_tmp! vault contains !amountIn!
)
Or:
SetLocal EnableDelayedExpansion
Set "Counter=0"
(
For /F %%A In (list.txt) Do (
Set /A Counter +=1
Set "Line_!Counter!=%%A"
Set /P "amountIn="<%%A.txt
Echo %%A vault contains !amountIn!
)
)>current.txt

Related

Batch FOR loop echo asterisk

I need to search multiple files within a folder in batch script and echo the not exist file and assign a variable for further logging, but unable to achieve that whenever my pattern contain (*) mark. Anyway i can achieve this?
SET pattern="abc*.txt" "ijk_*.txt" "xyz_*.txt"
SET count=0
FOR %%A IN (%pattern%) DO (IF EXIST "%%A" (SET /a count+=1) ELSE (
ECHO %date% %time%: %%A file missing.
SET fileList=!fileList! %%A
))
You can use a trick and split pattern into a multi-line variable and than iterate over each line. I'm using the question mark ? as a delimeter because it is a reserved character and can't be used within file names.
#echo off
SETLOCAL EnableDelayedExpansion
set patternlist="abc*.txt"?"ijk _*.txt"?"xyz_*.txt"
set pattern=!patternlist:?=^
!
SET count=0
FOR /F "delims=" %%A IN (!pattern!) DO (
if exist %%A (
SET /a count+=1
) ELSE (
ECHO %date% %time%: %%A file missing.
SET fileList=!fileList! %%A
)
)

Using multiple for loops in a batch

Below is the code I am working on
setlocal enabledelayedexpansion
echo on
IF NOT EXIST "D:\Deployments\Parameter_Build*.txt" (
exit
)
set SCRIPTDIRECTORY=D:\Deployments\
PUSHD %SCRIPTDIRECTORY%
FOR %%A in (Parameter_Build*.txt) DO (
echo %%A
set ANT_HOME=C:\tibco\ant\apache-ant-1.9.13
set string=
set /p string=< D:\Deployments\%%A
echo string is !string!
echo before for loop
call :myInnerloop !string!
)
POPD
GOTO:EOF
rem *************************************************************
:myInnerloop
FOR /F "tokens=2,4,6,8,10 delims==:" %%G IN ("!string!") DO (
echo inside for loop
set COMPONENTDIR="D:\kpn_eai\EAI_FIXED\branches\kpn_eai_fixed\fixed\components"
set CONFIGDIR="D:\kpn_eai\EAI_FIXED\branches\kpn_eai_fixed\fixed\configuration\components"
set ADAPTER=%%K
echo %%G %%H %%I %%J %%K
echo value taken from file is !string! >> D:\Deployments\logs\Deployment-%%J.log 2>&1
svn update !COMPONENTDIR!\!ADAPTER! >> D:\Deployments\logs\Deployment-%%J.log 2>&1
svn update !CONFIGDIR!\!ADAPTER! >> D:\Deployments\logs\Deployment-%%J.log 2>&1
echo Starting with deployment with parameters %%G %%H %%I >> D:\Deployments\logs\Deployment-%%J.log 2>&1
%ANT_HOME%\bin\xanteai deploy %%G %%H %%I >> D:\Deployments\logs\Deployment-%%J.log 2>&1
echo Deployment completed >> D:\Deployments\logs\Deployment-%%J.log 2>&1
move D:\Deployments\Parameter_Build-%%J.txt D:\Deployments\archive\Parameter_Build-%%J.txt
RENAME D:\Deployments\logs\Deployment.txt Deployment-%%J.log
)
:next
GOTO:EOF
endlocal
Parameter_Build file contains text in below format :-
Environment=:Domain=:Component=xyz.zip|abc.zip|jkl.zip|efg.zip:Build=160:Adapter=xyz|abc|jkl|efg
Here I am trying to perform deployment for each Component
Component=xyz.zip|abc.zip|jkl.zip|efg.zip
by taking SVN update for each Adapter
xyz|abc|jkl|efg
I need to separate Component and Adapter consecutively and pass it to deploy command one by one. Also before triggering deployment I need to use each components respective adapter for taking SVN update (for eg: If component xyz.zip is triggered for deployment Adapter xyz should get updated first by SVN Update utility)
This creates the variables a[0], a[1], ... a[n]
#echo off
setlocal EnableDelayedExpansion
set "var=xyz|abc|jkl|efg"
set cnt=0
set "a[0]=%var:|="&set /a cnt+=1&set "a[!cnt!]=%"
echo cnt=!cnt!
set a[
Output:
cnt=3
a[0]=xyz
a[1]=abc
a[2]=jkl
a[3]=efg
Explanation
The code simply replaces all occurences of the delimiter with "&set /a cnt+=1&set "a[!cnt!]=.
That looks strange but as example it looks like
set "a[0]=xyz" & set /a cnt+=1 & set "a[!cnt!]=abc" & set /a cnt+=1 & set "a[!cnt!]=jkl" & set /a cnt+=1 & set "a[!cnt!]=efg"
When unrolled to multiple lines it looks like
set "a[0]=xyz"
set /a cnt+=1
set "a[!cnt!]=abc"
set /a cnt+=1
set "a[!cnt!]=jkl"
set /a cnt+=1
set "a[!cnt!]=efg"
In other words, it creates several commands per one delimiter.
This technic can be used even with string delimiters like <->
set "a[0]=%var:<->=...
Thanks to #Aacini, who introduced this technic, see split string into substrings based on delimiter
I still don't understand what you really want to do, but perhaps this may help you:
#echo off
setlocal
set "Component=xyz.zip|abc.zip|jkl.zip|efg.zip"
for %%a in ("%Component:|=" "%") do (
echo With extension: %%~a
echo Without extension: %%~Na
)
EDIT: New method added
Please, review this code:
#echo off
setlocal EnableDelayedExpansion
rem Parameter_Build file contains text in below format :-
rem Environment=:Domain=:Component=xyz.zip|abc.zip|jkl.zip|efg.zip:Build=160:Adapter=xyz|abc|jkl|efg
rem Read the file:
set /P "line=" < Parameter_Build.txt
echo Line read:
echo !line!
rem Separate variables:
set "%line::=" & set "%"
rem Separate Component and Adapter in *matching* parts
set cnt=1
set "Component[1]=%Component:|=" & set /A cnt+=1 & set "Component[!cnt!]=%"
set cnt=1
set "Adapter[1]=%Adapter:|=" & set /A cnt+=1 & set "Adapter[!cnt!]=%"
rem Ok:
for /L %%i in (1,1,%cnt%) do echo Component[%%i]=!Component[%%i]!, Adapter[%%i]=!Adapter[%%i]!
Output example:
Line read:
Environment=:Domain=:Component=xyz.zip|abc.zip|jkl.zip|efg.zip:Build=160:Adapter=xyz|abc|jkl|efg
Component[1]=xyz.zip, Adapter[1]=xyz
Component[2]=abc.zip, Adapter[2]=abc
Component[3]=jkl.zip, Adapter[3]=jkl
Component[4]=efg.zip, Adapter[4]=efg

Parent directory batch

In a previous question of mine, Aacini provided me with this code:
#echo on
setlocal EnableDelayedExpansion
set "digits=8"
set mypath=%~dp0
cd /d "%~dp0"
rem delete old list from current even if read only
del /f list.txt
rem Assemble the factor with the proper number of digits
set "factor=1"
for /L %%i in (1,1,%digits%) do set "factor=!factor!0"
rem Accumulate size of all extensions in 2 groups of %digits% digits each
for %%a in (*.*) do (
set "size=%factor%%%~Za"
set /A "low[%%~Xa]+=1!size:~-%digits%!-factor, carry=low[%%~Xa]/factor,
low[%%~Xa]%%=factor"
set "size=%%~Za"
set "size=!size:~0,-%digits%!"
set /A "high[%%~Xa]+=carry+size"
)
rem Show results
for /F "tokens=2,3 delims=[.]=" %%a in ('set high[') do (
if %%b neq 0 (
set "low=%factor%!low[.%%a]!"
echo %%a %%b!low:~-%digits%! bytes >>"%mypath%\list.txt"
) else (
echo %%a !low[.%%a]! bytes >>"%mypath%\list.txt"
)
)
Now I'm trying to make it search through the parent directory when creating the list.
I still want the final test file to be saved to the local directory, so no changes there!

While splitting txt file through batch script Exclamations are ommitting

Iam new to batch script and here I tried to split my text file into chunks for each 1 million rows.
Chunk files are generated as I expected, but inside the output file content Iam missing the Exclamations ( ! ) and even it skipping the immediate column after Exclamation. Please help me to get data as it is in original file into chunks!
#ECHO OFF
setLocal DisableDelayedExpansion
set limit=1000000
set feed_name=test.txt
set file=%Tgt_Dir%\%feed_name%
set lineCounter=1
set filenameCounter=1
set name=
set extension=
for %%a in (%file%) do (
set "name=%%~na"
set "extension=%%~xa"
)
setlocal enabledelayedexpansion
for /f "tokens=*" %%a in (%file%) do (
set splitFile=!name!%date:~12,2%%date:~4,2%%date:~7,2%!filenameCounter!!extension!
if !lineCounter! gtr !limit! (
set /a filenameCounter=!filenameCounter! + 1
set lineCounter=1
echo Created !splitFile!.
)
echo %%a>> %Tgt_Dir%\!splitFile!
set /a lineCounter=!lineCounter! + 1
)
endlocal
It is a Tab delimiter file.
ScreenShot
You need to toggle delayed expansion.
setlocal DisableDelayedExpansion
for /f "tokens=*" %%a in (%file%) do (
Set "line=%%a"
setlocal EnableDelayedExpansion
set splitFile=!name!%date:~12,2%%date:~4,2%%date:~7,2%!filenameCounter!!extension!
echo(!line!>> %Tgt_Dir%\!splitFile!
if !lineCounter! gtr !limit! (
ENDLOCAL
set /a filenameCounter+=1
set lineCounter=1
echo Created file
) ELSE ENDLOCAL
set /a lineCounter=lineCounter + 1
)

batch script to add number to a string in loop

i need to generate list of dummy users. no of users will be provided as a input.. like 5 or 500 or 5000.
all i want is to have a standard text like usr and append a number and generate the list like usr1, usr2, usr3 etc. I thought i can do this in batch file quickly. but stuck with loop and appending the number to the string.. can some help?
#echo OFF
SETLOCAL ENABLEDELAYEDEXPANSION
set /p numb="Enter how many users to be generated "
set numb2=0
set x=0
set name1=tstusr
set name1=tstusr
set name2=%name1%
for /l %%x in (1,1,%numb%) do (
echo %%x
set numb2=%%x
set name1=tstusr
set name2=%name1%%numb2%
echo %name1%
echo %name2%
)
with a slight modification of your code...
#echo OFF
SETLOCAL ENABLEDELAYEDEXPANSION
set /p numb="Enter how many users to be generated "
set numb2=0
set x=0
set name1=tstusr
set name1=tstusr
set name2=%name1%
for /l %%x in (1,1,%numb%) do (
echo %%x
set numb2=%%x
set name1=tstusr
set name2=!name1!!numb2!
echo !name1!
echo !name2!
)
endlocal
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
set /p numb="Enter how many users to be generated "
>q28342811.txt ECHO tstusr%random%
:loop
set "name=tstusr%random%"
FINDSTR /x "%name%" q28342811.txt >NUL
IF ERRORLEVEL 1 SET /a numb -=1&>>q28342811.txt ECHO %name%
IF %numb% gtr 1 GOTO loop
TYPE q28342811.txt
GOTO :EOF
Produces q28342811.txt
generate a random username to the output file, then repeat the operation numb-1 times, checking that the new candidate doesn't already exist in the file first.

Resources