0

I've tried so sort this for the better part of the morning.

It is actually the same question as this one from 2013, to which no one replied:

batch script with FOR does not work

I'll do my best to format the code so that it is easy to follow and maybe I'll get an answer...

I am doing an archive project from our help desk ticket system. For sorting purposes, the files will have the ticket number and the date.

However, the ticket number varies in length. To fix this, all ticket numbers are to be 6 digits, with the shorter numbers padded with preceding zeroes (i.e. 1234 becomes 001234).

I can get the ticket number, but I need to find its length to know how many zeroes to add.

This works:

echo off
set my_str=12345 
set length=0

:Loop
  if defined my_str (
  set my_str=%my_str:~1%
  set /A "length+=1"
  goto Loop)

echo the string is %length% characters long

However, I get a bunch of ticket numbers in a list. So, I read through this:

set statements don't appear to work in my batch file

And got lost reading this:

http://www.computing.net/howtos/show/batch-script-variable-expansion-win200-and-up/248.html

And I tried this:

setlocal enabledelayedexpansion
echo off

for /f %%a IN (test.txt) do (
   set my_str=%%a
   set length=0

:Loop
if defined my_str (
    set my_str=!my_str:~1!
    set /A "length+=1"
    echo %length%
  goto Loop)

echo the string is %length% characters long
)

But it only reads the FIRST line of test.txt

Why does the FOR /F loop fail?

How do I get it to work?

Community
  • 1
  • 1
jmsallo
  • 21
  • 2

3 Answers3

4
  1. You do not need to know the length of the ticket number string (when you can assure it won't be longer than 6 characters), you can prefix 5 zeros and split off the last 6 characters then:

    set my_str=12345
    
    set my_str=00000%my_str%
    set my_str=%my_str:~-6%
    
    echo %my_str%
    

    If you still want to get the string length, consult this post.

  2. You cannot use goto within the for body as goto breaks the for loop context. However, you can call a sub-routine within for, which in turn may contain goto.

Community
  • 1
  • 1
aschipfl
  • 33,626
  • 12
  • 54
  • 99
0

Your batch script does exactly what you are describing: reading the first line of a number of files (in your case, only the one file) and determining the length of the first line. I.e. you got the inner part of your for loop wrong.

I don't know the correct solution either but I think that this page may be of some help to you:

http://ss64.com/nt/for_f.html

I hope that helps.

  • Yes, I'm aware of this on-line reference and use it frequently. What do you mean "you got the inner part of your loop wrong"? TEST.TXT has 10 lines each with a string, FOR /F should read each line and I should get 10 lines saying "the string is %length% characters long" – jmsallo Aug 20 '15 at 20:16
0

This seems like a XY problem. Instead of worrying about calculating the length of a string, just use the built-in substring methods to create a padded string.

@echo off
setlocal enabledelayedexpansion
for /f %%a IN (test.txt) do (
   set my_str=000000%%a
   @echo !my_str:~-6!
)

The end result echo'ed will be a six-digit number left-padded with zeroes.

ig0774
  • 39,669
  • 3
  • 55
  • 57
  • You are correct re: XY. Fortunately, I did describe what I needed in enough detail that you gave me a solution. Thanks! – jmsallo Aug 20 '15 at 20:35