Saved Bookmarks
| 1. |
Solve : Display filename and length in batch file? |
|
Answer» The script will dispaly the filename. I need to display the length as well eg. filename is abc.txt, the length is 6 a-hire.txtyou 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 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.Quote from: billrich on June 11, 2009, 07:40:55 PM Ghost,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" 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 25C:\>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.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 Quote from: billrich on June 11, 2009, 09:12:34 PM Ghost,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. |
|