SPLITTING CDR FILES

SQLM_M
SQLM_M used Ask the Experts™
on
I want to split a cdr files into multiple files based on conditions using powershell.

Please can anyone provide me the script...
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
I'm sorry to tell you you can't do that. Interpreting Corel Draw files requires to know the inner secrets of its format, and there is no documentation of it (see http://en.wikipedia.org/wiki/CorelDRAW#CDR_file_format). It might be possible to use Corel Draw and some automation interface via PowerShell, but even it is known how to do it, it remains very complex.
If you are talking about Cisco CDR logs then they are just text files and therefore you can use PowerShell to carve them up but we would need more info as to what you want to achieve. If it's is Corel Draw then see above.

Author

Commented:
The file format is

AAA;0;0;1;;;;111;11;;;
BB;;;;0;;1;;001;;;;00;
CC;;;;;;000;0001;;;;00;;;;;

I want to split files of this format using Powershell....
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

How do you want them split? Line by line or only after certain text? Every 10th line etc?

Author

Commented:
The file format is

AAA;0;0;1;;;;111;11;;;
BB;;;;0;;1;;001;;;;00;
CC;;;;;;000;0001;;;;00;;;;;

I want to move record lines starting AAA to separate files and BB to separate files and CC to separate files..

I want to split files of this format using Powershell....
Hi,

The below would work, you just need to put relevant paths for your environment.

$cdr = "c:\path\to\file.cdr"

$aaaoutput = "c:\path\to\aaa.cdr"
$bboutput = "c:\path\to\bb.cdr"
$ccoutput = "c:\path\to\cc.cdr"

get-content $cdr | ForEach-Object {
	switch -wildcard ($_)
	{
		"AAA*" { $_ | Out-File -Append -Encoding "Default" -FilePath $aaaoutput }
		"BB*" { $_ | Out-File -Append -Encoding "Default" -FilePath $bboutput }
		"CC*" { $_ | Out-File -Append -Encoding "Default" -FilePath $ccoutput }
	}
}

Open in new window

Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
Indeed, I would do it similar, but more failsafe, and with the minimum of changes in a condition:
$cdr = "c:\path\to\file.cdr"
$outfiles = @("other", "aaa", "bb", "cc")

get-content $cdr | ForEach-Object {
        $fileNo = 0
        switch -wildcard ($_)
        {
                "AAA*" { $fileNo = 1}
                "BB*"  { $fileNo = 2}
                "CC*"  { $fileNo = 3}
        }
       $_ | Out-File -Append -Encoding "Default" -FilePath "C:\path\to\"+$outfile[$fileNo]
}

Open in new window

Author

Commented:
Hi the_endjinn,

Thanks for your solution,

Please give me the solution to make the path and file dynamic..that is the file name of the source file should be captured and the respective wildcard should be assigned to it..
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
Could you explain that by example?

Author

Commented:
Hi,

I want to loop through all the files in a folder and i want to move the files to respective folder with filename.

If the source is CDAUS99AUSLM00729 and if the file contains

AAA;0;0;1;;;;111;11;;;
BB;;;;0;;1;;001;;;;00;
CC;;;;;;000;0001;;;;00;;;;;

I want the output files to move to respective folders like this

$aaaoutput = "c:\AAA\CDAUS99AUSLM00729.cdr"
$bboutput = "c:\BB\CDAUS99AUSLM00729.cdr"
$ccoutput = "c:\CC\CDAUS99AUSLM00729.cdr"


Hi, please see the below.

$aaafolder = "c:\aaa\"
$bbfolder = "c:\bb\"
$ccfolder = "c:\cc\"

$cdrfolder = "c:\path\to\files\"

Get-ChildItem $cdrfolder | ForEach-Object {
	get-content $_.FullName | ForEach-Object {
		switch -wildcard ($_)
		{
			"AAA*" { $_ | Out-File -Append -Encoding "Default" -FilePath "$aaafolder$_" }
			"BB*" { $_ | Out-File -Append -Encoding "Default" -FilePath "$bbfolder$_" }
			"CC*" { $_ | Out-File -Append -Encoding "Default" -FilePath "$ccfolder$_" }
		}
	}
}

Open in new window


You could also use the same approach with Qlemo's code above, which is better and cleaner although maybe not as easy to understand for someone new to PowerShell. You'd need him to alter it for you if you want to use it as I'm not passing it off as my own, but basically the output from the switch is being used to call a specific element of the $outfiles array.
"Batchelor", Developer and EE Topic Advisor
Top Expert 2015
Commented:
Like this?
$pattern = @("AAA", "BB", "CC")

Get-ChildItem "C:\SourcePath\*.cdr" |
  % { # $_ is file
    $name = $_.name
    get-content $_ | 
    % { # $_ is single line
      foreach ($p in $pattern)
      { if ($_.StartsWith($p))
        { Out-File -Append -FilePath "C:\TargetPath\$p\$name" -InputObject $_ }
      }
    }
  }

Open in new window

You'll need to adapt $pattern, and the paths "C:\SourcePath" and "C:\TargetPath" to your needs.

Author

Commented:
Hi Qlemo,

Thanks for your solution. It works 100%, i need one more solution, i am new to powershell, so  please help me..

I want to move the processed files to Archive folder..and is there any alerts available..

Author

Commented:
Hi,

I tried to make code dynamic, but couldn't able to move the files. Please help me the solve this..

$pattern = @("Mobile Originated Call", "Mobile Terminated Call", "Network Info")

$SourcePath = "D:\tap3"
$FileType = "*.cdr"
$DestPath = "D:\tap3\Archive\"

Get-ChildItem "$SourcePath\$FileType" |
  % { # $_ is file
    $name = $_.name
    get-content $_ |
    % { # $_ is single line
      foreach ($p in $pattern)
      { if ($_.StartsWith($p))
        { Out-File -Append -FilePath "$SourcePath\$p\$name" -InputObject $_  }
      }
    }
Move-Item "$SourcePath\$p\$name" "$DestPath\$name" -Force
      
  }

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial