Computing.Net > Forums > Programming > Read a file from DOS

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Read a file from DOS

Reply to Message Icon

Name: trusp
Date: December 15, 2008 at 03:53:35 Pacific
OS: DOS
CPU/Ram: Intel DUO
Product: Ms / E6550
Comment:

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.



Sponsored Link
Ads by Google

Response Number 1
Name: lee123abc
Date: December 15, 2008 at 05:00:07 Pacific
Reply:

try:
set TestPath="C:\TestDi~1\Testing\tmp.txt"


0

Response Number 2
Name: Judago
Date: December 15, 2008 at 05:22:27 Pacific
Reply:

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.


0

Response Number 3
Name: Judago
Date: December 15, 2008 at 05:39:46 Pacific
Reply:

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.


0

Response Number 4
Name: trusp
Date: December 15, 2008 at 07:20:36 Pacific
Reply:

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.


0

Response Number 5
Name: Judago
Date: December 15, 2008 at 14:23:57 Pacific
Reply:

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
)


0

Related Posts

See More



Response Number 6
Name: dave19
Date: December 15, 2008 at 15:24:36 Pacific
Reply:

It's TestDir~1 not Test_Dir~1.


0

Response Number 7
Name: trusp
Date: January 11, 2009 at 23:37:46 Pacific
Reply:

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.


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Read a file from DOS

Win32: Reading a texts from a file www.computing.net/answers/programming/win32-reading-a-texts-from-a-file/11673.html

read file from subroutine in c++ www.computing.net/answers/programming/read-file-from-subroutine-in-c/3714.html

Shell script to copy a file from local drive www.computing.net/answers/programming/shell-script-to-copy-a-file-from-local-drive-/18791.html