Batch file to go through directory and remove characters

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Casti
Posts: 11
Joined: 22 Mar 2012 10:53

Batch file to go through directory and remove characters

#1 Post by Casti » 22 Mar 2012 11:04

I have tried looking through the board to do this and have not found all the information in one place so as a Noobie I really would appreciate some help.

I am trying to loop through a directory of files:

z:\cgi2009\data

Go though each file and and remove CR LF or \n from each line in all the lines.

If there were 4 files in the directory and the first file looked like this:

REF^EI^956006143~
REF^1C^050262~
REF^1C^050262~
REF^1C^050262~

I would like the CR LF to be removed so the data in the file would be all on one line.

REF^EI^956006143~REF^1C^050262~REF^1C^050262~REF^1C^050262~

This process would repeat for all the files in the directory.

Thank you very much in advance!!!!

Casti

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch file to go through directory and remove characters

#2 Post by foxidrive » 22 Mar 2012 11:32

This works here.

Input line lengths should be less than 1022 bytes.
blank lines are ignored
lines starting with ; are ignored.

Code: Select all

@echo off
for /f "delims=" %%a in ('dir /a-d /b ^|find /v "%~nx0"')  do (
for /f "delims=" %%b in ('type "%%a"')  do (
set /p "=%%b"<nul >>tempfile$$.txt
)
move /y tempfile$$.txt "%%a"
)
pause

Casti
Posts: 11
Joined: 22 Mar 2012 10:53

Re: Batch file to go through directory and remove characters

#3 Post by Casti » 22 Mar 2012 11:36

Thanks for the quick response ..I will test it right now!!!

Casti :D

Casti
Posts: 11
Joined: 22 Mar 2012 10:53

Re: Batch file to go through directory and remove characters

#4 Post by Casti » 22 Mar 2012 11:47

Works great!!!!

I am being lazy and I will look at the board BUT you may be faster..

At the end after the first move I would like to move the file to a another location..a subdiretcoy called DONE so I don't have to process the file again.

Thanks

Casti :lol:

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch file to go through directory and remove characters

#5 Post by foxidrive » 22 Mar 2012 12:06

This is untested - remove the REM if it works, so that it deletes the original files.

Code: Select all

@echo off
md "Done" 2>nul
for /f "delims=" %%a in ('dir /a-d /b ^|find /v "%~nx0"')  do (
for /f "delims=" %%b in ('type "%%a"')  do (
set /p "=%%b"<nul >>"Done\%%a"
)
REM del "%%a"
)
pause

Casti
Posts: 11
Joined: 22 Mar 2012 10:53

Re: Batch file to go through directory and remove characters

#6 Post by Casti » 22 Mar 2012 15:55

Okay this works great.

I tried embedding the following Utility program called CHRTOCHR and this batch does not work. Any suggestions?

This Utility will also remove characters from a file

set "OUTPUTDIR=C:\PROCESSFILES\SECOND\DONE"

for /f "delims=" %%a in ('dir /a-d /b') do (
CHRTOCHR "%%a" CHR(126)+CHR(13)+CHR(10) CHR(126) ERA.txt
move /y ERA.TXT "%%a"
move "%%a" "%OUTPUTDIR%")
)


Thanks

Casti

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch file to go through directory and remove characters

#7 Post by foxidrive » 23 Mar 2012 00:31

Does that CHRTOCHR command work on the command line?

Casti
Posts: 11
Joined: 22 Mar 2012 10:53

Re: Batch file to go through directory and remove characters

#8 Post by Casti » 23 Mar 2012 00:53

Yes it does but here is my code that is not working:

set "SECONDSRC=C:\PROCESSFILES\SECOND"
set "OUTPUTDIR=C:\PROCESSFILES\SECOND\DONE"
set "TCRLF=CHR(126)+CHR(13)+CHR(10)"
set "CRLF=CHR(126)"

pushd "%secondsrc%"

for %%a in (*.835) do (
CHRTOCHR %%a %TCRLF% %CRLF% ERA.txt
pause
move /y ERA.TXT "%%a"
move "%%a" "%OUTPUTDIR%")



If I change

CHRTOCHR %%a %TCRLF% %CRLF% ERA.txt

to REM CHRTOCHR %%a %TCRLF% %CRLF% ERA.txt

I get the right information on the screen the program BUT without the "REM" it doesn't work and the screen just flashes too quickly..

Thanks

Casti

Casti
Posts: 11
Joined: 22 Mar 2012 10:53

Re: Batch file to go through directory and remove characters

#9 Post by Casti » 23 Mar 2012 01:05

One thing that I did notice is that the first time through

I get the following

CHRTOCHR %a CHR(126)+CHR(13)+CHR(10) CHR(126) ERA.TXT

and the Pause does not stop.

Then I do get a real file

CHRTOCHR ERAOO1.835 CHR(126)+CHR(13)+CHR(10) CHR(126) ERA.TXT

and the pause appears on the screen.

I have to go to sleep so I wll check back later.

Thanks

Casti

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch file to go through directory and remove characters

#10 Post by foxidrive » 23 Mar 2012 01:15

try this:

CALL CHRTOCHR %%a %TCRLF% %CRLF% ERA.txt


and if %%a contains long filename elements then quote the %%a

CALL CHRTOCHR "%%a" %TCRLF% %CRLF% ERA.txt

Casti
Posts: 11
Joined: 22 Mar 2012 10:53

Re: Batch file to go through directory and remove characters

#11 Post by Casti » 23 Mar 2012 07:53

I tried both those as I remembered about the "CALL" function.

What I think is my issue is that the first time the procedure is called I get a bogus first file name

CHRTOCHR %a CHR(126)+CHR(13)+CHR(10) CHR(126) ERA.TXT

The above is the actual text that is displayed and the program bombs and gives a message on the screen that the file name is wrong.

Thanks

Casti

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch file to go through directory and remove characters

#12 Post by foxidrive » 23 Mar 2012 08:59

Try it with the echo off and the code below which will just echo the filenames. See if they are all legitimate.

Code: Select all

@echo off
set "SECONDSRC=C:\PROCESSFILES\SECOND"
set "OUTPUTDIR=C:\PROCESSFILES\SECOND\DONE"
set "TCRLF=CHR(126)+CHR(13)+CHR(10)"
set "CRLF=CHR(126)"

pushd "%secondsrc%"

for %%a in (*.835) do (
echo "%%a"
pause
)
popd


Then try this and see if it runs successfully and processes the file. Change the "filename.835" to a legitimate file.


Code: Select all

@echo off
set "SECONDSRC=C:\PROCESSFILES\SECOND"
set "OUTPUTDIR=C:\PROCESSFILES\SECOND\DONE"
set "TCRLF=CHR(126)+CHR(13)+CHR(10)"
set "CRLF=CHR(126)"

pushd "%secondsrc%"

CHRTOCHR "filename.835" %TCRLF% %CRLF% ERA.txt

popd



Using echo off allows you to see the errors easily. It is very seldom that I don't include @echo off


BTW, is CHRTOCHR a batch file or an executable?

Casti
Posts: 11
Joined: 22 Mar 2012 10:53

Re: Batch file to go through directory and remove characters

#13 Post by Casti » 23 Mar 2012 10:59

CHRTOCHR is an exe that replaces characters.

So I tried your examples and they worked perfectly. The files were all listed correctly and the file is processed in the seocnd example.

It appears to me that when I use the for loop it doesn't work.
for %%a in (*.835) do (
C:\PROCESSFILES\CHRTOCHR "%%a_REMIT0027100271.835" %TCRLF% %CRLF% ERA.txt
)

I even tried hard coding the %%a like in your second example and that didn't work either.

for %%a in (*.835) do (

C:\PROCESSFILES\CHRTOCHR "1366401333_REMIT0027100271.835" %TCRLF% %CRLF% ERA.txt

)

Interesting.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch file to go through directory and remove characters

#14 Post by foxidrive » 23 Mar 2012 11:30

Does this work?

I think it needs the escaping of the parentheses - it wasn't clear earlier but I see now that they will cause the issue.

Code: Select all

@echo off
set "TCRLF=CHR^(126^)+CHR^(13^)+CHR^(10^)"
set "CRLF=CHR^(126^)"
for %%a in (1) do (
C:\PROCESSFILES\CHRTOCHR "1366401333_REMIT0027100271.835" %TCRLF% %CRLF% ERA.txt
)

Casti
Posts: 11
Joined: 22 Mar 2012 10:53

Re: Batch file to go through directory and remove characters

#15 Post by Casti » 23 Mar 2012 11:46

Yes that did the trick...

I had the orginal issue so I place the SET command at top so that I would not have the parentheses issue.

Thanks for your help..Your solution ours works great.

I am going to try the following:

@echo off
set "TCRLF=CHR^(126^)+CHR^(13^)+CHR^(10^)"
set "CRLF=CHR^(126^)"
for %%a in (*.835) do (
C:\PROCESSFILES\CHRTOCHR "%%a" %TCRLF% %CRLF% ERA.txt
)

to see if processes all the files

Thanks

Casti

Post Reply