Avatar of SQLM_M
SQLM_M
Flag for India asked on

SPLITTING CDR FILES

I want to split a cdr files into multiple files based on conditions using powershell.

Please can anyone provide me the script...
Powershell

Avatar of undefined
Last Comment
SQLM_M

8/22/2022 - Mon
Qlemo

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.
the_endjinn

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.
SQLM_M

ASKER
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....
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
the_endjinn

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

ASKER
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....
SOLUTION
the_endjinn

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Qlemo

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

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
SQLM_M

ASKER
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

Could you explain that by example?
SQLM_M

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


I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
the_endjinn

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.
ASKER CERTIFIED SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
SQLM_M

ASKER
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..
SQLM_M

ASKER
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
      
  }
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.