Link to home
Start Free TrialLog in
Avatar of osama120
osama120

asked on

Replace spcial character with new line feed in a bat file

Dear Support,
i have abat file i want to replace  [ESC] with new line please find the attched sanpshot fro both notpadd++ and notpad
 
@echo off
 setlocal enabledelayedexpansion
 set InFile=C:\Copy_EJ_Files\EJ_15-02-2016.txt
 set OutFile=C:\Copy_EJ_Files\Temp\EJ_15-02-2016.txt
 if exist "%OutFile%" del "%OutFile%"
 for /f "tokens=1* delims=[
]" %%a in ('type "%InFile%" ^| find.exe /n /v ""') do (
       set Line=%%b
       if defined Line set Line=!Line:-+=-!
       echo.!Line!
       >>"%OutFile%" echo.!Line!
 )
Sample_code.docx
Avatar of Qlemo
Qlemo
Flag of Germany image

Is [ESC] meant literally, or should it be the character 27 (0x1e, ESC)? The latter is very difficult, if not impossible, in a cmd batch, and you are better of with VBS or PowerShell.
Avatar of osama120
osama120

ASKER

how to do it in powershell
SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You code work and also this one but it take long time for big files
$filenames = @("C:\Copy_EJ_Files\EJ_15-02-2016.txt")
foreach ($file in $filenames) {
    $outfile = "$file" + ".out"

    Get-Content $file | Foreach-object {
        $_ -replace '\~}}|{{E', "`r`nE" `
            -replace '\|\r\n', "`r`n"
    } | Set-Content $outfile
}
how can i process  it with mention the file name only *.txt
These resemble ANSI control sequences, but in Windows these are pretty much obsolete these days.  Was this file generated on a unix system perhaps and captured from a data stream that was designed to be output on a terminal?  Although I don't recall <ESC>[np or <ESC>[nt as defined control codes.  But if you are trying to get the useful data out of the file, and if alignment of the data would be helpful, then it may make sense to actually try and process the full control sequence, not just remove it.  If you could provide a real file here for further analysis as well as a bit more information on where the file comes from, and what you want to do with it, that would also be very helpful.

~bp
i mean without mention the file name just point to the txt file in the folder
how to make this commad proccess big files
Get-Content .\test.txt | % {$_ -split "$([char]27)\[\d*[@-~]"} | ? {$_} | Set-Content -Path .\test-NoEsc.txt
i mean proccess it faster my file size 85555 KB
You can try if using
Get-Content -ReadCount 0 .\test.txt | % {$_ -split "$([char]27)\[\d*[@-~]"} | ? {$_} | Set-Content -Path .\test-NoEsc.txt

Open in new window

is faster.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks alot MR oBdA
 the code above work perfect but when i rum from Task Scheduler after embed in bat file like below
"@ECHO OFF
Robocopy "C:\EJ Export" "C:\Copy_EJ_Files\Temp" /MAXAGE:1 /XO /E

set dt=%date:~7,2%-%date:~4,2%-%date:~10,4%

copy /a "C:\Copy_EJ_Files\Temp\*.*" EJ_%dt%.txt

DEL /Q "C:\Copy_EJ_Files\Temp\*.*"

note:it will work if run it manualy

PowerShell.exe -Command "& 'C:\Copy_EJ_Files\Remove_Esc_Char.ps1'"
 
pause"
 the error i receive

C:\Copy_EJ_Files\Remove_Esc_Char.ps1 : Exception calling "CreateText" with "1"
argument(s): "Could not find a part of the path 'C:\Windows\system32\Temp\EJ_10
-02-2016_NoEsc.txt'."
At line:1 char:2
+ & <<<<  'C:\Copy_EJ_Files\Remove_Esc_Char.ps1'
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorExcep
   tion
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorExceptio
   n,Remove_Esc_Char.ps1
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks alot
Dear oBda
ca we genrate two diferent files one for the one with special charater and the other without
[CmdletBinding()]
Param(
      [string]$Path = $((Get-Location -PSProvider FileSystem).Path + "\*.txt")
)

Get-ChildItem -Path $Path | ForEach-Object {
      Try {
            $OutFile = $_.DirectoryName + "\To_Be_Processed\" + $_.BaseName + "_NoEsc" + $_.Extension
            "Processing '$($_.Name)' --> '$($OutFile)' ..." | Write-Host
            $StreamReader = [System.IO.File]::OpenText($_.FullName)
            $StreamWriter = [System.IO.File]::CreateText($OutFile)
            While (($Line = $StreamReader.ReadLine()) -ne $Null) {
                  ForEach ($Element In [regex]::Split($Line, "\x1B\[\d*[@-~]")) {
                        If (-not [string]::IsNullOrEmpty($Element)) {
                              $StreamWriter.WriteLine($Element)
                        }
                  }
            }
      } Catch {
            $_.Exception.Message | Write-Error
      } Finally {
            $StreamWriter.Close()
            $StreamReader.Close()
      }
}