-1

Let me preface, I've only been in software packaging a couple years and that has been the breadth of my cmd experience.

This little bit is almost just an exercise for me so I can engage in a larger project soon after. I can't set an environmental variable to a line of a txt... depicted below. I was hoping to grab the contents of the second line of data from a file retrieved from a wmic query.

enter image description here

enter image description here

for /f "delims=" %a in (file.txt) do set var=%a

Was my reference> Batch command to pull variables from a text file

What small stupid thing am I missing? My hope was %var% to be set to "cmd" if not "cmd "

Community
  • 1
  • 1
mfrancke
  • 1
  • 3
  • and it is set to cmd, and you try to "type" the file "cmd" which doesn't exist in the current folder, and you get a file not found error. Do you mean `echo %var%`? – TessellatingHeckler Nov 20 '15 at 16:08
  • http://i.stack.imgur.com/Yfku2.png still nothing – mfrancke Nov 20 '15 at 16:30
  • Uhh, it works here for me. I can't see or think what the difference is. Do you have another computer you can try the same thing on? – TessellatingHeckler Nov 20 '15 at 16:37
  • That is strange, I've tried it on two win 7 machines. Download link to my file... https://drive.google.com/file/d/0B787Q4uvgMBvMXNFTlFxNTV4TFU/view?usp=sharing – mfrancke Nov 20 '15 at 16:52
  • 1
    This is what it looks like when it works - http://i.imgur.com/Pp51LG2.png - although I am on Win 8.1, this is old enough technology that I don't think that's a likely difference. The fact that I see the `set` lines running, and you don't, suggests that your c:\file.txt is actually empty, and not what notepad shows. Can you run `c:\> type c:\file.txt` and see the contents in the command prompt ? – TessellatingHeckler Nov 20 '15 at 17:10
  • Type c:\file.txt did reflect the contents of the file, maybe its a permissions thing? I'm going to run this home and see if it still causes trouble on my home pc. – mfrancke Nov 20 '15 at 17:46
  • That's weird. I second your question "*What small stupid thing am I missing?*". I doubt it's permissions - writing to the root of c:\ is forbidden by default by UAC, but you've already written to it - and reading from it is allowed, especially if `type` can read it. What else could it be? Might you have a different `for` command somehow overriding the shell? What do you get if you run `c:\> for /?` does it show all the pages of documentation and help? Do you have ... a non-US regional Windows install? Any AV software? Strict business lockdown policies? – TessellatingHeckler Nov 20 '15 at 17:57

1 Answers1

-1

Try

for /f "skip=1delims=" %a in (c:\file.txt) do @if not defined var set "var=%a"

our code should set var to the last line of the file, but you've not posted the significant part of that file, so we can't analyse it.

Since you don't get a report of what var is being set to (as a series of lines) it would appear that your file is empty.

(Please edit your question to provide the additional data. Use te picture icon to insert graphics)

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • But it's not empty - see the filesize as 1Kb, and the comment chain saying they can `type c:\file.txt` and see the content. – TessellatingHeckler Nov 21 '15 at 03:27
  • @TessellatingHeckler: Additional data should be inserted into the question, not inserted in comments. It would have been easier to simply insert the relevant portion of the file into the question body. Stating that it can be `type`d is not useful - we still can't see it. External links are discouraged since they are outside of the control of StackExchange and can be deleted at any time. The object is to produce a database of solutions and missing links defeat that purpose. Please explain your downvote. – Magoo Nov 21 '15 at 03:58
  • I agree additional data should be edited into the question, but it's not my question, I only commented on the question asking for more information and requesting output from some commands, suggesting things to get more information - you're telling the wrong person; and I didn't downvote you. – TessellatingHeckler Nov 21 '15 at 04:08