Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Poll

How to check the length

opt1
1 (50%)
opt2
1 (50%)

Total Members Voted: 1

Author Topic: Display filename and length in batch file  (Read 16936 times)

0 Members and 1 Guest are viewing this topic.

valerie2001

  • Guest
Display filename and length in batch file
« on: June 10, 2009, 09:10:37 AM »
The script will dispaly the filename. I need to display the length as well eg. filename is abc.txt, the length is 6
the script as below:

@echo off
for %%a in (C:\*.*txt) do (
set fname=%%a
echo %fname%
)

billrich

  • Guest
Re: Display filename and length in batch file
« Reply #1 on: June 10, 2009, 10:06:25 AM »

C:\>cat  filelen.bat
Code: [Select]
@echo off
for /f  "delims==" %%i in ('dir /b *.txt')  do (
echo %%i
echo %%i  | wc -c
)


C:\>filelen.bat
Output:

Quote
a-hire.txt
      13
bat.txt
      10
caavsetupLog.txt
      19
caisslog.txt
      15
myFILE.txt
      13
mymoney.txt
      14
ReadMe.txt
      13
savenow.txt
      14
savestr.txt
      14
teststr.txt
      14
tokens.txt
      13
unix.txt
      11
ver.txt
      10
x.txt
       8
xvar.txt
      11
xx.txt
       9
xxx.txt
      10
zzz.txt
      10
C:\>
« Last Edit: June 11, 2009, 09:23:10 PM by billrich »

gh0std0g74



    Apprentice

    Thanked: 37
    Re: Display filename and length in batch file
    « Reply #2 on: June 10, 2009, 10:15:47 AM »
    you can use vbscript
    Code: [Select]
    Set objFS = CreateObject("Scripting.FileSystemObject")
    strFolder = "c:\test"
    Set objFolder = objFS.GetFolder(strFolder)
    For Each strFile In objFolder.Files
    WScript.Echo strFile.Name,Len(strFile.Name)
    Next
    save the above as myscript.vbs and on command prompt
    Code: [Select]
    c:\test> cscript /nologo myscript.vbs
    output
    Code: [Select]
    C:\test>cscript /nologo test.vbs
    11 MickeyMouse Road.doc 23
    11 MickeyMouse Road.pdf 23
    a-hire-1-0601-txt 17
    a-hire-2-0601-txt 17
    a1.csv 6
    a2.csv 6

    alternative solution, in Python (for Windows)
    Code: [Select]
    import os
    path=os.path.join("c:\\","test")
    os.chdir(path)
    for files in os.listdir("."):
        print files,len(files)
    save the code as myscript.py and on command prompt
    Code: [Select]
    c:\test> python myscript.py
    « Last Edit: June 10, 2009, 09:27:33 PM by gh0std0g74 »

    billrich

    • Guest
    Re: Display filename and length in batch file
    « Reply #3 on: June 11, 2009, 07:40:55 PM »
    Ghost,

    Why does wc -c  show 9 characters when there are only six?

    C:\>cat  unix.txt
    666666

    C:\>cat  unix.txt  | wc -c
           9

    C:\>
    « Last Edit: June 11, 2009, 07:59:34 PM by billrich »

    BC_Programmer


      Mastermind
    • Typing is no substitute for thinking.
    • Thanked: 1140
      • Yes
      • Yes
      • BC-Programming.com
    • Certifications: List
    • Computer: Specs
    • Experience: Beginner
    • OS: Windows 11
    Re: Display filename and length in batch file
    « Reply #4 on: June 11, 2009, 07:42:57 PM »
    carriage return and linefeed,  and the EOF mark, probably.
    I was trying to dereference Null Pointers before it was cool.

    gh0std0g74



      Apprentice

      Thanked: 37
      Re: Display filename and length in batch file
      « Reply #5 on: June 11, 2009, 08:16:56 PM »
      Ghost,

      Why does wc -c  show 9 characters when there are only six?

      C:\>cat  unix.txt
      666666

      C:\>cat  unix.txt  | wc -c
             9

      C:\>
      there are some unknown characters i believe. please check your unix.txt again. Most probably there are \r\n or somethign . you can use od to check what's inside your file
      Code: [Select]
      c:\test> od -c file.txt

      here's mine
      Code: [Select]
      C:\test>more file.txt
      666666

      C:\test>cat file.txt | wc -c  <-------------------- No need to use cat .. see wc example below
      6

      C:\test>wc -c < file.txt
      6
      there are no other characters after the last "6"

      billrich

      • Guest
      Re: Display filename and length in batch file
      « Reply #6 on: June 11, 2009, 08:34:14 PM »

      C:\>cat   ghost.bat
      Code: [Select]
      @echo off
      echo 11 MickeyMouse Road.doc | wc -c
      echo 11 MickeyMouse Road.pdf | Wc -c
      echo a-hire-1-0601-tx  | wc -c
      echo a-hire-2-0601-txt | wc -c
      echo a1.csv | wc -c
      echo wcup a2.csv | wc -l

      Output:

      C:\>ghost.bat
           
      Quote
      25
            25
            19
            19
             8
           C:\>

      gh0std0g74



        Apprentice

        Thanked: 37
        Re: Display filename and length in batch file
        « Reply #7 on: June 11, 2009, 08:42:11 PM »
        i will show you an example
        Code: [Select]
        C:\test>echo 11 MickeyMouse Road.doc | wc -c
        26

        C:\test>echo 11 MickeyMouse Road.doc | od -c
        0000000   1   1       M   i   c   k   e   y   M   o   u   s   e       R
        0000020   o   a   d   .   d   o   c      \r  \n
        0000032


        after ".doc" there is a space. is counted as 1. also , the echo command gives you \r\n at the back. that's why whenever you use echo like this
        Code: [Select]
        C:\test>echo test
        test
                  <---- extra blank
        C:\test>

        if you add them up, its 26 characters. cmd.exe's echo just treat everything after as 1 character, including quotes like "

        Since you already have GNU tools,  you can use printf instead of cmd.exe's echo..
        Code: [Select]
        C:\test>printf "11 MickeyMouse Road.doc" | wc -c
        23
        this is correct one.
        « Last Edit: June 11, 2009, 08:57:53 PM by gh0std0g74 »

        billrich

        • Guest
        Re: Display filename and length in batch file
        « Reply #8 on: June 11, 2009, 09:12:34 PM »
        Ghost,

        Hey,  you are good.  Do you work for Microsoft or AT&T?  I like these Unix solutions better than than the VBS solution.  I hope the lady who started this thread is still reading.  Thanks for the effort over and above . . .

        Quote

        C:\test>more file.txt
        666666

        C:\test>cat file.txt | wc -c  <-------------------- No need to use cat .. see wc example below
        6

        C:\test>wc -c < file.txt
        6

        gh0std0g74



          Apprentice

          Thanked: 37
          Re: Display filename and length in batch file
          « Reply #9 on: June 11, 2009, 09:23:20 PM »
          Ghost,
          Hey,  you are good.  Do you work for Microsoft or AT&T? 
          no. i am "self employed" and a hobbyist now. previous job as win/*nix administrator.
          Quote
          I like these Unix solutions better than than the VBS solution.  I hope the lady who started this thread is still reading.  Thanks for the effort over and above . . .
          no problem. Its good that most of these *nix tools are ported to windows to complement what batch lacks.