Question

So basically I have a process that outputs a text file that is pipe delimited, looks something like this:

|abc123|1*|004|**gobbligook|001|%|2014-01-01|||||||||||||

This is just an example, and I'm not sure if the answer involves regular expressions. If it does i will put the actual line. Anyways

ISSUE

So for this example the import process that accepts this file is looking for 8 pipes, but there are 20, if it sees any more pipes after the 8 it's looking for the import process fails.

Question

Is there a process that I can use in a Windows environment to trim the trailing pipes off the end of this for the entire file?

UPDATE

Magoo supplied me with a great answer that I am working but I keep getting this error: Delimiter was unexpected at this time

Here is code:

@ECHO OFF
SETLOCAL
SET "sourcedir=C:\Users\Desktop\Pipe Delimiter Project"
SET "destdir=C:\Users\Desktop\Pipe Delimiter Project"
(
 FOR /f "tokens=1-7delims=|" %%a IN ('TYPE "%sourcedir%\test.txt"') DO (
 ECHO(^|%%a^|%%b^|%%c^|%%d^|%%e^|%%f^|%%g^|
 )
)>%destdir%\newfile.txt

Anyone know what's wrong? I also just put in the line from the question |abc123|..| pasted in the file like 6 times...thanks!

Was it helpful?

Solution

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=."
SET "destdir=U:\destdir"
(
 FOR /f "delims=" %%a IN ('TYPE "%sourcedir%\q22863616.txt"') DO (
 SET "line=%%a"
 ECHO(!line:~0,-12!
 )
)>%destdir%\newfile.txt

GOTO :EOF

I used a file named q22863616.txt containing your data for my testing. Produces newfile.txt

Assuming that the final 12 fields are all empty, for lack of information otherwise.


Another form, given additional information

@ECHO OFF
SETLOCAL
SET "sourcedir=."
SET "destdir=U:\destdir"
(
 FOR /f "tokens=1-7delims=|" %%a IN ('TYPE "%sourcedir%\q22863616.txt"') DO (
 ECHO(^|%%a^|%%b^|%%c^|%%d^|%%e^|%%f^|%%g^|
 )
)>%destdir%\newfile.txt

OK - third time's a charm.

@ECHO OFF
SETLOCAL enabledelayedexpansion
SET "sourcedir=."
SET "destdir=U:\destdir"
:: remove variables starting $
FOR  /F "delims==" %%a In ('set $ 2^>Nul') DO SET "%%a="
(
 FOR /f "delims=" %%a IN ('TYPE "%sourcedir%\q22863616.txt"') DO (
 SET "$0=%%a"
 SET "$1=%%a"
 FOR /l %%c IN (1,1,8) DO SET "$1=!$1:*|=!"
 SET "$2=%%a"
 SET "$3="
 SET /a tot=0
 FOR /f "delims=:" %%e IN ('set $^|findstr /o /r "$"') DO SET /a tot=%%e - !tot! - 5
 CALL :show !tot!
 CALL ECHO %%$2:~0,-!tot!%%
 )
)>%destdir%\newfile.txt

GOTO :EOF

:show
CALL SET "$3=%%$2:~0,-%1%%"
FOR /f "tokens=1*delims==" %%y IN ('set $3') DO ECHO(%%z
GOTO :eof

This seems immune to % in the data, but chokes on ! or &. You pays your money, you takes your choice...

OTHER TIPS

This should eat trailing pipes but leave 8 of them

@echo off
type "file.txt"| repl "(.*?\|{8})\|*$" "$1" >"newfile.txt"

This uses a helper batch file called repl.bat - download from: https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat

Place repl.bat in the same folder as the batch file or in a folder that is on the path.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top