Computer Hope


Computer Hope Forum Welcome, Guest. Please login or register.
February 09, 2010, 08:22:25 AM
Home Help Staff Chat Login Register
News: Have your own custom built computer? Come join the self-built computer club.

Computer Hope - Computer Help Forums  >>  Microsoft  >>  Microsoft DOS (Moderator: Computer Hope Admin)  >>  Topic: Display filename and length in batch file 0 Members and 1 Guest are viewing this topic. « previous next »
Pages: [1] - (Bottom) Print
Poll
Question: How to check the length
opt1   -1 (50%)
opt2   -1 (50%)
Total Voters: 1

Author Topic: Display filename and length in batch file  (Read 840 times)
valerie2001
Topic Starter
Newbie
*
Posts: 1

Thanked: 0
OS: Windows XP
Experience: Beginner


« 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%
)
Logged
billrich
Guest
« Reply #1 on: June 10, 2009, 10:06:25 AM »


C:\>cat  filelen.bat
Code:
@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 » Logged
gh0std0g74
Apprentice
*
Posts: 590

Thanked: 37
OS: Linux Variant
Experience: Experienced


« Reply #2 on: June 10, 2009, 10:15:47 AM »

you can use vbscript
Code:
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:
c:\test> cscript /nologo myscript.vbs
output
Code:
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:
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:
c:\test> python myscript.py
« Last Edit: June 10, 2009, 09:27:33 PM by gh0std0g74 » Logged

billrich
Guest
« 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 » Logged
BC_Programmer
Sage
*
Posts: 9923

Thanked: 327
OS: Windows Vista
Computer: Specs
Experience: Experienced




WWW
« Reply #4 on: June 11, 2009, 07:42:57 PM »

carriage return and linefeed,  and the EOF mark, probably.
Logged

There are two ways of constructing a software design: one way is to make it so simple that there are obviously no deficiencies, and the other is to make it so complicated that there are no obvious deficiencies.

—C. A. R. Hoare
gh0std0g74
Apprentice
*
Posts: 590

Thanked: 37
OS: Linux Variant
Experience: Experienced


« 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:
c:\test> od -c file.txt

here's mine
Code:
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"
Logged

billrich
Guest
« Reply #6 on: June 11, 2009, 08:34:14 PM »


C:\>cat   ghost.bat
Code:
@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:\>
Logged
gh0std0g74
Apprentice
*
Posts: 590

Thanked: 37
OS: Linux Variant
Experience: Experienced


« Reply #7 on: June 11, 2009, 08:42:11 PM »

i will show you an example
Code:
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:
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:
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 » Logged

billrich
Guest
« 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
Logged
gh0std0g74
Apprentice
*
Posts: 590

Thanked: 37
OS: Linux Variant
Experience: Experienced


« 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.
Logged

Pages: [1] - (Top) Print 
Computer Hope - Computer Help Forums  >>  Microsoft  >>  Microsoft DOS (Moderator: Computer Hope Admin)  >>  Topic: Display filename and length in batch file « previous next »
Jump to:  


Login with username, password and session length

Old Forum Search | Forum Rules
Copyright 1998-2010 by Computer Hope (tm). All rights reserved
Powered by SMF 1.1.8 | SMF © 2006-2008, Simple Machines LLC
Page created in 2.102 seconds with 22 queries.