BATCH script to find string from file and append like variable in file name

batch filescript

Hi guys i need little help with batch script…
from file something.txt which contain

data
data
LastLogedUser=John.Doe
data
data

i want to find the string LastLogedUser= and append the text after that (in this case "John.Doe" like a variable in copy of other file…
Like this but something is wrong in my code:

for /F "delims=" %%a in ('findstr /I "LastLogedUser=" something.txt') do set "uniuser=%%a"  
echo User is: %uniuser%   
copy fpr_log.txt c:\fpr_log%uniuser%.txt

Best Answer

Something is wrong in my code

findstr /I "LastLogedUser=" something.txt is returning the whole line from the file:

> findstr /I "LastLogedUser=" something.txt
LastLogedUser=John.Doe

So your for loop needs to use = as a delimiter and get the second token in order to extract John.Doe.

I've also added some "s in case there are spaces in the user name ...

Corrected batch file:

@echo off
setlocal
for /F "tokens=2 delims==" %%a in ('findstr /I "LastLogedUser=" something.txt') do set "uniuser=%%a"  
echo User is: %uniuser%   
copy fpr_log.txt "c:\fpr_log%uniuser%.txt"
endlocal

Further Reading

Related Question