0

I'd like to search for a list of words from an external list (simple each word on a line) which we'll call "List.txt", and search for them in a file (C:\Users\P Ditty\Documents\SH3\data\cfg\Backups_SCR*.clg) (matching the whole word, even if it's inside another, and matching case), then if they are there find those words on another file(Campaign_SCR.mis.tmp) (matching the whole word even if its inside another, matching case) replacing the whole line in (Campaign_SCR.mis.tmp) with Name=ShipDummy ONLY if the line starts with "Name=". After that the two lines below that in that same file would be replaced with 2nd line "Class=ShipDummy", then 3rd line "Type=206".

Here is the code I have now. Now it searches for ClassName=Variable in C:\Users\P Ditty\Documents\SH3\data\cfg\Backups_SCR*.clg, then it replaces the whole line with Class=ShipDummy if it finds the variable in Campaign_SCR.mis.tmp. It also replaces 1 line below it with "Type=206".

setlocal enableDelayedExpansion

>Campaign_SCR.mis.tmp (
for /f "tokens=1* delims=:" %%A in ('findstr /n "^" Campaign_SCR.mis.backup') do (
    ( echo !ln!| findstr "^Type=12$" >NUL && set ln=ln ) || (
        set "ln=%%B"
        if "!ln:~0,6!"=="Class=" (
            findstr /s /c:"ClassName=!ln:~6!" "C:\Users\P Ditty\Documents\SH3\data\cfg\Backups_SCR\*.clg" >"C:\Users\P Ditty\Documents\SH3\data\cfg\Backups_SCR\null" && (
                echo Class=ShipDummy
                set "ln=Type=12"
            )
        )
        if #!ln!==# (echo;) else echo !ln!
    )
  )
)

BUT I'd like it to search for a name from a list I specify, search in (C:\Users\P Ditty\Documents\SH3\data\cfg\Backups_SCR*.clg). Then if it finds the match, then search for that in Campaign_SCR.mis.tmp and replace the whole line, only if that line starts with "Name=", with Name=ShipDummy. The two lines below that would be replaced with 2nd line "Class=ShipDummy", then 3rd line "Type=206":

Name=ShipDummy
Class=ShipDummy
Type=206

**Please keep in mind that within Campaign_SCR.mis.tmp the variable's line wont often appear as Name=variable. It is likely to be Name=(words)variable(words)

Can the list that is searched be external? Can we make it search one word per line. For exmaple: if the list is as follows:

Bismarck Hood Repulse

It will search for each word.**

When it matches the words from the list to (C:\Users\P Ditty\Documents\SH3\data\cfg\Backups_SCR*.clg) please ensure that it matches case as well. So "Bismarck" from list will find "Bismarck" from (C:\Users\P Ditty\Documents\SH3\data\cfg\Backups_SCR*.clg), and not find "bismarck"

Thank you for your time!

By the way, this may be a simpler workaround to the problem I have here: How do I apply this?

So for example:

Say I have an external list (call it List.txt for example) that looks like this:

Bismarck
Hood
Repulse

I will search C:\Users\P Ditty\Documents\SH3\data\cfg\Backups_SCR*.clg for that. Say it finds "Bismarck" and "Hood" in this:

sadfasfasfdBismarckfasdfasdfasdfas

asdfasfdafHoodasdfasfas

Then it will search Campaign_SCR.mis.tmp for Bismarck and Hood replacing:

Name=asdfBismarckasfdw
Class=jlkjf
Type=12

With:

Name=ShipDummy
Class=ShipDummy
Type=206

And

Name=asdfHoodasfdw
Class=jlkjf
Type=13

With:

Name=ShipDummy
Class=ShipDummy
Type=206
Community
  • 1
  • 1
machiavelli
  • 92
  • 1
  • 1
  • 9

1 Answers1

1
@echo off
setlocal EnableDelayedExpansion

REM INITIALIZE THE LIST OF WORDS THAT WILL BE SEARCHED
set targetWords=:EOF

rem I'd like to search for a list of words from an external list (simple each word on a line)
for /F %%a in (List.txt) do (   
   rem and search for them in a file (C:\Uses\P Ditty\Documents\SH3\data\cfg\Backups_SCR*.clg) 
   findstr "%%a" "C:\Uses\P Ditty\Documents\SH3\data\cfg\Backups_SCR*.clg" > NUL
   rem if they are there...
   if !errorlevel! equ 0 (
      REM INSERT THE WORD IN THE TARGET LIST
      set targetWords=!targetWords! %%a
   )
)

REM INSERT THE END-OF-FILE MARK IN THE FILE
echo :EOF>> Campaign_SCR.mis.tmp

REM INITIALIZE THE NUMBER OF LAST PROCESSED LINE IN REDIRECTED Campaign_SCR.mis.tmp
set lastLine=0

rem ... find those words on another file(Campaign_SCR.mis.tmp)
< Campaign_SCR.mis.tmp (for /F "delims=:" %%a in ('findstr /N "%targetWords%" Campaign_SCR.mis.tmp') do (
   REM DUPLICATE PREVIOUS LINES UNTIL NEW TARGET LINE
   set /A numOfLines=%%a-lastLine-1
   for /L %%i in (1,1,!numOfLines!) do (
      set line=
      set /P line=
      echo(!line!
   )
   rem if the line starts with "Name="
   set /P line=
   if "!line:~0,5!" equ "Name=" (
      rem replacing the whole line...with Name=ShipDummy
      echo Name=ShipDummy
      rem After that the two lines below that in that same file would be replaced with 2nd line "Class=ShipDummy", 
      set /P line=
      echo Class=ShipDummy
      rem then 3rd line "Type=206".
      set /P line=
      echo Type=206
      set /A lastLine=%%i+2
   ) else (
      REM DUPLICATE THE NON MATCHING LINE, IF IS NOT THE :EOF MARK
      if "!line!" neq ":EOF" (
         echo !line!
         set lastLine=%%i
      )
   )
)) > Campaign_SCR.mis.tmp.NEW

REM UPDATE THE NEW FILE
REM del Campaign_SCR.mis.tmp
REM ren Campaign_SCR.mis.tmp.NEW Campaign_SCR.mis.tmp
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • Wow, that was fast!. I'm going to test this out and report back. – machiavelli Feb 23 '13 at 01:29
  • Now what if I was to add "C:\Uses\P Ditty\Documents\SH3\data\cfg\Backups_SCR*.cfg" as well to the "C:\Uses\P Ditty\Documents\SH3\data\cfg\Backups_SCR*.clg" Do I just put in parenthesis the complete filename next to both instances of .clg in your script, and it will search both files? – machiavelli Feb 23 '13 at 01:30
  • I changed my username by the way – machiavelli Feb 23 '13 at 01:38
  • Almost got it. It changes one, but then for the rest it says ECHO IS OFF – machiavelli Feb 23 '13 at 02:59