0

Is there a way to set each line of a txt document as a different variable in a batch file? I have got a txt file with all drives found on the computer. How can I set each line (for each drive) as a different variable, if this is possible at all?

I have a file which writes all avalible drives in a text file. I want to read this file out and set a different variable for each line of text. For example:

    DeviceID  
    C:        
    D:        
    E:        

Now "DeviceID" would be the first variable. "C:" "D:" and "E:" are all getting individual variables. I hope you get what I mean.

Thanks in advance!

Okay so I've tried this:

@echo off
setlocal enabledelayedexpansion
set i=0
for /f "tokens=2 delims=:=" %%a in ('wmic logicaldisk get name /value ') do (
  set /a i+=1
  set drive[!i!]=%%a
)
set drive
pause
copy %0 %drive[1]%:\%random%.bat
pause

And it works pretty well. But I want to copy the currently executed file to all drives found without typing:

copy %0 %drive[1]%:\currentlyexecutedfile.bat
copy %0 %drive[2]%:\currentlyexecutedfile.bat
copy %0 %drive[3]%:\currentlyexecutedfile.bat
copy %0 %drive[4]%:\currentlyexecutedfile.bat

Is there a way to do this, without knowing how many drives there are and without causing errors?

Donald Trump
  • 83
  • 1
  • 6
  • Didn't understand your question, do you mean use `set c= C` etc.?: – Mr.Helpy Apr 01 '16 at 13:17
  • Possible duplicate of [Batch: Parse TXT Lines into Array](http://stackoverflow.com/questions/30403022/batch-parse-txt-lines-into-array) – aschipfl Apr 01 '16 at 13:47
  • 1
    What have you tried? Where are you getting stuck? I'm also struggling to understand, why rely on a txt file to find the drives on a computer? Why not use a `for /f` loop to capture the desired output from `wmic logicaldisk...` and kill 2 birds with one stone? – rojo Apr 01 '16 at 13:53
  • I am pretty new to programming, how would you solve the problem with a for /f loop? I'd try that for sure. – Donald Trump Apr 01 '16 at 14:04

1 Answers1

2

Is there a way to do this, without knowing how many drives there are and without causing errors ?

You should do it like this :

@echo off
setlocal enabledelayedexpansion
set i=0
for /f "tokens=2 delims=:=" %%a in ('wmic logicaldisk get name /value ') do (
  set /a i+=1
  set drive[!i!]=%%a
)
Rem set drive
Rem If you don't know how many elements the array have (that seems is the case), you may use this method:
for /F "tokens=2 delims==" %%s in ('set drive') do (echo Copy %0 "%%s:\currentlyexecutedfile.bat")
pause
Hackoo
  • 18,337
  • 3
  • 40
  • 70
  • This works for me. Is there a way to copy the just executed file to all found drives without knowing how many drives were found? – Donald Trump Apr 01 '16 at 14:26
  • @DonaldTrump Can you please edit your question and post the code you have tried until now ! and give more information for your aim ! – Hackoo Apr 01 '16 at 14:29
  • @DonaldTrump Check my last edit ,if it is ok then you can get rid of echo before Copy ! – Hackoo Apr 01 '16 at 15:01
  • 1
    @Hackoo instead of bothering to `set /a i += 1` and `set drive...` you could just `if not exist "%%a:\%~nx0" copy "%~f0" %%a:\ ` – rojo Apr 01 '16 at 15:36