Batch Script will not progress into FOR loop.

Julie Kurpa
Julie Kurpa used Ask the Experts™
on
For some reason, I can't get my Windows batch script to read a file.   The file exists...I've checked a million times. The path is correct...I've checked a million times.  

Yet it does not enter the "for" loop.  If I put double-quotes around the %DataElements%, it goes into the For loop just fine and prints the value of %DataElements%.  I paste that value in the CMD window and up pops the file.  It doesn't make sense.  

@echo off
setlocal enabledelayedexpansion
set DataElements=E:\myDir\Data_Elements.txt
for /f "tokens=*" %%a in (%DataElements%) do (
      set line=%%a
      echo line is !line!
)

What the heck?!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
Try it like this:
@echo off
setlocal enabledelayedexpansion
set DataElements=E:\myDir\Data_Elements.txt
for /f "tokens=*" %%a in ('type "%DataElements%"') do (
	set line=%%a
	echo line is !line!
)

Open in new window

NVITEnd-user support

Commented:
Your code works fine for me.
Julie KurpaSr. Systems Programmer

Author

Commented:
oBdA = why oh why is your code working and not mine?!  I am currently using the exact same thing in other batch scripts that run perfectly.

and obviously, my code worked for NVIT.

Please explain or I shall need to go on meds.....
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

NVITEnd-user support

Commented:
> ...my code worked for NVIT.

One cause: SPACE characters in your path. Without those chars, it works fine for me. Else, I get: The system cannot find the file.
In general, just do it like oBdA said, using the TYPE command.
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
No need for drugs. If you open the input file in Notepad, go to "Save as", and check the "Encoding" box, I bet that it will show "Unicode".
"for /f" can't read Unicode directly, but "type" can, and from then on, it's plain standard input.
Julie KurpaSr. Systems Programmer

Author

Commented:
OBdA -  You are correct.  It did indeed say "Unicode".   This file was created by Powershell further up in the script.  Is there an option to not let it use "Unicode" when it creates a file?

The powershell scripts redirect things using the arrow.

doing.stuff  > $outputPath
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Check if whatever cmdlet generates the output supports the -Encoding argument.
But don't bother. Scripts should work independent of the input encoding. Just use it as posted; that not only solves the Unicode problem, but the problem with spaces in the path as well. You could cure that using the "usebackq" options and enclosing the path in backquotes, but why go through all that trouble when "type" will solve both issues at once?
Julie KurpaSr. Systems Programmer

Author

Commented:
Thanks OBdA.   I will make a point of using that new way of doing it (well, new to me).    

I don't see that I was using spaces in the path though.  Not sure why that's being said.  :(
NVITEnd-user support

Commented:
> I don't see that I was using spaces in the path though.  Not sure why that's being said.  :(

I said it because you didn't mention it and I presumed your example path E:\myDir could have SPACEs. If it did, it would cause an error. Just trying to be thorough..
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
I mentioned that for the sake of completeness, because that is another possible issue with "for /f" that you need to consider in general. Enclosing the path in double quotes will not work, because then "for /f" will parse it as a literal string, instead of getting the content of the file.
Julie KurpaSr. Systems Programmer

Author

Commented:
Ah I understand.  I kept looking to see if I had spaces in the filename.  

Much appreciation everyone!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial