1

If I have 35 characters that I need to assign tokens to, I will need to use lower case and upper case. How do I treat the upper case ASCII characters if I already use all of the lower case ones? I've gotten to 26 with the lower case alphabet but when I added the three Upper case ASCII's it outputs A|B|C|...let me explain.

Here is code:

@ECHO OFF
SETLOCAL
SET "sourcedir=C:\Users\aborgetti\Desktop\Pipe Delimiter Project"
SET "destdir=C:\Users\aborgetti\Desktop\Pipe Delimiter Project"

(
 FOR /f "tokens=1-29delims=|" %%a IN ('TYPE "%sourcedir%\test.txt"') DO (
 ECHO(^|%%a^|%%b^|%%c^|%%d^|%%e^|%%f^|%%g^|%%h^|%%i^|%%j^|%%k^|%%l^|%%m^|%%n^|%%o^|%%p^|%%q^|%%r^|%%s^|%%t^|%%u^|%%v^|%%w^|%%x^|%%y^|%%z^|%%A^|%%B^|%%C^|

 )
)>"%destdir%\newfile.txt"

The last three %%A^|%%B^|%%C^| will not convert to the item I am assigning to them. Anyone know why? and what I can do to get more than 26 tokens?

UPDATE

Here is where I'm at I know I have some syntax off, but I think in general it's there. any help?

@ECHO OFF
SETLOCAL
SET "sourcedir=C:\Users\aborgetti\Desktop\Pipe Delimiter Project"
SET "destdir=C:\Users\aborgetti\Desktop\Pipe Delimiter Project"
SET str="1|2|#|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35"
(
 FOR /f "tokens=1-25* delims=|" %%a IN ('TYPE "%sourcedir%\test.txt"') DO (
    FOR /F "TOKENS=1-10 DELIMS=|" %%a in ("%%z") do (
 ECHO(^|%%a^|%%b^|%%c^|%%d^|%%e^|%%f^|%%g^|%%h^|%%i^|%%j^|%%k^|%%l^|%%m^|%%n^|%%o^|%%p^|%%q^|%%r^|%%s^|%%t^|%%u^|%%v^|%%w^|%%x^|%%y^|%%z^|%%A^|%%B^|%%C^|

 )
))>"%destdir%\newfile.txt"
Hituptony
  • 2,740
  • 3
  • 22
  • 44
  • Within your update, your second FOR /F uses `%%a` as the root, but you meant to use `%%A`. It will not work until you fix that. – dbenham Apr 05 '14 at 18:33
  • 2
    See http://stackoverflow.com/a/8520993/1012053 for a compilation of undocumented FOR /F features, including maximum tokens supported, as well which characters can be used as variables, and how. – dbenham Apr 05 '14 at 18:36

3 Answers3

1

Make two stages - 25 tokens + remainder first, then split remainder in second for loop:

@echo off
setlocal 
set str="1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35"

for /f "tokens=1-25* delims=|" %%a in (%str%) do (
  for /f "tokens=1-10 delims=|" %%A in ("%%z") do echo %%a -- %%y -- %%A -- %%J
)
wmz
  • 3,645
  • 1
  • 14
  • 22
  • Can you make this code usable for my specific code? I see what you're saying I think, but it needs to work with existing structure. and echo the same as before. I will accept then. – Hituptony Apr 04 '14 at 17:26
  • @Hituptony It should be straightforward to adapt. I will gladly help if you are unsure how to and/or try and face problems, but I won't write it for you. – wmz Apr 04 '14 at 18:10
  • 1
    @Hituptony Well, you're almost there. You may remove set str - it's there only as a test string; on your output remove %%z as this stores 10 last items (26-35). So you go from %%a to %%y and then %%A to %%J – wmz Apr 04 '14 at 20:22
  • 1
    if you can/want to use powershell, here is ps one liner: `powershell "gc .\test.txt |% {$a=@();$a+=$_.split('|'); for ($i=0;$i -lt 35; $i++) {write-host ($a[$i]+'|') -noNewLine}write-host}"` – wmz Apr 04 '14 at 21:00
  • I tried this one liner, and it got error at char 22...I wish I knew powershell enough to fix it, but do you know what's wrong ? I just copied into a powershell – Hituptony Apr 07 '14 at 14:03
  • An expression was expected afte '(' at line:1 char:22 – Hituptony Apr 07 '14 at 14:06
  • 1
    @Hituptony I included powershell invocation (so as run from `cmd`), if you want to run it from within powershell it will become simply `gc .\test.txt |% {$a=@();$a+=$_.split('|'); for ($i=0;$i -lt 35; $i++) {write-host ($a[$i]+'|') -noNewLine}write-host}` Please also remember it assumes your file to be in current directory and named `test.txt` – wmz Apr 07 '14 at 14:13
  • do you have any resources that you prefer, you seem to have powershell down...I think this is a very useful tool in my current environment. and Thank you again! – Hituptony Apr 07 '14 at 14:14
1

You may change the way to split your values. Instead of using a for /F "tokens=... command, you may use a regular for command that iterates over the values and use they to assemble the output line. However, this works only if the values have certain form. In the example below I assumed that the values have not quotes nor exclamation marks nor carets nor wild-cards (? or *); these points may be fixed, but with more code...

EDIT: I modified the code in order to get a limited number of fields.

@echo off
setlocal EnableDelayedExpansion

REM set "sourcedir=C:\Users\aborgetti\Desktop\Pipe Delimiter Project"
REM set "destdir=C:\Users\aborgetti\Desktop\Pipe Delimiter Project"

set "max=%1"
if not defined max set max=99
for /F "usebackq delims=" %%a in ("test.txt") do (
   set "input=%%a"
   set "output=|"
   set i=0
   for %%b in ("!input:|=" "!") do if !i! lss %max% (
      set "output=!output!%%~b|"
      set /A i+=1
   )
   echo !output!
)

This is test.txt:

1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35
A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z|a|b|c|d|e|f|g|h|i
35|34|33|32|31|30|29|28|27|26|25|24|23|22|21|20|19|18|17|16|15|14|13|12|11|10|9|8|7|6|5|4|3|2|1

Output example:

C:\> test
|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35|
|A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z|a|b|c|d|e|f|g|h|i|
|35|34|33|32|31|30|29|28|27|26|25|24|23|22|21|20|19|18|17|16|15|14|13|12|11|10|9|8|7|6|5|4|3|2|1|

C:\> test 26
|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|
|A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z|
|35|34|33|32|31|30|29|28|27|26|25|24|23|22|21|20|19|18|17|16|15|14|13|12|11|10|

C:\> test 7
|1|2|3|4|5|6|7|
|A|B|C|D|E|F|G|
|35|34|33|32|31|30|29|
Aacini
  • 65,180
  • 12
  • 72
  • 108
0

The maximum number of tokens in a for /F command is 31. If the number of tokens is greater than 31, nothing is processed:

@echo off

for /F "tokens=1-31" %%A in ("A B C D E F G H I J K L M N O P Q R S T U V W X Y Z A0 A1 A2 A3 A4 A5 A6 A7 A8 A9") do (
   echo %%A %%B %%C %%D %%E %%F %%G %%H %%I %%J %%K %%L %%M %%N %%O %%P %%Q %%R %%S %%T %%U %%V %%W %%X %%Y %%Z %%[ %%\ %%] %%^^ %%_ %%` %%a %%b %%c
)
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • interesting, so you're saying that you can only process 31..but then you list out an echo on 34? Is there any way I could get my code to work ? – Hituptony Apr 04 '14 at 17:27