-3

im looking for for a method to set variables from part of a text. I Have a .txt file where is something like this

part of text
part of text
part of text
part of text
password: 123456
part of text

how can i get a variable 123456 from this file?

I try to use

set var1=<"C:Windows\text.txt"

but it is not working

Any idea mate?

Pawel_CK
  • 3
  • 1
  • 1
    there are already a lot of duplicates: [How to read file contents into a variable in a batch file?](https://stackoverflow.com/q/3068929/995714), [Windows Batch: Set Variables from Text File](https://stackoverflow.com/q/5886334/995714), [Set variable equal to a number in a text file Batch](https://stackoverflow.com/q/27094750/995714), [How can I load the contents of a text file into a batch file variable?](https://stackoverflow.com/q/134001/995714)... – phuclv Oct 23 '18 at 08:11
  • 2
    Possible duplicate of [Find text in file and set it as a variable. Batch file](https://stackoverflow.com/questions/22198458/find-text-in-file-and-set-it-as-a-variable-batch-file) – phuclv Oct 23 '18 at 08:12
  • [Find string in text file and set variable with windows batch](https://stackoverflow.com/q/30435794/995714), [Find text in file and set it as a variable. Batch file](https://stackoverflow.com/q/22198458/995714), [Batch - Search text file for specified string and if found set variable to 1](https://stackoverflow.com/q/18170217/995714) – phuclv Oct 23 '18 at 08:14

1 Answers1

1

assuming you are working in a cmd shell on windows, this command from the command line may solve your question

for /F  "tokens=2"  %i in ('findstr "password: " "C:Windows\text.txt"') do set var1=%i

while this is if you need the variable in a batch file (where percentage symbols need to be duplicated):

for /F  "tokens=2"  %%i in ('findstr "password: " "C:Windows\text.txt"') do set var1=%%i

for further info I suggest you to read the help message of the for command with

for /?

(especially the first example => FOR /F "eol=; tokens=2,3* delims=, " %i in ...)

Compo
  • 30,301
  • 4
  • 20
  • 32
sigmud
  • 26
  • 4