Read a file from DOS

Ms / E6550
December 15, 2008 at 03:53:35
Specs: DOS, Intel DUO
Ex:1
@ echo off
set TestPath="C:\Test Dir\Testing\tmp.txt"
for /f "eol= tokens=* delims= " %%i in
(%TestPath%) do (
echo %%i
)

(or)

Ex:2
@ echo off
set TestPath=C:\Test Dir\Testing
for /f "eol= tokens=* delims= " %%i in
("%TestPath%\tmp.txt") do (
echo %%i
)

Here as found in the above examples. I am
trying to access the file content. But its
not responding as expected.

Here I am trying to access the file contents.
But the for loop is not supporting the path
names if there is a space in any directory
name.

The same code(for loop) worked fine when i
not use any space(Test_Dir in path) in the
path name.as in below Example:
Ex:3
@ echo off
set TestPath=C:\Test_Dir\Testing
for /f "eol= tokens=* delims= " %%i in
("%TestPath%\tmp.txt") do (
echo %%i
)

Please provide your suggestions in this.

I need to pass a path name to the for loop to
access the file content. The path will have
space (since the directory name has some
space)

How can we solve this issue. ?
Please help me.


See More: Read a file from DOS

Report •


#1
December 15, 2008 at 05:00:07
try:
set TestPath="C:\TestDi~1\Testing\tmp.txt"

Report •

#2
December 15, 2008 at 05:22:27
As far as I'm aware DOS can't read files without third party tools.

Do you mean scripting with windows batch script, for systems derived from windows nt? If the particular version of windows is on this list it is possible to read plain text files using batch. Although if the file to be read contains binary that isn't plain text it may not be possible to read the file with batch.

If your are using a nt based system simply add "usebackq" to the options of your for /f loop, "usebackq eol= tokens=* delims= ". The usebackq options changes the meaning of the double quotes from "literal string" to "file name". See "for /?" for more infomation.


Report •

#3
December 15, 2008 at 05:39:46
As leeabc123 points out 8dot3 short file aliases can also be used, but they will be useless in this situation surrounded by quotes. A alias will work fine with out any quotes.

There are caveats with using short file name aliases. Firstly people occasionally turn short file name aliases off. These aliases will almost certainly be phased out in future realises of windows(and possibly already in 64bit systems???) as they are maintained mainly for DOS compatibility reasons; DOS compatibility is being killed off in 64bit systems and of course 64bit systems will likely take over completely some time soon.


Report •

Related Solutions

#4
December 15, 2008 at 07:20:36
Hi Actually the problem i face is with the
for loop.

I can get the result even if i use:
set TestPath=C:\Test_Dir\Testing
instead
set TestPath=C:\Test_Di~1\Testing(as u
specified)

My problem is when there is a space in
between the directory names: as below(Test
Dir) it is not responding the file contents:
set TestPath=C:\Test Dir\Testing

Because it works fine when i use:
@ echo off
set TestPath=C:\Test_Dir\Testing
for /f "eol= tokens=* delims= " %%i in
("%TestPath%\tmp.txt") do (
echo %%i
)

But it not works when :
@ echo off
set TestPath=C:\Test Dir\Testing
for /f "eol= tokens=* delims= " %%i in
("%TestPath%\tmp.txt") do (
echo %%i
)

Can u please help me out here by making my
code to be executable one.


Report •

#5
December 15, 2008 at 14:23:57
Use the "usebackq" option and surround the file name in double quotes like I mentioned above.


@ echo off
for /f "usebackq eol= tokens=* delims= " %%i in ("C:\Test Dir\Testing\tmp.txt") do (
echo %%i
)


Report •

#6
December 15, 2008 at 15:24:36
It's TestDir~1 not Test_Dir~1.

Report •

#7
January 11, 2009 at 23:37:46
Hi,

I need a suggestion form you.

Is the below mentioned batch script failed because of the usebackq command.

Batch Files Command:
E:\>for /F "eol=; tokens=1 delims=" %%i in (C:\Num.txt) do (set /a no=%%i+1 )

CLI Output:
E:\>for /F "eol=; tokens=1 delims=" %%i in (C:\Num.txt) do (set /a no=%%i+1 )
E:\>(set /a no=ECHO is on.+1 )
Missing operator.

I also want to know how "ECHO is on." is been replaced for %%i.

Please help me on this issue.


Report •


Ask Question