Read 1 line at a time

February 8, 2010 at 11:11:23
Specs: Windows XP
I'm writing a script that closes specified files.
So far, the script will run 'openfiles' and delim the ID numbers of a specified file (if there are more than 1 copy open) to a text file.

What I'm trying to do now is read each ID (1 ID per line) and run a command to close that ID.

My non-working code is:

echo ┌──Closing %file%─────────────────────────────────────────────────────────────┐
:loop
set /a o+=1
echo. Count: %COUNT%
echo. o: %o%
if %o% leq %COUNT% (
for /f "tokens=1 delims=" %%e IN ('type "H:\_FileID"') do (

echo %%e
goto :loop
)
)
goto :eof
echo └─────────────────────────────────────────────────────────────────────────────┘
goto :eof

I'm just trying to run echo instead of the actually disconnect command to see what ID is being used. Currently, it keeps using the first ID found in the file instead of going to the next line, which I haven't found a way to do.


See More: Read 1 line at a time

Report •


#1
February 8, 2010 at 11:15:20
for /f "delims=" %%e IN (H:\_FileID) do echo %%e


Report •

#2
February 8, 2010 at 11:52:13
Seems we are getting close but this is the adjusted code:

:loop
set /a o+=1
echo. Count: %COUNT%
echo. o: %o%
if %o% leq %COUNT% (
for /f "delims=" %%e IN (H:\_FileID) do echo %%e
goto :loop
)

and the output:

Count: 9
o: 1
156053
158685
159200
169463
169893
170016
170238
170546
170583
Count: 9
o: 2
156053
158685
159200
169463
169893
170016
170238
170546
170583
Count: 9
o: 3
156053
158685
159200
169463
169893
170016
170238
170546
170583
Count: 9
o: 4
156053
158685
[...and so on till the 'o' count is up to 10...]

So now it reads the whole file instead of one line at a time


Report •

#3
February 8, 2010 at 11:54:38
Any reason you want to iterate over the file 10 times? No? Then why are you wrapping it up into a second loop?

Report •

Related Solutions

#4
February 8, 2010 at 12:09:46
The command I need to run is this (in place of echo)

openfiles /s \\%computer% /disconnect %IDofFile%

I guess I need to load 1 line into a variable and swap it until all IDs have been run through the script.

I don't trust that 'openfiles /disconnect %%e' will work as it accepts 1 ID, not 9


Report •

#5
February 8, 2010 at 12:24:31
I need to load 1 line into a variable
%%e is a variable.

for /f "delims=" %%e IN (H:\_FileID) do openfiles /s \\%computer% /disconnect %%e

EDIT: Suggested reading: FOR /?

H:\>for /?
Runs a specified command for each file in a set of files.

FOR %variable IN (set) DO command [command-parameters]

 . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    more tokens.  The body of the for loop is then called with the
    variable value(s) set to the found token string(s).  By default, /F

 . . . . . . . . . . . . . . . . . . . . . . . . . . . . .



Report •

#6
February 8, 2010 at 12:31:43
i'm not sure exactly what id you're trying to generate, exactly, but it looks like all you need to do is like Razor said:

setlocal
for /f "delims=" %%e IN (H:\_FileID) do (
echo.%%e
echo openfiles /s \\%computer% /disconnect %%e
)

but maybe this %IDofFile% is meant to have a sequence added to it? (ie: %o%, so idoffile looks like 156053-1 or something?), i don't know. maybe post what you need the output var. to look like...


Report •

#7
February 8, 2010 at 12:44:28
Ok I think I was over complicating it. Thank you very much!

Report •


Ask Question