3

I have a million old text files that I need to convert the format on. I have been desperately trying to do this myself but I really could use help. I am trying to convert data that looks like this:

text
11111.111
22222.222
33333.333
text2
44444.444
55555.555
66666.666

77777.777
88888.888
99999.999
(each number is on a seperate line and there are some blank lines, but I need them to go into the output file as a place keeper)

Into a .txt file that looks like this:
**I also need to add an increment number at the beginning of each line to number the lines.

1,11111.111,22222.222,33333.333,text
2,44444.444,55555.555,66666.666,text2
3,77777.777,88888.888,99999.999,

the files that I have are in separate folders in a directory and have no file extension but they behave exactly like a standard text file.

I have tried all sorts of stuff but I am just not that well versed in programming. Here is the little bit of code that I havent deleted for the 100th time. gettting frustrated

:REFORMAT
FOR %%F IN (*) DO IF NOT %%~XF==.BAT (
SETLOCAL DISABLEDELAYEDEXPANSION
(
SET /P LINE1=
SET /P LINE2=
SET /P LINE3=
SET /P LINE4=
)<"%%F"
ECHO %LINE2%,%LINE3%,%LINE4%,%LINE1%>>"%%F".TXT
PAUSE >NUL
:END

I am using windows I have access to dos6 dos7 winxp 32 and win7 64

the text and text2 are text strings within the file, they are descriptors telling me what the numbers below mean and some of the descriptors are left out. I also need it to process more than the first four lines. Some files have up to 200 lines inside of them. Thank you so much for helping me. thank you so much dbenham here is the final result:

@echo off
setlocal enableDelayedExpansion
for /R %%F in (*.) do (
set /a ln=0
for /f %%N in ('type "%%F"^|find /c /v ""') do set /a cnt=%%N/4
for /l %%N in (1 1 !cnt!) do (
for %%A in (1 2 3 4) do (
  set "ln%%A="
  set /p "ln%%A="
)
set /a ln+=1
echo !ln!,!ln2!,!ln3!,!ln4!,!ln1!
)
) <"%%F" >"%%F.CSV"
Cam690
  • 31
  • 2

2 Answers2

1

Using nothing but pure native batch:

@echo off
setlocal enableDelayedExpansion
for %%F in (*.) do (
  set /a ln=0
  for /f %%N in ('type "%%F"^|find /c /v ""') do set /a cnt=%%N/4
  for /l %%N in (1 1 !cnt!) do (
    for %%A in (1 2 3 4) do (
      set "ln%%A="
      set /p "ln%%A="
    )
    set /a ln+=1
    echo !ln!,!ln2!,!ln3!,!ln4!,!ln1!
  )
) <"%%F" >"%%F.txt"

The above will process all files that have no extension in the current folder. If you want to recursively include all sub-folders, then add the /R option to the outer FOR statement.

The whole thing can be done quite simply using my REPL.BAT utility:

@echo off
for %%F in (*.) do (
  <"%%F" repl "([^\r\n]*)\r?\n([^\r\n]*)\r?\n([^\r\n]*)\r?\n([^\r\n]*)\r?\n?" "$2,$3,$4,$1\r\n" mx|findstr /n "^"|repl "^(.*):" "$1," >"%%F.txt"
)
Community
  • 1
  • 1
dbenham
  • 127,446
  • 28
  • 251
  • 390
  • I OWE YOU A BEER GOOD SIR!!! WOW, ok I'll calm down, thank you very much this had me so frustrated, I hadn't thought to put the inside for lookp and set another variable for it, your code worked flawlessly. I added the /r option and changed the .txt to .csv so that this will work with my other software without further conversion. seriously, thank you so much. Here is the final working Code that will save me about a year of my life stuck in the office. I will post the revised code above in case anyone else ever needs the same thing. – Cam690 Jun 23 '14 at 18:01
  • Why not accept this answer, then this question will go from the pool of open and unanswered questions! – Brian Tompsett - 汤莱恩 Dec 27 '14 at 23:30
0

just a few modifications:

set /p doesnt delete a variable if input is empty, so you have to delete it before.

You missed a closing paranthese )

You have to use delayed expansion to use a changed variable inside parantheses

the counter.

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set count=0
FOR %%F IN (*.) DO IF NOT %%~XF==.BAT (
   set "LINE1="
   set "LINE2="
   set "LINE3="
   set "LINE4="
   set count+=1
  (
    SET /P LINE1=
    SET /P LINE2=
    SET /P LINE3=
    SET /P LINE4=
  )<"%%F"

  ECHO !count!,!LINE2!,!LINE3!,!LINE4!,!LINE1!>>"%%F.txt"
)
PAUSE >NUL
:END
Stephan
  • 53,940
  • 10
  • 58
  • 91
  • I believe the OP wants to process all lines in each file, not just the first 4. – dbenham Jun 21 '14 at 05:21
  • @dbenham: I'm not sure, what he wants. "a million files to convert the format on" means, he wants to have a million outputfiles. But the "need of an incremental number" means, he wants one single outputfile. Maybe both of us are wrong. I'm also not sure how to read his example: is this one file or are these three files with 4 lines each. – Stephan Jun 21 '14 at 05:44
  • Yes, I also struggled with interpretation of the question. I wasn't sure if "text" and "text2" were lines within one file, or file names. But the OP explicitly mentions blank lines as being important, and I don't see how the output is possible unless "text", "text2", and the blank line all represent lines in one file. – dbenham Jun 21 '14 at 06:27
  • @dbenham: It might be, that some files have an empty first line. Let's wait for the OP to answer our questions ^^ – Stephan Jun 21 '14 at 06:29
  • the text is a text string within the file, it is a descriptor telling me what the numbers below mean, some of the descriptors are left out. I also need it to process more than the first four lines. Some files have up to 200 lines inside of them. Thank you so much for helping me. – Cam690 Jun 23 '14 at 13:03