[Cmd Script] TextToFile

Discussion in 'Scripting' started by Dark Dinosaur, Aug 3, 2021.

  1. Dark Dinosaur

    Dark Dinosaur X Æ A-12

    Feb 2, 2011
    3,754
    5,216
    120
    #1 Dark Dinosaur, Aug 3, 2021
    Last edited: Aug 20, 2021
    if you have for loop that use some text file \ else
    i made a stupid thing that convert your text file into class
    so no more reading from file ....
    it wont work with special chars. only for basic text like.
    list of id & numbers etc etc ...
    if your file name it

    v7 ~ better better way

    Code:
    @cls
    @echo off
    setlocal enabledelayedexpansion
    echo %1 | >nul find /i """" && (
        if not exist %1 goto :eof
        1>nul 2>nul type %1 || goto :eof
        set input=%1
        goto :_next
    )
    if not exist "%1" goto :eof
    type "%1" >nul 2>&1 || goto :eof
    set input="%1"
    goto :_next
    :_next
    set ignore=true
    set output="%~dpn1.cmd"
    set class=%~n1
    set class=%class: =_%
    set tempFile=%~nx1
    for /f "tokens=*" %%g in ('type !input!') do (
        if defined ignore (
            >%output%  echo @cls
            >>%output%  echo @echo off
            >>%output%  echo ^>"%%temp%%\!tempFile!" call :!class!
            >>%output%  echo goto :eof
            >>%output%  echo.
            >>%output%  echo :!class!
            if /i '%%g' EQU '' (>>%output% echo.)
            if /i '%%g' NEQ '' (>>%output% echo echo %%g)
        )
     
        if not defined ignore (
            if /i '%%g' EQU '' (>>%output% echo.)
            if /i '%%g' NEQ '' (>>%output% echo echo %%g)
        )
     
        set ignore=
    )
    >>%output% echo goto :eof
    goto :eof
    
    will produce code like this
    Code:
    @cls
    @echo off
    >"%temp%\A B C.txt" call :A_B_C
    goto :eof
    
    :A_B_C
    echo a
    echo b
    echo c
    echo d
    echo e
    echo f
    echo g
    goto :eof
    
    
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  2. AveYo

    AveYo MDL Expert

    Feb 10, 2009
    1,836
    5,693
    60
    would this help?
    Code:
    @echo off
    title AveYo's :export text attachments snippet
    
    :: export bundled README (just display it on screen)
    call :export readme
    
    :: export bundled README as file
    call :export readme > "README.txt"
    
    ::done
    pause
    exit/b
    
    :export usage: call :export NAME
    setlocal enabledelayedexpansion || Prints all text between lines starting with :NAME:[ and :NAME:] - A pure batch snippet by AveYo
    set [=&for /f "delims=:" %%s in ('findstr/nbrc:":%~1:\[" /c:":%~1:\]" "%~f0"') do if defined [ (set/a ]=%%s-3) else set/a [=%%s-1
    <"%~fs0" ((for /l %%i in (0 1 %[%) do set /p =)&for /l %%i in (%[% 1 %]%) do (set txt=&set /p txt=&echo(!txt!)) &endlocal &exit/b
    
    :readme:[
    
           Text file attachments in the bat file itself by AveYo
       using just a |3| lines portable pure batch snippet - all you need to do is
       surround your text with the special named markers :name:[ and :name:]
       and remember to not use the same name for multiple attachments - that's it!
       whenever you need to print attachment named myfile you use command call :export myfile
       & can redirect it to a file by instead using command call :export myfile > "pathto\myfile.ext"
    
    :readme:]
    
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. Dark Dinosaur

    Dark Dinosaur X Æ A-12

    Feb 2, 2011
    3,754
    5,216
    120
    what is this 0<"%~fs0" ( ?
    and where is the end of it, every ( must end with ) ?
    and how it possible to show special char without error ?
    please explain to me .. first time for everything


    Code:
    0<"%~fs0" (
    
    for /l %%i in (0 1 %[%) do set /p =
    for /l %%i in (%[% 1 %]%) do (
        set txt=
        set /p txt=
        echo(!txt!)
    )
    Code:
    @cls
    @echo off
    setlocal enabledelayedexpansion
    title AveYo's :export text attachments snippet
    
    :: export bundled README (just display it on screen)
    call :export readme
    
    :: export bundled README as file
    REM call :export readme > "README.txt"
    
    ::done
    exit/b
    
    :export
    set [=
    for /f "delims=:" %%s in ('findstr /nbrc:":%~1:\[" /c:":%~1:\]" "%~f0"') do (
        if defined [        set /a ]=%%s-3
        if not defined [     set /a [=%%s-1
    )
    
    0<"%~fs0" (
    
    for /l %%i in (0 1 %[%) do set /p =
    for /l %%i in (%[% 1 %]%) do (
        set txt=
        set /p txt=
        echo(!txt!)
    )
    
    goto :eof
    
    :readme:[
    
           Text file attachments in the bat file itself by AveYo
       using just a |3| lines portable pure batch snippet - all you need to do is
       surround your text with the special named markers :name:[ and :name:]
       and remember to not use the same name for multiple attachments - that's it!
       whenever you need to print attachment named myfile you use command call :export myfile
       & can redirect it to a file by instead using command call :export myfile > "pathto\myfile.ext"
    
    :readme:]
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  4. AveYo

    AveYo MDL Expert

    Feb 10, 2009
    1,836
    5,693
    60
    understandable as it's ultra-compact ;)
    basically using findstr to get the line numbers for the [ begin txt and end txt ] markers
    then self-read the whole script <"%~fs0" into a (context) from where set/p + delayed expansion can read lines one by one
    where (context) has one loop of empty set/p command [ times to skip whatever is before begin txt marker
    and one loop that grabs set/p lines between the [ begin txt and end txt ] markers via loop-only txt variable
    txt variable must first be cleared, and then passed outside the loop via echo
    set/p is great with special characters but has some issues with lines starting with = and a few more, alleviated by this double-read ping-pong method
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  5. Dark Dinosaur

    Dark Dinosaur X Æ A-12

    Feb 2, 2011
    3,754
    5,216
    120
    this one is too much for my brain Sorry :(
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...