1

I have a file (file.txt) containing just one line (I have concatenated all the lines in one). So I have the following line:

85;048;048;057;048;055; 045 ;84;048;048;057;055;055

And I would like that file.txt containing the ASCII conversion of each number delimited by

;

So the file.txt should then contains:

U00907 - T00977

I have tried with a loop but it really does not work:

set content=
for /f "delims=;" %%i in (file.txt) do (
  cmd /c exit %%i
  Set "char=!=ExitCodeAscii!"
  set content=%content% %char%
)

Any ideas?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • Based on your intended txt file output, `U00907 - T00977`, you may need to add additional logic to prevent a `U00907-T00977` result. The sequence has completely ignored your spaces which should have been included in your line. _Your prepended 0's make no difference_ so your line of text should be similar to this: `85;048;048;057;048;055;32;045;32;84;048;048;057;055;055` – Compo Jun 28 '17 at 17:11
  • 1
    @compo : Observation1: There are two space characters either side of the `045` in the example data - perhaps these are intended literally (whether this is unique to spaces - my crystal ball is a little foggy... Observation2: There is a data element `048`. Need I remind you of the *octal * effect? – Magoo Jun 28 '17 at 17:36
  • @Magoo, in both the original and edited versions of the question body there appears to be only a single space either side of `045`. My comment wasn't an attempted answer, just a source data correction. – Compo Jun 28 '17 at 17:58
  • @compo: Yes - there are two spaces - one space each side of the `045`. I was probably - well, no "probable" about it - ambiguous there. Interestingly, the `048` is **not** interpreted as octal in an `exit` statement, in fact, the value assigned to *errorlevel* appears decimal for all cases - regardless of the leading `0`. Another quirk for the books. – Magoo Jun 28 '17 at 18:09

1 Answers1

1

some minor logic failures: with delims=; you take only the first token 85 and ignore the rest. You need one loop to get each line of file.txt and another loop to process the tokens in each line (a simple for without /f). And you need delayed expansion.

@echo off
setlocal enabledelayedexpansion
for /f "delims=" %%i in (file.txt) do (
  set content=
  for %%a in (%%i) do (
    cmd /c exit %%a
    Set "char=!=ExitCodeAscii!"
    set content=!content!!char!
  )
  echo !content:-= - !
)

If you are sure, file.txt contains only one line (or you don't care about the rest), you can replace the first for /f loop with <file.txt set /p string= and process that with
for %%a in (%string%) do (

Stephan
  • 53,940
  • 10
  • 58
  • 91
  • Please see my remarks to Compo on the initial text of the question. I may alter your approach a little. – Magoo Jun 28 '17 at 17:38
  • Wow, I am impressed. Very quick response in a few hours (minutes). Seems to be the good answer Stephan. I need to test a bit. Thank you very much to Magoo and Compo too.. You save me a lot of time! – Mojomuseflo Jun 28 '17 at 20:51