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-201 6.txt
set OutFile=C:\Copy_EJ_Files\T emp\EJ_15- 02-2016.tx t
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
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
set OutFile=C:\Copy_EJ_Files\T
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
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.
ASKER
how to do it in powershell
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
You code work and also this one but it take long time for big files
$filenames = @("C:\Copy_EJ_Files\EJ_15- 02-2016.tx t")
foreach ($file in $filenames) {
$outfile = "$file" + ".out"
Get-Content $file | Foreach-object {
$_ -replace '\~}}|{{E', "`r`nE" `
-replace '\|\r\n', "`r`n"
} | Set-Content $outfile
}
$filenames = @("C:\Copy_EJ_Files\EJ_15-
foreach ($file in $filenames) {
$outfile = "$file" + ".out"
Get-Content $file | Foreach-object {
$_ -replace '\~}}|{{E', "`r`nE" `
-replace '\|\r\n', "`r`n"
} | Set-Content $outfile
}
ASKER
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
~bp
ASKER
i mean without mention the file name just point to the txt file in the folder
ASKER
how to make this commad proccess big files
Get-Content .\test.txt | % {$_ -split "$([char]27)\[\d*[@-~]"} | ? {$_} | Set-Content -Path .\test-NoEsc.txt
Get-Content .\test.txt | % {$_ -split "$([char]27)\[\d*[@-~]"} | ? {$_} | Set-Content -Path .\test-NoEsc.txt
ASKER
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
is faster.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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_E sc_Char.ps 1'"
pause"
the error i receive
C:\Copy_EJ_Files\Remove_Es c_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_E sc_Char.ps 1'
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorExcep
tion
+ FullyQualifiedErrorId : Microsoft.PowerShell.Comma nds.WriteE rrorExcept io
n,Remove_Esc_Char.ps1
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%
copy /a "C:\Copy_EJ_Files\Temp\*.*
DEL /Q "C:\Copy_EJ_Files\Temp\*.*
note:it will work if run it manualy
PowerShell.exe -Command "& 'C:\Copy_EJ_Files\Remove_E
pause"
the error i receive
C:\Copy_EJ_Files\Remove_Es
argument(s): "Could not find a part of the path 'C:\Windows\system32\Temp\
-02-2016_NoEsc.txt'."
At line:1 char:2
+ & <<<< 'C:\Copy_EJ_Files\Remove_E
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorExcep
tion
+ FullyQualifiedErrorId : Microsoft.PowerShell.Comma
n,Remove_Esc_Char.ps1
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks alot
ASKER
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 ($_.FullNa me)
$StreamWriter = [System.IO.File]::CreateTe xt($OutFil e)
While (($Line = $StreamReader.ReadLine()) -ne $Null) {
ForEach ($Element In [regex]::Split($Line, "\x1B\[\d*[@-~]")) {
If (-not [string]::IsNullOrEmpty($E lement)) {
$StreamWriter.WriteLine($E lement)
}
}
}
} Catch {
$_.Exception.Message | Write-Error
} Finally {
$StreamWriter.Close()
$StreamReader.Close()
}
}
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
$StreamWriter = [System.IO.File]::CreateTe
While (($Line = $StreamReader.ReadLine()) -ne $Null) {
ForEach ($Element In [regex]::Split($Line, "\x1B\[\d*[@-~]")) {
If (-not [string]::IsNullOrEmpty($E
$StreamWriter.WriteLine($E
}
}
}
} Catch {
$_.Exception.Message | Write-Error
} Finally {
$StreamWriter.Close()
$StreamReader.Close()
}
}