Solved Split file into multiple files using windows Batch

January 10, 2015 at 15:16:59
Specs: Windows 7
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:

@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


See More: Split file into multiple files using windows Batch

Report •


#1
January 10, 2015 at 18:15:33
✔ Best Answer
Here's prototype:
::======== begin batchscript
@echo off & setlocal enabledelayedexpansion
set c=0
for /f "tokens=*" %%a in (test.sdf) do (
if "%%a" equ "$$$$" (
set /a c+=1
:: this next is just to kill any lingering left-overs
>f!c!.mol echo.
) else (
>> f!c!.mol echo %%a
)
)
::===== end batchscript, limited testing

Report •

#2
January 10, 2015 at 18:26:24
thank you very much

Report •

#3
January 10, 2015 at 18:52:15
If you're used to Bash, you'll probably be happier with PowerShell.
[int]$fileNo = 0
Get-Content -Path .\test.sdf | 
Out-String |
ForEach-Object { $_ -split "`r`n\$\$\$\$`r`n" } |
ForEach-Object { $_ > "file$((++$fileNo)).mol" }

How To Ask Questions The Smart Way


Report •

Related Solutions


Ask Question