Foxb2328
Foxb2328

Reputation: 41

Batch file to run through a text file and create an IP addesss based on each line

I'm trying to take a list of user numbers in a text file...;

e.g.
002
003
004
.
.
.
408
409
etc

...create an IP address based on this number, make a connection to the IP address then copy a folder from this location to a network location;

REM  *******************************************************************
REM  Get User number from Text File and remove leading zero's if present
REM  *******************************************************************

FOR /F "tokens=* delims=0" %%A IN (C:\Users.txt) DO SET host=%%A

pause

REM  **************************************
REM  Calculate IP Address from User number
REM  **************************************

If %host% gtr 250 goto :continue
set userip=18.100.%host%.100

goto :IPAttained
:continue

If %host% gtr 500 goto :continue2
set /a host=%host% - 250
set userip=18.101.%host%.100

goto :IPAttained
:continue2

set /a host=%host% - 500
set userip=18.102.%host%.100

:IPAttained

pause

net use N: \\%userip%\d$ /user:%%Acom\master password
xcopy "\\%userip%\d$\Time Sheet" "\\NetworkLocation\Users\%%Acom\Time Sheet" /E /C /R /I /K /Y
net use N: /delete

)

So far it appears to work...however it runs through the entire list and then works out the IP address on the last line in the text file!!

I'd like it to run for every line of course but I'm unsure what I'm missing here. Hope that makes sense.

Upvotes: 0

Views: 86

Answers (3)

MC ND
MC ND

Reputation: 70923

The main problem with your code is that all the address logic is out of the for do clause, so, the input file is readed, assigning the value of each of the lines to the variable, and when the last line is readed, for command finishes its work and the batch file continues.

An alternative is to move all the address management to a subroutine that gets called for each value readed from the input file, passing the readed value as a parameter

FOR /F "delims=" %%A IN (C:\Users.txt) DO call :process %%A
pause
exit /b 

:process 
rem read passed parameter
set "host=%1"

If %host% gtr 250 goto :continue
set /a host=1%host%-1000
set userip=18.100.%host%.100

goto :IPAttained
:continue

If %host% gtr 500 goto :continue2
set /a host=%host% - 250
set userip=18.101.%host%.100

goto :IPAttained
:continue2

set /a host=%host% - 500
set userip=18.102.%host%.100

:IPAttained

pause

net use N: \\%userip%\d$ /user:%1com\master password
xcopy "\\%userip%\d$\Time Sheet" "\\NetworkLocation\Users\%1com\Time Sheet" /E /C /R /I /K /Y
net use N: /delete

goto :eof

Or you can put your code INSIDE the do clause (parenthesis enclosed), but have to reformat how this work, as any goto command executed inside a for block automatically finishes the processing of the for. It is more clear and efficient, but "requires" delayed expansion for it to work

@echo off

    setlocal enableextensions enabledelayedexpansion

    for /f "delims=" %%a IN (C:\Users.txt) DO (
        if %%a gtr 500 (
            set /a "host=%%a-500"
            set "userip=18.102.!host!.100"
        ) else if %%a gtr 250 (
            set /a "host=%%a-250"
            set "userip=18.101.!host!.100"
        ) else (
            set /a "host=1%%a-1000"
            set "userip=18.100.!host!.100"
        )
        net use N: \\!userip!\d$ /user:%%acom\master password
        xcopy "\\!userip!\d$\Time Sheet" "\\NetworkLocation\Users\%%acom\Time Sheet" /E /C /R /I /K /Y
        net use N: /delete
    )
    endlocal

    pause

Upvotes: 1

Magoo
Magoo

Reputation: 79957

@ECHO OFF
SETLOCAL
FOR /f %%a IN (q24533225.txt) DO (
 CALL :copyts %%a
)

GOTO :EOF

:copyts
SET host=%1
:copytsl
IF DEFINED host IF %host:~0,1%==0 SET host=%host:~1%&GOTO copytsl
SET /a nic=100
:SEThostl
IF %host% gtr 250 set /a nic+=1&SET /a host-=250&GOTO sethostl
SET "userip=18.%nic%.%host%.100"
ECHO(net use N: \\%userip%\d$ /user:%1com\master password
ECHO(xcopy "\\%userip%\d$\Time Sheet" "\\NetworkLocation\Users\%1com\Time Sheet" /E /C /R /I /K /Y
ECHO(net use N: /delete


GOTO :eof

It would appear that you want 18.100.1-250.100, 18.101.1-250.100, 18.102.1-250.100 etc.

The above routine will simply ECHO the required commands. After testing and verification, remove the three ECHO( to activate the commands.

I used a file named q24533225.txt containing your data for my testing. The filename is unimportant, of course - but be very careful testing any solution against 008, 009 and 249..251 where the calculations are taking place. You don't unfortunately specify what the results in these cases should be and we're forced to derive the formula from your code and make assumptions.

Upvotes: 1

cure
cure

Reputation: 2688

your problem is that your for loop spins through the file, and does nothing but set a variable, so it resets it every iteration, effectively storing the value on the last line by the end of the loop. i cant test this, but here is some example code of one way you might fix the problem.

set line=0
set ohost=000
:top

REM  *******************************************************************
REM  Get User number from Text File and remove leading zero's if present
REM  *******************************************************************

FOR /F "skip=%line% tokens=* delims=0" %%A IN (C:\Users.txt) DO @( SET host=%%A&goto break)
:break
set /a line=line+1
if %host% EQU %ohost% (
    goto :EOF
) else (
    set ohost=%host%
)



REM  **************************************
REM  Calculate IP Address from User number
REM  **************************************

If %host% gtr 250 goto :continue
set userip=18.100.%host%.100

goto :IPAttained
:continue

If %host% gtr 500 goto :continue2
set /a host=%host% - 250
set userip=18.101.%host%.100

goto :IPAttained
:continue2

set /a host=%host% - 500
set userip=18.102.%host%.100

:IPAttained

pause

net use N: \\%userip%\d$ /user:%%Acom\master password
xcopy "\\%userip%\d$\Time Sheet" "\\NetworkLocation\Users\%%Acom\Time Sheet" /E /C /R /I /K /Y
net use N: /delete

)
goto top

Upvotes: 1

Related Questions