Split File Into Multiple Files Using Batch

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
kokko
Posts: 1
Joined: 10 Jan 2015 17:23

Split File Into Multiple Files Using Batch

#1 Post by kokko » 10 Jan 2015 17:29

I need split one large file, which contains chemical structures of molecules into separated files for each molecule one.
Important is, that every subfile is separated by $$$$
So the data look like: file test.sdf

line1
line2
line3
line4
$$$$
line6
line7
line8
line9
line10
$$$$
etc.

and I need have separated files
file1.mol

line1
line2
line3
line4

file2.mol

line6
line7
line8
line9
line10
etc.

I tried to write a script but it looks like it does not work. :( I will really appreciate if someone could help me. I am not comfortable with batch and rather use bash, but I need to learn something new...

here is my code:

Code: Select all

@ECHO OFF
SET "destdir=C:\Users\miru\Desktop"
SET "extensions=mol"
SET "output="
FOR /f "delims=" %%a IN (test.sdf) DO (
IF "%%a"=="$$$$" (SET "output=Y"&SET "ext="
) ELSE (
IF DEFINED output (
IF NOT DEFINED ext FOR %%s IN (%extensions%) DO IF /i "%%a"=="%%s" SET "ext=%%s"
)
)
GOTO :EOF



thank you for response :wink:

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Split File Into Multiple Files Using Batch

#2 Post by Squashman » 10 Jan 2015 18:01

Code: Select all

@ECHO OFF &setlocal enabledelayedexpansion
SET "destdir=C:\Users\miru\Desktop"
set count=1
FOR /f "delims=" %%G IN (foo.txt) DO (
   IF "%%G"=="$$$$" (
      SET /a count+=1
   ) ELSE (
      >>"%destdir%\file!count!.mol" echo %%~G
   )
)

output

Code: Select all

C:\Batch\split>dir /b *.mol
file1.mol
file2.mol
file3.mol

C:\Batch\split>type file*.mol

file1.mol


line1
line2
line3
line4

file2.mol


line6
line7
line8
line9
line10

file3.mol


etc.

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Split File Into Multiple Files Using Batch

#3 Post by Aacini » 10 Jan 2015 18:55

Already posted (and answered) at SO: http://stackoverflow.com/questions/2788 ... dows-batch

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Split File Into Multiple Files Using Batch

#4 Post by foxidrive » 11 Jan 2015 07:31

Aacini wrote:Already posted (and answered) at SO: http://stackoverflow.com/questions/2788 ... dows-batch


I don't think the person will be back - he deleted the post and your answer Antonio, and without saying thank you.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Split File Into Multiple Files Using Batch

#5 Post by dbenham » 11 Jan 2015 22:34

Ahh - memories. I used to live and breath chemical structure connection tables in another life. I was the principal developer of the first commercial application that stored 2D structures in Oracle and allowed substructure searches combined with relational data searches. Cool stuff 8)

An SDFILE can have arbitrary data associated with each MOLFILE that appears after the M END line, and before the $$$$ line. The actual MOLFILE ends at the M END line.

MOLFILEs that include that additional data do not follow the actual specification for a MOLFILE, so your output files may not work with your software.


Dave Benham

Post Reply