Cam690
Cam690

Reputation: 31

Need help writing a batch file to process multiple files into comma delimited text files

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"

Upvotes: 3

Views: 145

Answers (2)

dbenham
dbenham

Reputation: 130829

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"
)

Upvotes: 1

Stephan
Stephan

Reputation: 56180

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

Upvotes: 0

Related Questions