0

I know this question has been asked in all shapes and forms on here, and my syntax seems to be correct, I can't understand where I am going wrong.

When using

FOR /F "tokens=4 delims=." %%G IN ("7.1.2.12") DO ECHO This is G: %%G

in cmd.exe I get an output of 12, as expected.

If I put that number into a file version.txt for example

FOR /F "tokens=4 delims=." %%G IN (version.txt) DO ECHO This is G: %%G

I don't get any output or error, nothing!

Even when explicit about the directory

FOR /F "tokens=4 delims=." %%G IN (C:\Users\Myname\Desktop\version.txt) DO ECHO This is G: %%G

I looked into setlocal EnableDelayedExpansionbut don't think it applies in my situation (I eventually want to SET %%G to a variable.)

Sidenote: I am using the double % because I'm running it from a batch file.

Any ideas? Any more info needed?

  • Errrrrm... I tested that with literally copied code from your question... Works totally fine. Make sure they are exactly same name and in the same directory; add a pause at the end of the batch-file and comment out the probably existing `@echo off` to see every step happening when executed. Using that you hopefully can determine what happened. Even with a leading new line it works perfectly fine... Only idea is that you have probably not saved the file/not wrote the version correctly. This will leed to the echo not beeing executed... – geisterfurz007 Nov 01 '16 at 15:10
  • 4
    check the coding of your textfile. Should be plain text (in notepad "save as" coding "ANSI"). – Stephan Nov 01 '16 at 15:17
  • I've definitely seen for loops get broken by a lack of ANSI before. – SomethingDark Nov 01 '16 at 15:20
  • THANK YOU SO MUCH!! My sanity is restored!. The only issue is the file is created from batch script, how can I get it to default to ANSI as opposed to Unicode? – Marvin Sinclair Nov 01 '16 at 15:26
  • There should be no usual reason why your batch file is outputting unicode. One additional thing to bear in mind is that if there are spaces in the path within the parentheses you'll need to place it in double quotes and use the usebackq option. `"usebackq tokens=4 delims=." %%G IN ("C:\Users\Myname\My Folder\version.txt")` – Compo Nov 01 '16 at 16:36
  • 1
    What command is creating version.txt? – Squashman Nov 01 '16 at 17:24
  • 1
    In case of `wmic` output is redirected into `version.txt` and later processed, look on answers of [How to correct variable overwriting misbehavior when parsing output?](http://stackoverflow.com/questions/24961755/) and [cmd is somehow writing Chinese text as output](http://stackoverflow.com/questions/10210553/). – Mofi Nov 01 '16 at 20:52
  • To convert the Unicode text file to an ANSI text file during reading, you could use the following code: `for /F "tokens=4 delims=." %%G in ('type "version.txt"') do echo This is G: %%G`... – aschipfl Nov 01 '16 at 22:11
  • Thanks guys, in the end I went for
    `WMIC DATAFILE WHERE name="C:\\Program Files (x86)\\Company\\Application.exe" get Version>C:\Users\Myname\Desktop\UNIversion.txt`
    `REM Converting the extracted file from UNICODE to ASCII:`
    `CMD /A /C TYPE UNIversion.txt > ASCIIversion.txt`
    – Marvin Sinclair Nov 02 '16 at 08:05

2 Answers2

0

not sure whether this would be help. But I am having the same issue this afternoon and spent more than 1 hour until i saw this thread. Basically I created that "version.txt" inside MS Powershell by "dir > version.txt", and the For /F didnt work. Until i have delete the version.txt, then re-run the same command in CMD. Then For /F works.

0

Do chcp 65001 (utf-8 at the start of script). and save the file as utf-8.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140