- The same file can be opened for input and output independently.
- A given file can only be opened once for output due to an exclusive lock
- The same file can be opened multiple times for input. Each input file stream maintains its own file pointer independent of the others.
- Redirection to an existing file handle via something like &1 shares the stream with only 1 file pointer between them.
- FIND resets the file pointer to the beginning of file before scanning.
- FINDSTR does not reset the file pointer (I first saw this in an Aacini post)
- Writing to an input handle fails with a puzzling error message
- Reading from an output handle fails simply returns nothing (fails?), but there is no error message.
Code: Select all
@echo off
0<test.txt 7>test.txt 8<&0 9<test.txt (
call :read 0
call :write 1
call :write 2
call :write 3
call :read 0
call :read 8
call :read 9
call :write 4
call :read 8
call :read 8
call :read 0
echo(
echo FINDSTR from 9 does not reset file pointer to beginning, and ends at EOF:
0<&9 findstr "^"
echo(
call :read 9
0<test.txt (
echo(
echo Temporarily reopen input file via 0
call :read 0
call :read 0
echo Close and resume old 0
echo(
)
call :write 5
call :read 0
call :read 9
call :read 0
call :read 8
call :read 9
echo(
echo FINDSTR from 0 does not start at beginning:
findstr "^"
echo(
echo FIND from 0 resets file pointer to beginning, and ends at EOF:
find /v ""
echo(
call :read 0
echo(
echo Cannot reopen output file because of exclusive lock
1>test.txt (
1>&2 echo This is never executed
)
echo(
echo Cannot read from non-existent file
0<notExists (
set /p "ln=This is never executed"
)
echo(
1>&0 (
1>&2 echo Cannot write to input handle, but redirection worked and error message puzzling
echo This fails when try to write to input handle
)
echo(
0<&1 set /p "ln=Why does read from output handle not generate an error message?"
echo(
)
exit /b
:read handle
setlocal
if "%1" neq "0" set "src=0<&%1"
set "ln="
set /p "ln=" %src%
echo Reading from %1 ==^> %ln%
exit /b
:write txt
1>&7 echo %*
echo Wrote %*
exit /b
--OUTPUT--
Code: Select all
Reading from 0 ==>
Wrote 1
Wrote 2
Wrote 3
Reading from 0 ==> 1
Reading from 8 ==> 2
Reading from 9 ==> 1
Wrote 4
Reading from 8 ==> 3
Reading from 8 ==> 4
Reading from 0 ==>
FINDSTR from 9 does not reset file pointer to beginning, and ends at EOF:
2
3
4
Reading from 9 ==>
Temporarily reopen input file via 0
Reading from 0 ==> 1
Reading from 0 ==> 2
Close and resume old 0
Wrote 5
Reading from 0 ==> 5
Reading from 9 ==> 5
Reading from 0 ==>
Reading from 8 ==>
Reading from 9 ==>
FINDSTR from 0 does not start at beginning:
FIND from 0 resets file pointer to beginning, and ends at EOF:
1
2
3
4
5
Reading from 0 ==>
Cannot reopen output file because of exclusive lock
The process cannot access the file because it is being used by another process.
Cannot read from non-existent file
The system cannot find the file specified.
Cannot write to input handle, but redirection worked and error message puzzling
The system cannot find the file specified.
Why does read from output handle not generate an error message?
Note that I did not redirect handle 3 after redirecting 0 because of this issue: http://stackoverflow.com/a/9880156/1012053
Dave Benham