0

with respect to this question, i am trying to adjust the suggested script to changed needs but am failing. Here is the context:

  • The script below parses through a set of files with names like EventLog_12345.txt
  • In every file it identifies the line that contains the string vVariable (example 2018-06-22 08:21:19 0133 LET vVariable = 'h**ps://somedomain.com/test/joblog.XML' )
  • It gets the file name of the URL at the end of this line (joblog)
  • The saves the file as a new file with a new name: joblog_12345.txt

  • The following is the script suggested in the referred post, which does the job.

    :: Q:\Test\2018\05\22\SO_50982243.cmd
    @Echo off & Setlocal EnableDelayedExpansion
    For /f "delims=" %%A in ('findstr /I /M "vVariable" *_*.txt ') Do (
       For /f "tokens=2delims=_" %%B in ("%%~nA") Do (
           For /f "delims=" %%C in ('findstr /I "vVariable" ^<"%%A"') Do Set "Line=%%C"
           Set "Line=!Line:/= !"
           For %%D in (!Line!) Do Set "NewName=%%~nD_%%B%%~xA"
           Echo Ren "%%~A" "!NewName!"
       )
    )
    
  • However, when i try to do the same thing on a set of files that have different names, i am desperately failing (i played around with the *_*.txt piece to get this done, but with no success.)

  • The filenames that i need to identify are looking something like this: Worker FFM Generator.qcc.2018_06_24_14_46_55.txt where evereything up until the numbers start is a constant - so the filenames differ only in their numeric part. Eventually i want to save a new file (picking up the example line from above) as joblog_2018_06_24_14_46_55.txt

Thanks a lot!

kalinkula
  • 3
  • 1
  • 5

1 Answers1

2

On the innermost for /F loop, change "%%A" to "%%~A" or "%%~fA". As-is, your filenames containing spaces are populating %%A with quoted results. Therefore, for /F is probably trying to execute something like this:

findstr /I "vVariable" ^<""Worker FFM Generator.qcc.2018""

... which is incorrect. Adding the tilde will strip the inner quotes if present, keeping your explicit quotes valid.

For future reference, using @echo on can help you track down simple bugs such as this, letting you see where the script doesn't behave as expected.

If your script does not live in the same directory as your log files, you should also add pushd directoryname below @echo off, putting the name of the directory containing "Worker FFM Generator etc." in place of directoryname. If you need the script to search files recursively, add the /S switch to findstr.

rojo
  • 24,000
  • 5
  • 55
  • 101
  • Thanks. Almost all good. Only thing: The new file name is not correct. Using the following as example file: `Worker FFM Generator.qcc.2018_06_25_07_46_28` and line: `2018-06-25 09:45:55 0133 LET vVariable = 'h**ps://somedomain.com/svn/BusinessContent/TEST/Generators/'&'Alliances/GEN_Quota.xml'` the script suggestes a new file name of `vVariable_06.txt`(it actually always seems to suggest this file name), where the correct new file name should be `GEN_Quota_2018_06_25_07_46_28.txt` – kalinkula Jun 25 '18 at 08:49
  • @kalinkula I bet for the same reasons `"%%~A"` needs a tilde, you also need to insert a tilde in `%%~B` on the `For %%D in (!Line!) Do Set "NewName=%%~nD_%%B%%~xA"` line. It should be `...Set "NewName=%%~nD_%%~B%%~xA"`. If that doesn't help, then just above `Set "Line=!Line:/= !"` try putting `echo(!Line!` and see whether the rightmost token in that value is `vVariable_06` or `GEN_Quota_etc`. Maybe the contents of your WORKER FPM Generator file has a newline after vVariable? – rojo Jun 26 '18 at 00:16