# BATCH:Simple Question.But I'm Lost!

March 27, 2009 at 12:03:06
Specs: Windows XP
 Hello All!,I am having an issue getting a batch program to do what I need it to do. I need it to search through a text file (text.txt) for the string "12/09/09", If it finds "12/09/09", I need it to find the string "8UP-MS" (which is in the same line) and replace it with "8UP-OS".Here is the find/replace code that I have so far:@echo off & setlocal EnableDelayedExpansion > C:\temp\text.new for /F "delims=" %%j in (C:\temp\text.txt) do ( set row=%%j set row=!row:8UP-MS=8UP-OS! ) Any help making this only run if it finds "12/09/09" in the same line would be helpful.

See More: BATCH:Simple Question.But Im Lost!

#1
March 27, 2009 at 13:47:44
 @echo off & setlocal EnableDelayedExpansion > C:\temp\text.new for /F "delims=" %%j in ('type C:\temp\text.txt ^| find "12/09/09"') do set hit=%%j for /F "delims=" %%j in (C:\temp\text.txt) do ( set row=%%j if "%hit%"=="%%j" set row=!row:8UP-MS=8UP-OS! echo !row!>> C:\temp\text.new ) :: End_Of_Batch Well behaved posters report if the response solved the issue.

Report •

#2
March 27, 2009 at 14:07:59
 Ouch. My apologies IVO, I will reply to our earlier thread about the find/replace script (which worked beautifully, thanks!)I am having trouble fitting in this new script into my current program.Here is the complete program, from top to bottom: @echo off cls echo Converting Signs... echo Please Wait. pushd "T:\pos_exchange\pos" for %%j in (*.txt) do ( echo.>> Labels.new type "%%j" >> Labels.new ) del *.txt ren Labels.new Labels.txt copy labels.txt c:\temp\ @echo off & setlocal EnableDelayedExpansion > C:\temp\labels.new for /F "delims=" %%j in (C:\temp\labels.txt) do ( set row=%%j set row=!row:AD_6_UP=8UP-AD ! set row=!row:VBS_6UP=8UP-VB ! set row=!row:MGR_SP=8UP-MS! set row=!row:EDLP6 =8UP-QV! set row=!row:LMT_6UP=8UP-LMT! set row=!row:BOGO1F =8UPB1G1! set row=!row:BOGO2F =8UPB1G2! echo.!row!>> C:\temp\labels.new ) del C:\temp\labels.txt ren C:\temp\labels.new labels.txt copy c:\temp\labels.txt "T:\pos_exchange\pos" echo Done! 

Report •

#3
March 27, 2009 at 14:19:33
 Thanks for the reply.I examine your script, but you need to be patient as the inspection may take time. I hope to re-post the code working.

Report •

Related Solutions

#4
March 27, 2009 at 14:44:47
 Well, I can proced to insert the routine into your script, but I have to point out that the lines replacement for /F strips away the blank lines you previously inserted. If you want to retain them, there are two ways:A- using a cumbersome Find workaroundB- replacing the blank lines with e.g. ---- linesLet me know your coice so I can rearrange the code.

Report •

#5
March 27, 2009 at 15:03:41
 EDIT:I believe I understand what you mean now. Blank lines are no problem and will not affect the output of my code.The blank lines I previously inserted?I'm confused. I wasn't aware that the code was inserting blank lines.Are you talking about the included spaces inset row=!row:VBS_6UP=8UP-VB !orset row=!row:EDLP6 =8UP-QV!?If so, those spaces are needed because the number of characters on either side of the "=" need to remain constant.But if I'm adding blank lines that do not need to be there, it isn't on purpose... Option A, I suppose?

Report •

#6
March 27, 2009 at 15:43:27
 OK, tomorrow you'll get the code you need. Now in Italy time is jate night.

Report •

#7
March 27, 2009 at 17:54:03
 if you can use sed (perl, etc) for windows. just 1 line solves your problem.C:\test>more file.txt 1. this is line 1 2. this is line 2 3. someword 12/09/09 someword 8UP-MS some last words 4. this is line 4 C:\test> sed -e "/12\/09\/09/s/8UP-MS/8UP-OS/" file.txt 1. this is line 1 2. this is line 2 3. someword 12/09/09 someword 8UP-OS some last words 4. this is line 4 

Report •

#8
March 28, 2009 at 03:58:03
 Here your script revised embedding the instructions to check the 12/09/09 line. Beware it is not tested so handle with care.Just pay attention when Delayed Expansion is enabled not to manipulate or disply anymore exclamation marks because they are reserved to dynamic variables' identifiers. So disply Done. not Done!. To manipulate ! characters special coding is required, but that is far beyond this quick note (to know more about Delayed Expansion type SET /? at prompt thought the on-line tutorial is unfriendly).@echo off & setlocal EnableDelayedExpansion > C:\temp\Labels.new cls echo. echo. Converting Signs. echo. Please Wait... pushd T:\pos_exchange\pos for %%j in (*.txt) do ( echo.>> Labels.new type "%%j" >> Labels.new ) del *.txt ren Labels.new Labels.txt copy Labels.txt C:\temp > nul for /F "delims=" %%j in ('type C:\temp\Labels.txt ^| find "12/09/09"') do set hit=%%j for /F "delims=" %%j in (C:\temp\Labels.txt) do ( set row=%%j set row=!row:AD_6_UP=8UP-AD ! set row=!row:VBS_6UP=8UP-VB ! set row=!row:MGR_SP=8UP-MS! set row=!row:EDLP6 =8UP-QV! set row=!row:LMT_6UP=8UP-LMT! set row=!row:BOGO1F =8UPB1G1! set row=!row:BOGO2F =8UPB1G2! if "%hit%"=="%%j" set row=!row:8UP-MS=8UP-OS! echo !row!>> C:\temp\Labels.new ) del C:\temp\Labels.txt ren C:\temp\Labels.new Labels.txt copy C:\temp\Labels.txt > nul popd echo. Done. echo. :: End_Of_Batch 

Report •

#9
March 28, 2009 at 07:19:32
 IVO,I thank you kindly for your help. This script works, however, when I look at the output text file, it seems as if the script only converts the very last line of text that has a "12/09/09" in it. There are lines above it that did not get converted according to the "find" specifications. Is there an easy way around this? or perhaps the easiest way would be to use ghostdog's sed suggestion?

Report •

#10
March 28, 2009 at 07:56:30
 IVO solution is better.

Report •

#11
March 28, 2009 at 09:14:45
 I assumed just one line holds that string; I have to review my script to take into account multiple occurrencies.I'll give you a response asap.

Report •

#12
March 28, 2009 at 09:25:45
 >> or perhaps the easiest way would be to use ghostdog's sed suggestion?first of all, you don't have to use the sed suggestion if the batch works for you and you are restricted to only batch secondly as a side note: I just want to tell you, it took less than 1 minute to come up with that sed solution, but how long does it take for the batch? In the real world, time is precious. if you have tools to do the job faster and more convenient, use them.

Report •

#13
March 28, 2009 at 09:27:43
 >> IVO solution is better.you could at least tell the OP in what sense is it better.

Report •

#14
March 28, 2009 at 09:50:13
 ghostdog,By reading your response, I sense that you are a little angry. I considered using sed, but due to the format in which I'll be using this script, sed would not be the most time constructive use, as I would have to install sed onto multiple machines. Time is precious, right? Thank you for your help!

Report •

#15
March 28, 2009 at 09:55:09
 Delete the statementfor /F "delims=" %%j in ('type C:\temp\Labels.txt ^| find "12/09/09"') do set hit=%%j and replace if "%hit%"=="%%j" set row=!row:8UP-MS=8UP-OS! with set hit=!row:12/09/09=##/##/##! if not "!hit!"=="!row!" set row=!row:8UP-MS=8UP-OS! 

Report •

#16
March 28, 2009 at 10:04:12
 IVO,This works perfectly! Thank you so much!This completes my script! (And all of my questions as of late!)Thank you for all of your help!-Zephyrus14

Report •

#17
March 28, 2009 at 23:38:08
 IVO solution is the best.sed is not for everyone, so stop replying every batch thread and ask people do download your favourite tools. in fact the title say it all: "B.A.T.C.H"

Report •

#18
March 29, 2009 at 01:10:37
 >> sed is not for everyoneif batch is for everyone, OP wouldn't have problems with it in the first place. please don't dictate what I can or cannot post. you can complain about my post if it violates the rules of this forum or if i give a solution that really is OT. other than that, i think you should take a hike. Its up to the OP whether he wants to use sed or not.

Report •