Iterate Files by Tcltk

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.
# eryar@163.com
# 2015-01-18
# package require Tcl
package require Tk wm 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加载脚本文件,如下图所示:

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

Iterate Files by Tcltk的更多相关文章

  1. Mapper XML Files详解

    扫扫关注"茶爸爸"微信公众号 坚持最初的执着,从不曾有半点懈怠,为优秀而努力,为证明自己而活. Mapper XML Files The true power of MyBatis ...

  2. Java NIO Path接口和Files类配合操作文件

    Java NIO Path接口和Files类配合操作文件 @author ixenos Path接口 1.Path表示的是一个目录名序列,其后还可以跟着一个文件名,路径中第一个部件是根部件时就是绝对路 ...

  3. java:Hibernate框架4(延迟加载(lazy),抓取(fetch),一级缓存,get,load,list,iterate,clear,evict,flush,二级缓存,注解,乐观锁和悲观锁,两者的比较)

    1.延时加载和抓取: hibernate.cfg.xml: <?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-co ...

  4. Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define ...

    Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define ... 这个错误是因为有两个相 ...

  5. The type javax.ws.rs.core.MediaType cannot be resolved. It is indirectly referenced from required .class files

    看到了http://stackoverflow.com/questions/5547162/eclipse-error-indirectly-referenced-from-required-clas ...

  6. 未能写入输出文件“c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\106f9ae8\cc0e1

    在本地开发环境没问题,但是发布到服务器出现:未能写入输出文件"c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.Ne ...

  7. iBatis.net 循环iterate,没有foreach

    3.9.4. Iterate Element This tag will iterate over a collection and repeat the body content for each ...

  8. Find and delete duplicate files

    作用:查找指定目录(一个或多个)及子目录下的所有重复文件,分组列出,并可手动选择或自动随机删除多余重复文件,每组重复文件仅保留一份.(支持文件名有空格,例如:"file  name" ...

  9. Android Duplicate files copied in APK

    今天调试 Android 应用遇到这么个问题: Duplicate files copied in APK META-INF/DEPENDENCIES File 1: httpmime-4.3.2.j ...

随机推荐

  1. Spark Application的调度算法

    要想明白spark application调度机制,需要回答一下几个问题: 1.谁来调度? 2.为谁调度? 3.调度什么? 3.何时调度? 4.调度算法 前四个问题可以用如下一句话里来回答:每当集群资 ...

  2. 手把手教你搭建深度学习平台——避坑安装theano+CUDA

    python有多混乱我就不多说了.这个混论不仅是指整个python市场混乱,更混乱的还有python的各种附加依赖包.为了一劳永逸解决python的各种依赖包对深度学习造成的影响,本文中采用pytho ...

  3. Linux文件系统

    今天学习了Linux文件系统,现在来做个小总结. 首先Linux中一切都是文件,下面这个清单是Linux系统的顶层目录结构. 清单 1. Linux 系统的顶层目录结构 / 根目录 ├── bin 存 ...

  4. elasticsearch一些常用的配置

    配置了解 cluster #配置下集群的名称 cluster.name: my-application

  5. 简单说说.Net中的弱引用

    弱引用是什么? 要搞清楚什么是弱引用,我们需要先知道强引用是什么.强引用并不是什么深奥的概念,其实我们平时所使用的.Net引用就是强引用.例如: Cat kitty = new Cat(); 变量ki ...

  6. 用c#创建支持多语言的WinForm应用程序

    实现多语言的方法可能有使用资源文件,或者配置xml两种方法吧.没时间研究过多,学习了一下使用资源文件的方法,成功了. 在.net2.0 中,m$ 为我们提供了一种简单方便的方法, 使用资源文件 1.新 ...

  7. Oracle EBS - Profile Setting

    EBS Profile Setting (Personalization Basics): Personalization Basics For R12 Forms Enable personaliz ...

  8. IOAPIC重定位中断处理函数思路整理

    因为小可并非硬件编程出身,汇编基础又比较差...所以刚开始理解利用IOAPIC重定位技术的时候相当困难. 何为IOAPIC? 首先,必须认识到它是一个硬件,可编程的硬件.我理解的它在整个流程中的作用如 ...

  9. Cocos2d-x游戏引擎实战开发炸弹超人项目教程 全套下载 1至6课

    下载地址: http://pan.baidu.com/s/1b19HN

  10. Redis分布式锁服务(八)

    阅读目录: 概述 分布式锁 多实例分布式锁 总结 概述 在多线程环境下,通常会使用锁来保证有且只有一个线程来操作共享资源.比如: object obj = new object(); lock (ob ...