Reputation: 3
with respect to this question, i am trying to adjust the suggested script to changed needs but am failing. Here is the context:
EventLog_12345.txt
2018-06-22 08:21:19 0133 LET vVariable = 'h**ps://somedomain.com/test/joblog.XML'
)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.)
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!
Upvotes: 0
Views: 79
Reputation: 24466
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
.
Upvotes: 2