Computer Hope
Microsoft => Microsoft DOS => Topic started by: valerie2001 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%
)
-
C:\>cat filelen.bat
@echo off
for /f "delims==" %%i in ('dir /b *.txt') do (
echo %%i
echo %%i | wc -c
)
C:\>filelen.bat
Output:
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:\>
-
you can use vbscript
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
c:\test> cscript /nologo myscript.vbs
output
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)
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
c:\test> python myscript.py
-
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:\>
-
carriage return and linefeed, and the EOF mark, probably.
-
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
c:\test> od -c file.txt
here's mine
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"
-
C:\>cat ghost.bat
@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
25
25
19
19
8
C:\>
-
i will show you an example
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
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..
C:\test>printf "11 MickeyMouse Road.doc" | wc -c
23
this is correct one.
-
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 . . .
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
-
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.
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.