Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #################################################################################################################
- # https://stackoverflow.com/a/48676531/9248774
- #
- # Converts 4 digit binary strings to decimal while sorting out all strings which equal 0 or are greater than 9.
- #
- # Adjust the source directory and input and output file names (files don't have to be .txt files).
- #
- $source = "C:\adjust\path"
- $input_file = "file_name.extension"
- $dec_output = "dec_output.txt"
- $bin_output = "bin_output.txt"
- #
- #
- # Using Get-Content on an input file with a size of 1GB or more will cause System.OutOfMemoryExceptions,
- # therefore a large file gets temporarily split up.
- #
- $split_size = 100MB
- $echo_ss = $split_size/1MB
- #
- #
- # This adds carriage returns to the temporary file after each 16'384th character. Although the sweet spot is
- # somewhere around 18'000 characters, the line length needs to be dividable by 4 and at best fit exactly n times
- # into the temporary file; using 16'384 or characters (that is exactly 16 KB) ensures that.
- #
- $line_length = 16384
- #
- #
- # Thanks @BenN (https://superuser.com/a/1292916/868077)
- # Thanks @Bob (https://superuser.com/a/1295082/868077)
- #################################################################################################################
- Set-Location $source
- if (Test-Path $bin_output) {
- $name = (Get-Item $bin_output).Basename
- $ext = (Get-Item $bin_output).Extension
- $num = 1
- while ($num -le 9999) {
- $test = $name+"_"+$num+$ext
- if (Test-Path $test) {
- $num += 1
- } else {
- break
- }
- }
- Rename-Item $bin_output $test
- $a = "`n`n Renamed 'bin_output'!"
- }
- if (Test-Path $dec_output) {
- $name = (Get-Item $dec_output).Basename
- $ext = (Get-Item $dec_output).Extension
- $num = 1
- while ($num -le 9999) {
- $test = $name+"_"+$num+$ext
- if (Test-Path $test) {
- $num += 1
- } else {
- break
- }
- }
- Rename-Item $dec_output $test
- $b = "`n`n Renamed 'dec_output'!"
- }
- if (Test-Path ".\_temp") {
- "`n"
- while ($overwrite -ne "true" -and $overwrite -ne "false") {
- $overwrite = Read-Host ' Splitted files already/still exists! Delete and recreate?'
- if ($overwrite -match "y") {
- $overwrite = "true"
- Remove-Item .\_temp -force -recurse
- $c = " Deleted existing splitted files and creating new ones!"
- } elseif ($overwrite -match "n") {
- $overwrite = "false"
- } elseif ($overwrite -match "c") {
- exit
- } else {
- "`n"
- Write-Host " Error: Invalid input!" "`n" " Type 'y' for 'yes'." " Type 'n' for 'no'." " Type 'c' for 'cancel'."
- "`n"
- "`n"
- }
- }
- }
- Clear-Host
- "`n"
- while ($delete -ne "true" -and $delete -ne "false") {
- $delete = Read-Host ' Delete splitted files afterwards?'
- if ($delete -match "y") {
- $delete = "true"
- $d = "`n`n Splitted files will be deleted afterwards!"
- } elseif ($delete -match "n") {
- $delete = "false"
- $d = "`n`n Splitted files will not be deleted afterwards!"
- } elseif ($delete -match "c") {
- exit
- } else {
- "`n"
- Write-Host " Error: Invalid input!" "`n" " Type 'y' for 'yes'." " Type 'n' for 'no'." " Type 'c' for 'cancel'."
- "`n"
- "`n"
- }
- }
- Clear-Host
- "`n"; "`n"; "`n"; "`n"; "`n"; "`n"
- $a
- $b
- $d
- $start_o = (Get-Date)
- if ($overwrite -ne "false") {
- $c
- "`n"
- $start = Get-Date
- New-Item -ItemType directory -Path ".\_temp" >$null 2>&1
- [Environment]::CurrentDirectory = Get-Location
- $bytes = New-Object byte[] 4096
- $in_file = [System.IO.File]::OpenRead($input_file)
- $file_count = 0
- $finished = $false
- if ((Get-Item $input_file).length -gt $split_size) {
- Write-Host " Input file larger than $echo_ss MB!"
- Write-Host " Splitting input file and inserting carriage returns..."
- $v=([MATH]::Floor([decimal]((Get-Item $input_file).Length/100MB)))
- $sec_rem = -1
- while (!$finished) {
- $perc = [MATH]::Round($file_count/$v*100)
- $file_count++
- $bytes_to_read = $split_size
- $out_file = New-Object System.IO.FileStream ".\_temp\_temp_$file_count.tmp",CreateNew,Write,None
- while ($bytes_to_read) {
- $bytes_read = $in_file.Read($bytes, 0, [Math]::Min($bytes.Length, $bytes_to_read))
- if (!$bytes_read) {
- $finished = $true
- break
- }
- $bytes_to_read -= $bytes_read
- $out_file.Write($bytes, 0, $bytes_read)
- }
- if (($i = $file_count-1) -gt 0) {
- (Get-Content ".\_temp\_temp_$i.tmp") -Replace ".{$line_length}", "$&`r`n" | Set-Content ".\_temp\_temp_$i.tmp"
- }
- $out_file.Dispose()
- $sec_elaps = (Get-Date) - $start
- $sec_rem = ($sec_elaps.TotalSeconds/$file_count) * ($v-$file_count+1)
- Write-Progress -Id 1 -Activity "Splitting input file and inserting carriage returns..." -Status "Progress ($perc%):" -PercentComplete ($perc) -SecondsRemaining $sec_rem
- }
- $in_file.Dispose()
- (Get-Content ".\_temp\_temp_$file_count.tmp") -Replace ".{$line_length}", "$&`r`n" | Set-Content ".\_temp\_temp_$file_count.tmp"
- Write-Progress -Id 1 -Activity null -Completed
- } else {
- if ((Get-Item $input_file).length -lt $split_size) {
- " Input file smaller than $echo_ss MB!"
- } else {
- " Input file exactly $echo_ss MB!"
- }
- Write-Host " Inserting carriage returns..."
- (Get-Content $input_file) -Replace ".{$line_length}", "$&`r`n" | Set-Content ".\_temp\_temp_1.tmp"; $file_count = 1
- }
- $dur = (Get-Date) - $start
- Write-Host "`n Done! Duration:"$dur.ToString("hh\:mm\:ss\.fff")
- "`n"
- } else {
- "`n"
- Write-Host " Continuing with existing files..."
- "`n"
- Get-ChildItem ".\_temp\*" -File -Include *.tmp | ForEach-Object -Process {$file_count++}
- }
- Write-Host " Converting binary into decimal..."
- $sec_rem = -1
- $start = Get-Date
- Get-ChildItem ".\_temp\*" -File -Include *.tmp | ForEach-object -Process {
- $cur_file++
- $line_count = (Get-Content ".\_temp\_temp_$cur_file.tmp").count
- ForEach ($line in Get-Content ".\_temp\_temp_$cur_file.tmp") {
- $cur_line++
- $perc = [MATH]::Round(($cur_file-1+($cur_line/$line_count))/$file_count*100)
- $n = 0
- if ($line.length -ge 4) {
- while ($n -lt $line.length) {
- $dec = 0
- $bin = $line.substring($n,4)
- $dec = ([Convert]::ToInt32($bin,2))
- if ($dec -gt 0 -and $dec -le 9) {
- $temp_dec = "$temp_dec$dec"
- $temp_bin = "$temp_bin$bin"
- }
- $n += 4
- }
- $temp_dec | Add-Content $dec_output -Encoding ascii -NoNewline
- $temp_bin | Add-Content $bin_output -Encoding ascii -NoNewline
- Clear-Variable -Name "temp_dec", "temp_bin"
- }
- $sec_elaps = (Get-Date) - $start
- $sec_rem = ($sec_elaps.TotalSeconds/($cur_file-1+($cur_line/$line_count))) * ($file_count-($cur_file-1+($cur_line/$line_count)))
- Write-Progress -ID 2 -Activity "Converting binary into decimal..." -Status "Progress ($perc%):" -PercentComplete ($perc) -SecondsRemaining $sec_rem -CurrentOperation "Current file: '_temp_$cur_file.tmp'"
- }
- Clear-Variable -Name "cur_line"
- }
- Write-Progress -Activity null -Completed
- $dur = (Get-Date) - $start
- Write-Host "`n Done! Duration:"$dur.ToString("hh\:mm\:ss\.fff")
- "`n"
- if ($delete -eq "true") {
- Remove-Item ".\_temp" -Force -Recurse
- }
- "`n"
- "`n"
- Write-Host " Script finished!"
- Write-Host " Start time: "$start_o.ToString("dd\.MM\.yyyy\ hh\:mm\:ss\.fff")
- Write-Host " End time: "(Get-Date).ToString("dd\.MM\.yyyy\ hh\:mm\:ss\.fff")
- $dur = (Get-Date) - $start_o
- Write-Host "`n Duration: "$dur.ToString("hh\:mm\:ss\.fff")
- "`n`n`n"
- Pause
- Exit
Add Comment
Please, Sign In to add comment