Reputation: 1017
I have a folder with 1 or more files like this:
bigfileA.txt
bigfileB.txt
bigfileC.txt
Each file contains 1 line and is delimited by '
For example, bigfileA.txt
may contain abcdef'ghijklmnop'qrst
How can I use powershell so that when I run the .ps file it will automatically split all files in the specified directory into multiple files? The output will look like this:
bigfileA1.txt --> abcdef
bigfileA2.txt --> ghijklmnop
bigfileA3.txt --> qrst
bigfileB1.txt --> uvw
bigfileB2.txt --> xyz
bigfileC1.txt --> ... and so on...
... and so on...
Can someone help? Thank you in advance!
Upvotes: 0
Views: 991
Reputation: 2149
The below script will go through a directory you choose and create a text file with the name of the base file + the current index (For example bigfileA
+ 1
) and will have the delimited value inside.
$bigfiles = Get-ChildItem -Path 'C:\path\to\bigfiles\here'
ForEach ($path in $bigfiles)
{
$contents = Get-Content $path.FullName
ForEach ($line in $contents)
{
$splitItems = $line.split("'")
For ($i = 0; $i -lt $splitItems.count; $i++)
{
New-Item -Path "$($path.Directory)\$($path.BaseName)$i.txt" -ItemType File -Value $splitItems[$i]
}
}
}
EDIT: Re-read question, must have misinterpreted the first time. Updated code.
If you want the files to start at 1 instead of 0 change this line
New-Item -Path "$($path.Directory)\$($path.BaseName)$i.txt" -ItemType File -Value $splitItems[$i]
to this
New-Item -Path "$($path.Directory)\$($path.BaseName)$($i+1).txt" -ItemType File -Value $splitItems[$i]
We're simply changing the $i
to $($i+1)
Functional Edit
Has not been tested and was put together quickly because i'm at work but it's at least a start :)
function Split-BigFiles
{
[CmdletBinding(DefaultParameterSetName='All',
SupportsShouldProcess=$false,
PositionalBinding=$false)]
Param
(
# Paths to bigfiles
[Parameter(Mandatory=$false,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$false,
ValueFromRemainingArguments=$false,
Position=0,
ParameterSetName='All')]
[ValidateScript({Test-Path $_ -PathType Container})]
[ValidateNotNullOrEmpty()]
[String[]]
$Path
)
Begin
{
Write-Verbose "Starting Function Split-BigFiles"
}
Process
{
ForEach($folder in $Path)
{
Try
{
$bigfiles = Get-ChildItem -Path $folder -ErrorAction Stop
}
Catch
{
Write-Warning "Oh no i couldn't get the folder! This is the error i got $($_.Exception.Message)"
}
if ($bigfiles)
{
ForEach ($path in $bigfiles)
{
$contents = Get-Content $path.FullName
ForEach ($line in $contents)
{
$splitItems = $line.split("'")
For ($i = 0; $i -lt $splitItems.count; $i++)
{
New-Item -Path "$($path.Directory)\$($path.BaseName)$i.txt" -ItemType File -Value $splitItems[$i]
}
}
}
}
}
}
End
{
Write-Verbose "Exiting Function Split-BigFiles"
}
}
Upvotes: 1