![]() |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() |
|
![]() |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() |
![]() |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Once upon a time people played pacman and used MSDOS and command line tools to manage their files. Now we have playstations and xplorer˛, respectively. But not everything can be done within xplorer˛; I've been asked the other day, How can I find files with extremely long filenames? You can't do this in xplorer˛ yet, as there is no column (property) with the length of a filename in characters that can be used with the <Ctrl+F> find command. One far-fetched possibility is to write a column handler that lists the length of filenames, which will be integrated with xplorer˛. A much simpler approach is to hack a visual basic script to read the contents of the folder we want to scan. One year ago I wrote an introduction to WSH that perhaps wasn't focused enough. So today we will write a small script to search for files under a folder. Hack your first windows script File management scripts are similar to the CMD/BAT files of yestercentury, only they resemble more a programming language, as many of you who write scripts for webpages will know. I prefer visual basic scripts (VBS), which are like the macros we write in Excel. Plain text VBS files can be written in notepad or if you want frills like autocompletion you can use a script IDE.
Recurse into subfolders A basic search operation is to enter subfolders. When reading a folder you come across a subfolder, you want to read inside it too. Which may lead you to further subfolders even deeper down and you want to enter these too, all the way down. We will modify our basic script so that it contains a subroutine to scan a folder, which will call itself whenever it encounters a subfolder. That's the well known principle of recursion. Type and save the following listing as SUBDIR.VBS:
If you compare this with the earlier script, you'll see that the former script was turned into a subroutine called LISTDIR, delimited with the visual basic SUB and END SUB statements. We use the CALL statement to execute the subroutine from the main script using the root folder name C:\temp. Our LISTDIR subroutine accepts the name of the folder to scan, a variable called what as before. The real trick is that for subfolders, we call ourselves with the deep folder name: we compose the full path to the subfolder and pass it as an argument to ourselves to show its contents:
File search criteria Our script can already list all the files in our folders and subfolders. Instead of printing the name of all files, we want to search for something in particular, files with long names. Here is the final search script:
This listing is almost identical, except for the contitional IF statement in the file reading loop. We use visual basic's Len function to get the length of the filename and we only print it if it is larger than 100 characters long. So only long filenames will be 'found' when you run this script from a command prompt (cscript SEARCH.VBS). For fun and practice you can extend this searching script to find only files with particular attributes, e.g. whose size is larger than 1MB. Hint: look into the methods of File object to see what you can use (that Size property looks useful :) ps. If you already have microsoft office or developer studio you can use the MSE script editor after some tweaks to edit VBS scripts. This way you get method and property autocompletion for the various filesystem objects above
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
© 2002—2010 Nikos Bozinis, all rights reserved |