Getting a files line and setting as a variable

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
skaliam
Posts: 8
Joined: 23 Jul 2012 12:09

Getting a files line and setting as a variable

#1 Post by skaliam » 23 Jul 2012 12:29

Hi guys,
Im writing a script so that it takes a line from a file (numbers.txt) and then converts that number into an IP
I've got it to convert a number from direct input but what i want to do is:
Take first line of file and use a first variable
Run a few commands with the variable
Once command has been run go to next line and use a next variable
I have about 1300 numbers in the numbers.txt file

so far ive got:

Code: Select all

for /F %%i in (numbers.txt) do set content=%%i


I am then not able to use the variable as it just goes to the next line and outputs it.

Squashman
Expert
Posts: 4472
Joined: 23 Dec 2011 13:59

Re: Getting a files line and setting as a variable

#2 Post by Squashman » 23 Jul 2012 12:35

Code: Select all

for /F %%i in (numbers.txt) do (
set content=%%i
Do the rest of your stuff here
Do more stuff.
And more commands
)

skaliam
Posts: 8
Joined: 23 Jul 2012 12:09

Re: Getting a files line and setting as a variable

#3 Post by skaliam » 23 Jul 2012 13:04

Well that kind of worked

But the problem is, I have a lot of sub routines and im not able to put them in there
also it doesn't go to the next line ive now got

Code: Select all

for /F "skip="%line% %%i in (numbers.txt) do (
set STR=%%i
goto convert
)

Squashman
Expert
Posts: 4472
Joined: 23 Dec 2011 13:59

Re: Getting a files line and setting as a variable

#4 Post by Squashman » 23 Jul 2012 13:12

skaliam wrote:Well that kind of worked

But the problem is, I have a lot of sub routines and im not able to put them in there
also it doesn't go to the next line ive now got

Code: Select all

for /F "skip="%line% %%i in (numbers.txt) do (
set STR=%%i
goto convert
)

What kind of FOR Loop is that?

You need to better explain what you are trying to do.

skaliam
Posts: 8
Joined: 23 Jul 2012 12:09

Re: Getting a files line and setting as a variable

#5 Post by skaliam » 23 Jul 2012 13:19

Sorry,
Okay so the logic of the batch file is:

get a number from numbers.txt
use that number and convert that into an ip address
check if that ip is online
if not go to start and get next number from numbers.txt
if ip is online
delete a file on ip address
go back to start and get the next number from numbers.txt

Squashman
Expert
Posts: 4472
Joined: 23 Dec 2011 13:59

Re: Getting a files line and setting as a variable

#6 Post by Squashman » 23 Jul 2012 13:22

skaliam wrote:Sorry,
Okay so the logic of the batch file is:

get a number from numbers.txt
use that number and convert that into an ip address
check if that ip is online
if not go to start and get next number from numbers.txt
if ip is online
delete a file on ip address
go back to start and get the next number from numbers.txt

Pretty easily done all inside a for loop.
Not sure what the Convert IP step is. Maybe you could enlighten us with what is inside the numbers.txt file and how it is converted.

skaliam
Posts: 8
Joined: 23 Jul 2012 12:09

Re: Getting a files line and setting as a variable

#7 Post by skaliam » 23 Jul 2012 13:47

okay so the numbers file has site numbers say 1 - 1300 or so each on separate lines
The first octet is always 10
the second octet is calculated by dividing the site number by 256 then adding 160
the third octet is calculated if the site number is bigger than 255 then minus 256 untill it is below

so for example
123 / 256 = 0.48046875 + 160 = 160
123 is lower than 255 so keep it as 255
so the ip will be:
10.160.123.whatever

Is that more helpful?

Squashman
Expert
Posts: 4472
Joined: 23 Dec 2011 13:59

Re: Getting a files line and setting as a variable

#8 Post by Squashman » 23 Jul 2012 13:58

What about the 4th octet?

skaliam
Posts: 8
Joined: 23 Jul 2012 12:09

Re: Getting a files line and setting as a variable

#9 Post by skaliam » 23 Jul 2012 14:03

Well the site consists of the second and third octect and they have a number of devices connected at each site.
Each site has the same sort of setup and the fourth octet will stay the same depending on the device

Squashman
Expert
Posts: 4472
Joined: 23 Dec 2011 13:59

Re: Getting a files line and setting as a variable

#10 Post by Squashman » 23 Jul 2012 15:59

I don't have another pc to test this with. I think it will work.

Code: Select all

@echo off
setlocal enabledelayedexpansion

FOR /F "delims=" %%G in (numbers.txt) do (
   SET octet1=10
   SET octet4=100
   SET octet2=%%~G / 256 + 160
   call :octet3 %%G octet3
   PUSHD "\\!octet1!.!octet2!.!octet3!.!octet4!\C$"
   IF "!errorlevel!"=="0" (
      DEL "\\!octet1!.!octet2!.!octet3!.!octet4!\C$\folder\file.txt"
      POPD
   )
)
GOTO :EOF


:octet3
set octet=%1
:Loop
IF %octet% GTR 255 (
   set /a octet=%octet%-256
   goto :Loop
)
set %2=%octet%
goto :EOF

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Getting a files line and setting as a variable

#11 Post by foxidrive » 23 Jul 2012 17:19

Assuming you have the code already to compare, calculate and check-if-online etc then this is a method you can use.

Code: Select all

@echo off
for /F "delims=" %%i in (numbers.txt) do (
set STR=%%i
call :convert
)
pause
goto :EOF

:convert
echo %STR%
rem do more commands here

Post Reply