Count the words in a text file

pignon -  
dubcek Posted messages 18627 Registration date   Status Contributor Last intervention   -
Bonjour,

I would like to count the number of words (or rather strings) contained in a text file.

For example, in toto.txt I have "chaine1 chaine2 chaine3 chaine4". How can I count these 4 strings in DOS? If I am trying to count the number of strings, it is of course because I do not know in advance what will be in toto.txt.

If you want to know why I am looking for this (or find another way to do what I want), it is because I write in toto.txt the name of a file (with its path) but the file name can very well have spaces in it. However, I need to retrieve the entire line. I have managed to retrieve all this for 2 strings (separated by a space), but if the file name contains 2 spaces (or more), it no longer works; I need to recode for 3 spaces... Anyway, since I do not know in advance the name of the file, I absolutely need to count the number of strings on the line.

Thank you very much ;)
Configuration: Windows XP Firefox 3.0.17

5 answers

  1. dubcek Posted messages 18627 Registration date   Status Contributor Last intervention   5 659
     
    hello
    by reading the lines of your file with
    for /f "delims=" %%a in ('type liste.txt') do
    the variable %%a will contain the name with all the spaces
    0
  2. pignon
     
    Hello,

    Thank you for your reply. I get the error
    %%a was unexpected
    when I run this line. Do you know why?

    Thank you
    0
  3. dubcek Posted messages 18627 Registration date   Status Contributor Last intervention   5 659
     
    non, can we see the code
    C:> type f1.txt a a b b b c c c c d d d d d e e e e e e f f f f f f f C:> C:> type aa.bat @echo off for /f "delims=" %%a in ('type f1.txt') do echo %%a C:> aa a a b b b c c c c d d d d d e e e e e e f f f f f f f 
    0
  4. pignon
     
    In fact, if I do this in a .bat (like you did), it works, but if I put this directly in the command prompt, I get the error mentioned above. I don't know why.

    In the bat:

    @echo off

    for /f "delims=" %%a in ('type toto.txt') do echo %%a

    In the command prompt:

    for /f "delims=" %%a in ('type toto.txt') do echo %%a

    And the file toto.txt contains:

    "C:\Documents and Settings\Administrateur\Mes documents\mon fichier.txt"

    Thank you
    0
  5. dubcek Posted messages 18627 Registration date   Status Contributor Last intervention   5 659
     
    dans l'invite de commande, il ne faut utiliser qu'un seul %:
    for /f "delims=" %a in ('type toto.txt') do echo %a
    0