Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))

Well House Consultants
You are on the site of Well House Consultants who provide Open Source Training Courses and business hotel accommodation. You are welcome to browse and use our resources subject to our copyright statement and to add in links from your pages to ours.
Other subject areas - resources
Java Resources
Well House Manor Resources
Perl Resources
Python Resources
PHP Resources
Object Orientation and General topics
MySQL Resources
Linux / LAMP / Tomcat Resources
Well House Consultants Resources
Extras Resources
C and C++ Resources
Ruby Resources
Tcl/Tk Resources
Web and Intranet Resources
Tcl/Tk module T209
File and Directory Handling
Exercises, examples and other material relating to training module T209. This topic is presented on public courses Learning to program in Tcl, Tcl Programming, Tcl Programming

This module covers opening, reading and writing files in Tcl. You'll also learn how to check the contents of directories, test for the existence of individual files, and look up other information ranging from file size to file update time.


Articles and tips on this subjectupdated
4524Tcl - a new example for data reformatting
Tcl is an excellent language for combing through and reformatting data - though rarely a first choice language for such tasks if users aren't doing other things with it too. Towards the end of last week's Learning to program in TCL course, a "side task" requirement came up to reformat our course diary ...
2015-10-10
 
4523Catching failed commands and not crashing the program in Tcl
Where a command may fail in Tcl (causing Tcl to crash), you can defer it within a catch command. That will return a true value if it fails, and an optional extra parameter gives you the associated error message. A program to look for the biggest file in or below a directory [here] fails if you run ...
2015-10-10
 
4461Reading from a URL, and reading Json, from your Tcl script
You can read from a URL in Tcl using the http package ... for example   package require http   set handle [::http::geturl http://www.wellho.co.uk/robots.txt] And you can then collect the data:   set body [::http::data $handle] and the http status code:   set ...
2015-03-15
 
3617The fileutil package and a list of file system commands in Tcl
On top of the file commands, the ::fileutil package which is shipped with Tcl includes a range of commonly used structures ("design patterns") which means you don't have to keep wring your own composites. Here is a combined list, sorting them out by category rather than by which command or package they ...
2012-02-18
 
3429Searching through all the files in or below a directory - Ruby, Tcl, Perl
Many of our customers want to learn how to traverse all the files in a directory, or perhaps even all the files in or below a directory. I quite often write a demonstration program during our courses which looks though part of a file system tree for files over a certain size, or for the largest (so ...
2011-09-09
 
3320Reading the nth line from a file (Perl and Tcl examples)
"How do I find the 100th line in a file" - a common question for newcomers to coding. The short answer is to open the file, and loop through to read lines until the one that you want. Although most languages have a seek command or function, that works by bytes and with a typical text / ascii file, ...
2011-06-09
 
3192Tcl - Some example of HOW TO in handling data files and formats
During the Tcl course I was running earlier this week, we got involved in a number of interesting topics such as • How to clean up input lines into lists of fields (use split) • How to reformat awkward fields (use regexp and regsub) • How to combine files that vary in format from year ...
2011-03-04
 
1407Reading from another process in Tcl (pipes and sockets)
If you want to read from another process on the same system that you're running your code on in Tcl can be done using a pipe - you can use open with the command name instead of the file name, preceeded by a pipe character (|) - thus: set said [open |df r]   while {[gets $said this] >= 0} {   puts ...
2011-03-01
 
2467Tcl - catching an error before your program crashes
There are times when a Tcl command can fail because of the data being passed in to it ... and when it fails, it can do so with a spectacular crash! For example, the glob command which matches files to a pattern (Tcl's ls or dir if you like to think of it that way) can go 'belly up' if there are no files ...
2009-10-22
 
1467stdout v stderr (Tcl, Perl, Shell)
When you're programming, you shouldn't write code to read directly from the keyboard and write to the screen .... what if you want to have your program read from or write to a file sometimes? Instead, you should write code to read from stdin (Tcl) or STDIN (Perl) and write to stdout (Tcl) or STDOUT ...
2007-12-12
 
1426Buffering up in Tcl - the empty coke can comparison
A comment on buffering in Tcl .... if {1 == 0} { When you finish drinking a can of Coke, you don't call your local recycling plant up straight away and have them send a truck around to collect the empty - that would be inefficient to put it mildly. And in the same way, a computer doesn't always save ...
2007-11-10
 
779The fragility of pancakes - and better structures
Have you ever hunted around a directory for a file ... you KNOW it's there, but you can't find it - can't see the wood for the trees, if you like, in a listing that spills over many pages? I call such directory structures "pancakes" as they're thin and flat, and they tend not to be very easy to work ...
2006-06-30
 
785Running external processes in Tcl and Tcl/Tk
If you want to run external processes from a Tcl based program (Tcl, Tcl/Tk, expect), there are various ways of doing it. Firstly, the whole purpose of the Expect extension is to allow you to control other processes via its three major commands of spawn which starts another process, send which sends ...
2006-06-30
 
Examples from our training material
bigbelow   Find biggest file in or below a specified folder
bigbelow.v2   biggest below - practical example that traps empty folders
biggest   using glob to check all files in a directory
diary   diary data for for_qa script
disc_status   Piping input from an operating system command
dnstab   Read a file, extract from it
failer   Using catch to trap file open failures
files   fileutil package in use
filework   Report file statuses
for_qa.tcl   Data reformatting example / course end tie together
multifile   Output to stdout and several files at the same time
rail   Data extraction and reporting from multiple files
recent   List all files modified in the last hour
Pictures
Tcl is a great Engineer's language
Background information
Some modules are available for download as a sample of our material or under an Open Training Notes License for free download from [here].
Topics covered in this module
Reading and writing files.
Opening a file - the basics.
Reading from a file.
Writing to a file.
Closing a file.
More about reading and writing files.
Other open access arguments.
An alternative to gets.
Other Input/Output commands.
Example.
File and directory handling.
The file command.
Looping through all the files in a directory.
Complete learning
If you are looking for a complete course and not just a information on a single subject, visit our Listing and schedule page.

Well House Consultants specialise in training courses in Tcl/Tk, Expect,Ruby, Lua, Python, Perl, PHP, and MySQL. We run Private Courses throughout the UK (and beyond for longer courses), and Public Courses at our training centre in Melksham, Wiltshire, England. It's surprisingly cost effective to come on our public courses - even if you live in a different country or continent to us.

We have a technical library of over 700 books on the subjects on which we teach. These books are available for reference at our training centre.


You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/resources/T209.html • PAGE BUILT: Sun Oct 11 14:50:09 2020 • BUILD SYSTEM: JelliaJamb