Importing variables from Text file, variables not incrementing - input

My goal is to be able to go into a specific folder, run a task, then when that task is completed, move to another folder and repeat.
#echo off
setlocal ENABLEDELAYEDEXPANSION
set vidx=0
for /F "tokens=*" %%A in (z:\desktop\cookieclean\test.txt) do (
SET /A vidx=!vidx! + 1
set var!vidx!=%%A
)
set var
echo %var!vidx!%
set /a vidx+=1
echo %var!vidx!%
endlocal
That is the code I am working on. I copied the code from somewhere else on the web and tried to get it to do what I wanted. the code above is just me attempting to take the test file, throwing in the 3 lines of text as the variables, then having it increment.
I'm sure it's something super simple, but I've been trying to get this to work for hours, and my rookie-ness is showing.
A pseudo code of what I'm TRYING to do would be
Input variables from text
Open folder containing files
Delete files that are older than X days
Move active folder to next folder in directory
Delete files that are older than X days
Loop
End
I'm just trying to use the cmd line.

You have your delayed and normal expansion reversed. You want the inner variable expanded before the outer one: !var%vidx%!
Your SET /A statement in your loop works, but you do not need to expand the variable. This would work just as well: SET /A vidx=vidx + 1, or better yet: SET /A vidx+=1
I also believe your logic to show results is incorrect. The code below should work.
#echo off
setlocal enableDelayedExpansion
set "vidx=0"
for /f "tokens=*" %%A in (z:\desktop\cookieclean\test.txt) do (
set /a vidx+=1
set "var!vidx!=%%A"
)
set var
set "cnt=vidx"
set "vidx=1"
:loop
if %vidx% leq %cnt% (
echo !var%vidx%!
set /a vidx+=1
goto :loop
)
endlocal
Here is a much simpler and more efficient method using FOR /L instead of a GOTO loop.
#echo off
setlocal enableDelayedExpansion
set "vidx=0"
for /f "tokens=*" %%A in (z:\desktop\cookieclean\test.txt) do (
set /a vidx+=1
set "var!vidx!=%%A"
)
set var
for /l %%N in (1 1 %vidx%) do echo !var%%N!
endlocal

Related

How to assign variables from text file (batch only!)

best user,
I tried to assign variables from a txt file in batch. I have a text file called "Final.txt".
Final.txt:
0216027232411405
02160272b6172505
1115fb9c6f423305
1645fb9c6f423305
0000fb9c6f423305
script that I tested till now:
#echo off
setlocal ENABLEDELAYEDEXPANSION
set vidx=0
for /F "tokens=*" %%A in (Final.txt) do (
SET /A vidx=!vidx! + 1
set var!vidx!=%%A
)
set var
(
set /p var1=
set /p var2=
set /p var3=
)<Final.txt
#echo off
setlocal enabledelayedexpansion
set Counter=1
for /f %%x in ("Final.txt") do (
set "Line_!Counter!=%%x"
set /a Counter+1
if Counter== 0 goto :EOF
)
set /a NumLines=Counter - 1
echo %Line_1%
pause
:EOF
Echo please connect a device
pause
exit
The problem is that the code works, but it only assign the first line of the text file. I want to assign every line in the text file to a variable.Now I only have 5 devices,but in the future iw till expand to more lines. I will do more research, but if you have a solution. Your answers are always welcome!. If I can figure it out I am going to use it in my other script so you are a life saver. I only want to use batch for this.

Assign variables in batch script

I'm new in Windows batch programming and I have found problems in the variable assignment. This is my code:
#echo off
setlocal enabledelayedexpansion
set Video=1
set FILEMEDIA=outputMedia.txt
for /f %%a in (%FILEMEDIA%) do (
set /a Video=%Video%+1
#echo Video
set file=%%a
#echo file
)
If FILEMEDIA has two lines I would like to obtain Video=2 and the line in file variable. However, at the end I obtain Video=1 and an error when I tried to print file (echo is off).
Some kind of duplicate with How do I increment a DOS variable in a FOR /F loop?
Variables that should be delay expanded are referenced with !VARIABLE! instead of %VARIABLE%.
#echo off
setlocal enabledelayedexpansion
set Video=1
set FILEMEDIA=outputMedia.txt
for /f %%a in (%FILEMEDIA%) do (
set /a Video+=1
#echo !Video!
set file=%%a
#echo file
)
endlocal

Recalling variables in a command prompt loop

I am setting a number of variables using the for /f command.
setlocal ENABLEDELAYEDEXPANSION
set vidx=0
for /F "tokens=*" %%A in (target_list.txt) do (
SET /A vidx=!vidx! + 1
SET var!vidx!=%%A
)
set var
Now that I have set them I need to be able to be able recall them in a loop and apply them to the following commands.
copy gateway%num%.bat \\%var1%\C$\WINDOWS\system32
psexec \\%var1%\ gateway%num%.bat
del \\%var1%\\C$\WINDOWS\system32\gateway%num%.bat
The reason I need them in a loop is because the number of variables will change periodically and I need it to be able to figure out how many it made in the previous command and then apply them in the second command. I don't want to have to copy this command over and over and only change (var1) to (var2) to (var3) etc.
try this:
for /l %%i in (1,1,%vidx%) do copy copy gateway%num%.bat \\!var%%i!\C$\WINDOWS\system32
for /l %%i in (1,1,%vidx%) do psexec \\!var%%i!\ gateway%num%.bat
for /l %%i in (1,1,%vidx%) do del \\!var%%i!\\C$\WINDOWS\system32\gateway%num%.bat

Get variable values used inside a loop for outside of the loop

I need to get a part of some lines(Myfile) out of the 'for' loop either into two separate variables or into an array.My goal is to use each other for constituting path of the following lines result*.txt. I write what you are seing below but it's visible that my 'a' variable is only echoing 'C:\Temp\FR'(the end of line matching token in Myfile).
Please, Is there any way to get all matching tokens in a variable/array? For example, in my case, I need to get 'C:\Temp\USA' and 'C:\Temp\FR' out of the loop for? Thanks
for /f "tokens=2 delims==" %%x in (Myfile) do (
set a=%%x
)
echo %a%
'MyFile
DIR1= C:\Temp\USA
DIR2= C:\Temp\FR
result1.txt
result2.txt
Try this:
#echo off&setlocal
for /f "tokens=1* delims== " %%x in (Myfile) do (
if /i "%%x"=="dir1" set "path1=%%y"
if /i "%%x"=="dir2" set "path2=%%y"
)
echo %path1%
echo %path2%
Output is:
C:\Temp\USA
C:\Temp\FR
There are several ways to do that. If you want read a few lines from the beginnng of the file, the easiest way is to directly read them in a couple variables:
< Myfile (
set /P var1=
set /P var2=
)
Note that these variables contain the complete lines, so it is necessary to extract the part after the equal sign.
In your example you just want the lines with equal sign, so we may use findstr "=" Myfile in a FOR command to just process they. If the number of lines is undefined, you must save all desired lines in an array as you suggested:
setlocal EnableDelayedExpansion
set n=0
for /F "tokens=2 delims==" %%a in ('findstr "=" Myfile') do (
set /A n+=1
set var[!n!]=%%a
)
rem Show all array elements
for /L %%i in (1,1,%n%) do (
echo !var[%%i]!
)
rem Show just the second element
echo %var[2]%
However, in your particular case the lines you want have the form of variable assignments. This means that if you execute such lines with a SET command, the values in the lines will be "automatically" saved:
for /F "delims=" %%a in ('findstr "=" Myfile') do set %%a
After previous FOR , you may directly get the values of DIR1 or DIR2 variables, for example:
echo %DIR1%
echo %DIR2%
This method is very simple and don't require any testing on the individual variables being loaded.
If you want to remove the space after the equal sign in the value, you may adjust FOR options to do so:
for /F "tokens=1* delims== " %%a in ('findstr "=" Myfile) do set %%a=%%b

How to add variables in a batch file

I'm trying to get a list of numbers an letters like 11111111 and a letter. Letter si calculated with the number. Something like:
#echo off
set var=23
FOR /L %%H IN (40000000,1,49999999) DO (
set number = %%H+%var%
echo %number%A
)
pause
exit
I want to get (more or less)
400000023A
400000024A
400000025A
...
But without changing the FOR sentence... Is that possible?
To continue with what jeb stated, for your requested output, the code should be this:
#echo off & setlocal EnableDelayedExpansion
set var=23
FOR /L %%H IN (40000000,1,49999999) DO (
set /a number=%%H+!var!
echo !number!A
)
endlocal
pause
exit
Avoid spaces with set statements
Use set /a if you want to calculate
set /a number=%%H+var
Avoid percent expansion in a block (and also in a FOR block)
Use delayed expansion instead (add a setlocal EnableDelayedExpansion to your file)
echo !number!A
To use numbers in batch use
set /a
See set /? for all the info.
jeb provided the hints needed to fix your code, and Jeff K actually implemented the fix.
However, since you are adding a constant to every value, it is simpler and more efficient to add the constant to the FOR /L start and end values.
#echo off
setlocal
set var=23
set /a "start=40000000+var, end=49999999+var"
for /l %%H in (%start%, 1, %end%) do echo %%HA
There is, but with changing the FOR sentence.
You actually forgot the setlocal command right there.
setlocal EnableDelayedExpansion
This command is needed in order to add variables.
Still don't get it?
Here's the code so that you will add variables.
#echo off
setlocal EnableDelayedExpansion
cls
set var=17
echo %var%
pause
set /a var=%var%+7
echo %var%
pause
Try to find any errors and mistakes on my code. Then test it.
There you have it! Added variables!
The best method is:
set /a counter+=1
so:
#echo off
set /a counter=
:Top
set /a counter+=1
echo 4000000%counter%A
GOTO Top
You can copy the code above.