首页 技术 正文
技术 2022年11月9日
0 收藏 418 点赞 4,726 浏览 2459 个字

Iterate Files by Tcltk

https://www.shuzhiduo.com/A/xl56RMx5rZ/eryar@163.com

Abstract. Tcl/Tk provide a programming system for developing and using graphical user interface(GUI) applications. Tcl stands for “tool command language” and is pronounced “tickle”, is a simple scripting language for controlling the extending applications. The blog use Tcl/Tk to iterate all the files for a given directory, this is useful to some automation work, such as change all the file names for a given directory; add copyright info for the source code files.

Key Words. Tcl/Tk, Iterate Files,遍历文件夹中所有文件

1. Introduction

Tcl/Tk是一种用于易于使用的脚本语言,可以用来对程序进行扩展及完成一些自动化的工作,加上内置的一些命令,其功能要比Windows中的DOS的批处理命令功能更强大,使用更方便。Tcl脚本语言是开源免费的,可以方便获取且免费使用。

OpenCASCADE 中使用了Tcl/Tk来实现了一个自动化测试体系。使用在OpenCASCADE中使用自定义的Tcl命令,可以快速来检验算法的结果。通过编写脚本文 件,实现了测试的自动化。所以学习一下Tcl/Tk脚本语言,并在实际的工作中加以应用,可以将一些机械的劳动交给计算机自动完成。

本文主要说明如何使用Tcl/Tk来遍历指定文件夹中所有文件。利用此功能,可以稍微加以扩展,就可以完成一些实际的重复劳动。如遍历指定目录中所有的源文件或指定类型的文件,添加上版权信息等。

2. Tcl/Tk Code

要遍历指定目录下所有的文件,包括子文件夹,需要用到命令glob及一个递归函数。脚本代码如下所示:

#
# Tcl/Tk script to iterate all the files for a given directory.
# https://www.shuzhiduo.com/A/xl56RMx5rZ/eryar@163.com
# 2015-01-18
#package require Tcl
package require Tkwm title . "Iterate Files"label .labelDirectory -text "Directory "
entry .entryDirectory -width -relief sunken -textvariable aDirectory
button .buttonDirectory -text "..." -command {chooseDirectory .entryDirectory}button .buttonApply -text "Apply" -command {perform $aDirectory}
button .buttonCancel -text "Cancel" -command {exit}grid .labelDirectory .entryDirectory .buttonDirectory
grid .buttonApply .buttonCancel# chooseDirectory--
# choose the directory to iterate.
#
proc chooseDirectory {theEntry} {
set dir [tk_chooseDirectory -initialdir [pwd] -mustexist ] if {[string compare $dir ""]} {
$theEntry delete end
$theEntry insert $dir
$theEntry xview end
}
}# perform--
# perform the algorithm.
#
proc perform {theDirectory} {
puts "Iterate all the files in $theDirectory" if {[string length $theDirectory] < } {
tk_messageBox -type ok -icon warning -message "Please select the directory!" -parent .
return
} # process the iterate...
process $theDirectory
}# process--
# recursion every folder and file.
#
proc process {theFolder} { set aFiles [glob -nocomplain -directory $theFolder *] foreach aFile $aFiles {
if {[file isfile $aFile]} {
# just output the file name here.
# you can do something such as rename for the file.
puts "$aFile \n"
} else {
process $aFile
}
}
}

程序用法为打开Tcl解释器,使用命令source加载脚本文件,如下图所示:

Iterate Files by Tcltk

Figure 2.1 Tcl usage

3. Conclusion

通过应用Tcl/Tk来体验脚本编程的乐趣,并加深对Tcl/Tk的理解。从而对OpenCASCADE的模块Draw Test Harness更好地理解。

如果有编程基础,Tcl/Tk会很快入门的。入门后,可以应用其直接编写一些有意思有脚本,来实现一些重复工作的自动化。也可将Tcl加入到自己的程序中,增加程序的二次开发功能。

可见,玩一玩脚本语言,还是非常有趣的!

PDF Version and Script: Iterate Files by Tcl

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,488
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,487
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,127
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289