Batch file to go through directory and remove characters
Moderator: DosItHelp
Batch file to go through directory and remove characters
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
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
Re: Batch file to go through directory and remove characters
This works here.
Input line lengths should be less than 1022 bytes.
blank lines are ignored
lines starting with ; are ignored.
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
Re: Batch file to go through directory and remove characters
Thanks for the quick response ..I will test it right now!!!
Casti
Casti

Re: Batch file to go through directory and remove characters
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
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

Re: Batch file to go through directory and remove characters
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
Re: Batch file to go through directory and remove characters
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
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
Re: Batch file to go through directory and remove characters
Does that CHRTOCHR command work on the command line?
Re: Batch file to go through directory and remove characters
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
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
Re: Batch file to go through directory and remove characters
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
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
Re: Batch file to go through directory and remove characters
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
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
Re: Batch file to go through directory and remove characters
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
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
Re: Batch file to go through directory and remove characters
Try it with the echo off and the code below which will just echo the filenames. See if they are all legitimate.
Then try this and see if it runs successfully and processes the file. Change the "filename.835" to a legitimate file.
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?
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?
Re: Batch file to go through directory and remove characters
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.
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.
Re: Batch file to go through directory and remove characters
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.
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
)
Re: Batch file to go through directory and remove characters
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
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