Assign variables in batch script - variables

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

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.

FINDSTR in FOR loop with variables as strings (Batch File)

It's my first post here, unfotrtunately question to you instead of help.
I'm writing small program which moves text and pdf files when string is found in text file.(1.txt, 1.txt.pdf ...) It was working very good when i had variables manually set in batch like this :
set c1=xxx
set p1=c:\test\xxx\
set c2=yyy
set p2=c:\test\yyy\
FOR /f "tokens=*" %%A IN ('FINDSTR /i /m "%c1%" "c:\test\*.txt"') DO (
IF "%ERRORLEVEL%"=="0" MOVE %%A "%p1%">nul
MOVE %%A.pdf %p1%
FOR /f "tokens=*" %%A IN ('FINDSTR /i /m "%c2%" "c:\test\*.txt"') DO (
IF "%ERRORLEVEL%"=="0" MOVE %%A "%p2%">nul
MOVE %%A.pdf %p2%
Now I'm trying to use input file with variables :
for /F "delims=^ tokens=1,2" %%A in (c:\test\db\input.cdb) do (
SET /A vidx=!vidx! + 1
set c!vidx!=%%A & set p!vidx!=%%B
It works good, but the problem starts when I try to use for loop for all this variables. Literally search for all (c1,c2,c3...) variables and move them to (p1,p2,p3,...) paths.
Input.cdb looks like this :
xxx ^ c:\test\xxx
yyy ^ c:\test\yyy
Code:
setlocal ENABLEDELAYEDEXPANSION
set vidx=0
for /F "delims=^ tokens=1,2" %%A in (c:\test\db\input.cdb) do (
SET /A vidx=!vidx! + 1
set c!vidx!=%%A & set p!vidx!=%%B
FOR /f "tokens=*" %%D IN ('FINDSTR /i /m "%c!vidx!%" "c:\test\*.txt"') DO (
IF "%ERRORLEVEL%"=="0" MOVE %%D "%p!vidx!%">nul
MOVE "%%D.pdf" "%p!vidx!%"
ECHO %%D File is being procesed now
)
)
I don't know why this code doesn't work, maybe because there is loop inside loop?
Or how to create loop to do the same from 0 to how many lines of variables has been read from input.cdb?
I've tried to run this in separate for loop with temporary variable but it's still not using my string and path variables.
Thank you in advance for any help!
With Regards
Blackfusion
Ugh. More than one problem, and what is the appropriate solution?
First item is a matter of logical design. Let's assume that the batch worked as intended. c!vidx! would be set to %%A and p!vidx! to %%B, so unless there's some unpublished use for c... and p... then you could simply substitute as follows:
setlocal ENABLEDELAYEDEXPANSION
for /F "delims=^ tokens=1,2" %%A in (c:\test\db\input.cdb) do (
FOR /f "tokens=*" %%D IN ('FINDSTR /i /m "%%A" "c:\test\*.txt"') DO (
IF "%ERRORLEVEL%"=="0" MOVE %%D "%%B">nul
MOVE "%%D.pdf" "%%B"
ECHO %%D File is being procesed now
)
)
Now - there's a common fundamental misconception about %var% where var is any variable (including %ERRORLEVEL% and %C!vidx!%...) - a %VAR% occurring in a compound statement (or "block") - within the parentheses if a FOR...DO (in here) or IF...(here) ELSE (or here) is replaced with that variable's value at the time the outermost statement is parsed - before it is executed. If delayedexpansion is invoked (as you have) then !var! refers to the run-time value and %var% to the parse-time value.
Hence, if "%errorlevel%"=="0" would be evaluated according to the state of errorlevel when the for...%%A was parsed, not the value as set by the findstr.
This can be corrected by
setlocal ENABLEDELAYEDEXPANSION
for /F "delims=^ tokens=1,2" %%A in (c:\test\db\input.cdb) do (
FOR /f "tokens=*" %%D IN ('FINDSTR /i /m "%%A" "c:\test\*.txt"') DO (
IF not errorlevel 1 MOVE %%D "%%B">nul
MOVE "%%D.pdf" "%%B"
ECHO %%D File is being procesed now
)
)
Where IF not errorlevel 1 is interpreted as if the CURRENT (run-time) errorlevel is NOT (1 or greater than 1)

Saving command into variable

I've question about this variables.How can I store result of a command into a variable? For example: saving drive serial into location variable.
thanks.
E.g:
#ECHO OFF
SET location=vol
ECHO We're working with %location%
for /f "delims=" %%x in ('your command and parameters') do set "var=%%x"
where the 'single quotes' are required around the command from which you wish to save output.
Edit - now we know which command.
Comment - one variable can contain up to ~8,000 characters.
Here's a routine that will set the values in $1...$whatever and the entire set in $all with each field enclosed in angle-brackets.
#ECHO OFF
SETLOCAL
SET "$all="
FOR /f "tokens=1*delims=:" %%a IN (
'systeminfo 2^>nul^|findstr /n /r "$"'
) DO (
SET "$%%a=%%b"
FOR /f "tokens=*" %%c IN ("%%b") DO CALL SET "$all=%%$all%%<%%c>"
SET /a max=%%a
)
SET $
pause
FOR /l %%z IN (1,1,%max%) DO CALL ECHO %%$%%z%%
GOTO :EOF
thank you...please see my batch file contents:
for /f "delims=" %%x in ('systeminfo') do set "var=%%x"
command_line.exe %var%
only last line of 'systeminfo' saved in var variable and it is:
"data execution perevention available: yes"
I want to store all contetnt of systeminfo into variable!
thank you.I'm too basic
do you mean result of systeminfo is into all variable? at the end of your code can I add this command:
start command_line.exe $%all%
is this true?
can you put complete code for my batch file?

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

Importing variables from Text file, variables not incrementing

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