0

I have multiple files that have a line like this somewhere in the file:

10.18.77.14.152392.16.APP_Ret,04/10/2018 17:21:07.592,,start,ani,1234567890

The line has several pieces of information and can be variable in length.

I'm trying to write a batch file that will look in a directory, search each file for the string start,ani and then set a variable off of the values. Like

VAR1=04/10/2018
VAR2=17:21:07
VAR3=1234567890

Then I need to change the / and : to _ and finally rename the file thus:

1234567890_04_10_201817_21_07.txt

I can handle changing the characters to underscores, the issue I am having is trying to extract that line and set the information into variables.

Here is what I've been playing with:

cd /d %tmp%
set var1=1
if %var1%==1 (
    for /F "delims=" %%a in ('findstr /I "start,ani" %tmp%\*.txt') do (
        set "var=%%a"
        echo %%a
        echo %var%
    )
)

My goal here was just to find the line and set var to the entire contents. But when I echo var it just says echo off.

Compo
  • 36,585
  • 5
  • 27
  • 39
mte0910
  • 27
  • 5
  • Long time listener, first time poster. I should add that I'm not a programmer, I just basardize other people's hard work into a format that works for me. – mte0910 Apr 11 '18 at 16:15
  • A long time listner would have noted that the most common issue here is related to delayed expansion! – Compo Apr 11 '18 at 16:33

2 Answers2

0

After I turned off the echoing of commands run, by adding @echo off at the top of the script, I had no issues with the script. I created a file called file.txt, pasted the mock line you provided, and placed this the directory stored in the tmp environment variable. Once i ran the script, the var environment variable contained the line found and it was also printed out as expected.

The script I used:

@echo off

cd /d %tmp%
set var1=1
if %var1%==1 (
    for /F "delims=" %%a in ('findstr /I "start,ani" %tmp%\*.txt') do (
        set "var=%%a"
        echo %%a
        echo %var%
    )
)

This post has a bit more on the @ and echo off.

Update: I'm not sure of your full scenario but this may be something you eventually run into since you're setting a variable and echoing it in a for loop. If you have two lines in a file with the token "start,ani" your output will look like the following (the file path has been removed and replaced with simply [file path]):

[file path]:10.18.77.14.152392.16.APP_Ret,04/10/2018 17:21:07.592,,start,ani,1234567890 [file path]:11.18.77.14.152392.16.APP_Ret,04/10/2018 17:21:07.592,,start,ani,1234567890 [file path]:11.18.77.14.152392.16.APP_Ret,04/10/2018 17:21:07.592,,start,ani,1234567890 [file path]:11.18.77.14.152392.16.APP_Ret,04/10/2018 17:21:07.592,,start,ani,1234567890

As you can see the first echo of %var% is the same as the last and this is because the variable is only expanded once. To get more expected output you would need to use delayed expansion:

@echo off
setlocal ENABLEDELAYEDEXPANSION

cd /d %tmp%
set var1=1
if %var1%==1 (
    for /F "delims=" %%a in ('findstr /I "start,ani" %tmp%\*.txt') do (
        set "var=%%a"
        echo %%a
        echo !var!
    )
)

Now the first echo of var will display as expected:

[file path]:10.18.77.14.152392.16.APP_Ret,04/10/2018 17:21:07.592,,start,ani,1234567890 [file path]:10.18.77.14.152392.16.APP_Ret,04/10/2018 17:21:07.592,,start,ani,1234567890 [file path]:11.18.77.14.152392.16.APP_Ret,04/10/2018 17:21:07.592,,start,ani,1234567890 [file path]:11.18.77.14.152392.16.APP_Ret,04/10/2018 17:21:07.592,,start,ani,1234567890

This post has more info on delayed expansion.

Fizz
  • 3,427
  • 4
  • 27
  • 43
  • I need to do some more research to understand the %var% versus the !var!. But this works great! Thank you very much. – mte0910 Apr 11 '18 at 17:11
0

If you wanted to go further, and split your individual lines down in order to get your required variables and rename the files, you could do so in a called label section returning to the loop for the rename.

Example, (based on the text file snippet posted and only one matching line in any file):

@Echo Off
SetLocal EnableDelayedExpansion
For /F "Tokens=1* Delims=:" %%A In (
    'FindStr/RIC:",start,ani," *.txt'
) Do (
    Call :Sub "%%B"
    Echo=Ren "%%A" "!VAR3!_!VAR1!_!VAR2!%%~xA"
)
Pause
Exit /B

    :Sub
    Set "line=%*"
    Set "line=%line:,=","%"
    Set "#=0"
    For %%A In (%line%) Do (
        Set /A #+=1
        Set "VAR!#!=%%~A"
    )
    For /F "Tokens=1-6 Delims=/:. " %%A In ("%VAR2%") Do (
        Set "VAR1=%%A_%%B_%%C"
        Set "VAR2=%%D_%%E_%%F"
    )
    Set "VAR3=%VAR6%"
    For %%A In (4 5 6) Do Set "VAR%%A="
    Exit /B

Remove Echo=, and optionally Pause, if the output looks good.

Compo
  • 36,585
  • 5
  • 27
  • 39
  • Oh wow! That is exactly what I was trying to do. I was working with a nested if and running into exactly what Fizz suggested I might. If I understand correctly, you are handing the renaming WHILE in the original LOOP? Thank you both for your help, I'm on my way now... – mte0910 Apr 11 '18 at 19:23