Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

create filenames and add text using a batch file

gheimsteadgheimstead Posts: 5Member
Well, I thought I was better at this than I am and I've got a problem I need some help with. I thought this would be simple, but at least right now, apparently I'm brain dead...

Here's the problem... I have a file called import.txt, the file layout looks like this...

4323
Name of Company
Light
no
no
no
no
no
no

4325
Name of next company
#N/A
no
no
no
no
no
no

Each record is exactly the same size, seperated by a space. Here's what I would like the batch file to do...

read import.txt
Create a file from the first line of each record, i.e., 4323.txt
insert the next eight lines exactly as shown
move the the next record and do it all again until all the records are seperate files.

Thanks in advance for any help, I really appreciate it!
Gary

Comments

  • gheimsteadgheimstead Posts: 5Member
    I wanted to share the answer with anyone who might have been interested in this problem. It came from a different forum I'm a member of, from a guy named aGerman. Here's his reply...

    -------------------------

    It's not that easy. Maybe it will fail if the number of lines is greater than the 32bit limit for numeric expressions in batch.


    Code:
    @echo off &setlocal
    set "empty=0"
    for /f "delims=: tokens=1*" %%a in ('findstr /n "^" "import.txt"') do (
    if "%%b"=="" (
    call set "empty=%%empty%% %%a"
    ) else (
    set "line_%%a=%%b"
    )
    )

    for %%a in (%empty%) do (
    set /a nameLine=%%a+1
    set /a startLine=%%a+2
    set /a endLine=%%a+9
    call :proc
    )
    goto :eof

    :proc
    call set "name=%%line_%nameLine%%%.txt
    if "%name%"==".txt" goto :eof
    >"%name%" type nul
    for /l %%a in (%startLine%,1,%endLine%) do (
    >>"%name%" call echo.%%line_%%a%%
    )
    goto :eof




    This code should work if the separator is an empty line.
    But you wrote the separator is a space. If this is realy true you have to replace:

    Code:
    if "%%b"==" " (

    -----------------------------


  • gheimsteadgheimstead Posts: 5Member
    This post has been deleted.
Sign In or Register to comment.