Reading From A Text File - Batch

后端 未结 1 661
余生分开走
余生分开走 2021-02-08 12:41

I have a text file, a.txt:

Hello World
Good Afternoon

I have written a batch script to read contents of this file line by l

相关标签:
1条回答
  • 2021-02-08 13:28

    Your code "for /f "tokens=* delims=" %%x in (a.txt) do echo %%x" will work on most Windows Operating Systems unless you have modified commands.

    So you could instead "cd" into the directory to read from before executing the "for /f" command to follow out the string. For instance if the file "a.txt" is located at C:\documents and settings\%USERNAME%\desktop\a.txt then you'd use the following.

    cd "C:\documents and settings\%USERNAME%\desktop"
    for /f "tokens=* delims=" %%x in (a.txt) do echo %%x
    echo.
    echo.
    echo.
    pause >nul
    exit
    

    But since this doesn't work on your computer for x reason there is an easier and more efficient way of doing this. Using the "type" command.

    @echo off
    color a
    cls
    cd "C:\documents and settings\%USERNAME%\desktop"
    type a.txt
    echo.
    echo.
    pause >nul
    exit
    

    Or if you'd like them to select the file from which to write in the batch you could do the following.

    @echo off
    :A
    color a
    cls
    echo Choose the file that you want to read.
    echo.
    echo.
    tree
    echo.
    echo.
    echo.
    set file=
    set /p file=File:
    cls
    echo Reading from %file%
    echo.
    type %file%
    echo.
    echo.
    echo.
    set re=
    set /p re=Y/N?:
    if %re%==Y goto :A
    if %re%==y goto :A
    exit
    
    0 讨论(0)
提交回复
热议问题