Skip to content

Instantly share code, notes, and snippets.

@YujiSODE
Created March 3, 2023 14:57
Show Gist options
  • Save YujiSODE/ee070936443f2855fc45cb1471bd3e8b to your computer and use it in GitHub Desktop.
Save YujiSODE/ee070936443f2855fc45cb1471bd3e8b to your computer and use it in GitHub Desktop.
It generates a CSV file based on shallow structure in the target directory.
#dirCSV
#dirCSV.tcl
##===================================================================
# Copyright (c) 2023 Yuji SODE <yuji.sode@gmail.com>
#
# This software is released under the MIT License.
# See LICENSE or http://opensource.org/licenses/mit-license.php
##===================================================================
#It generates a CSV file based on shallow structure in the target directory.
#
#=== Synopsis ===
#
# ** Shell **
# - `tclsh dirCSV.tcl path;`
#
# ** Tcl **
# - `::dirCSV::read path;`
#
# Argument
# - `$path`: path for the target directory
##===================================================================
set auto_noexec 1;
package require Tcl 8.6;
# ===============
#
#** Tcl **
# === namespace: dirCSV ===
namespace eval ::dirCSV {};
#
#It generates a CSV file based on contents in the target directory.
proc ::dirCSV::read {path} {
# - $path: path for the target directory
# ===============
#
#path for the current working directory
set dir0 [pwd];
#
#info to be output
set csv {"#","names","directories","types","file_extensions","sizes_in_bytes","CRC-32"};
#
#move to the target directory
cd [file normalize $path];
set dir [pwd];
set dirName [lindex [file split $dir] end];
#
#filename to be output
set csvName "${dirName}_[clock seconds].csv";
#
#filename
set name {};
#
#number of files
set n 0;
#
set C {};
set crc32 0;
#
foreach e [glob -tails -path $dir/ *] {
#
#CSV character escape
set name [string map {\" \"\"} $e];
incr n 1;
#
set C {};
set crc32 0;
#
#CRC-32
#Checksum using the CRC-32 algorithm
if {![file isdirectory $dir/$e]} {
set C [open $dir/$e r];
fconfigure $C -encoding binary -translation binary;
set crc32 [format %x [zlib crc32 [gets $C]]];
close $C;
};
append csv "\n$n,\"$name\",\"$dirName\",\"[file type $dir/$e]\",\"[file extension $dir/$e]\",[file size $dir/$e],\"$crc32\"";
};
# ---------------
#
#back to the current working directory
cd $dir0;
# ---------------
#
#output as csv file
puts -nonewline [set csvC [open $csvName w]] $csv;
close $csvC;
# ===============
#
unset dir0 dir dirName name n C crc32 csvC;
#
#message to display output filename
puts stdout "------------\noutput: \"$csvName\"\n------------";
#
#returned value is csv data
return $csv;
};
#
#** Shell **
#
if {$argc} {
::dirCSV::read [lindex $argv 0];
};
#
##===================================================================
#*** LICENSE ***
#MIT License
#
#Copyright (c) 2023 Yuji Sode
#
#Permission is hereby granted, free of charge, to any person obtaining a copy
#of this software and associated documentation files (the "Software"), to deal
#in the Software without restriction, including without limitation the rights
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the Software is
#furnished to do so, subject to the following conditions:
#
#The above copyright notice and this permission notice shall be included in all
#copies or substantial portions of the Software.
#
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
#SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment