Question

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"
Was it helpful?

Solution

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
)

OTHER TIPS

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|

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
)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top