This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

change filenames

Help for those learning Tcl or writing their own scripts.
Post Reply
g
glennsftn
Voice
Posts: 2
Joined: Mon Mar 23, 2015 10:57 pm

change filenames

Post by glennsftn »

Please excuse my noobiness, I’ve just been learning TCL a few days now and have little coding experience except a rudimentary understanding of Expect.

I have a NAS with a bunch of files that I’d like to rename. I want to create a script which will generate the commands to name the files from “this” to “that.” Below is a quick example of how I’m going about this.

Code: Select all

#!/usr/bin/tcl

set src1 myfilename1.txt
set src2 myfilename2.txt
set src3 myfilname3.txt
set dst1 mynewfilename1.txt
set dst2 mynewfilename2.txt
set dst3 mynewfilename3.txt
set count 0
set log [open output.txt w]


while { $count < 4 } {
   set count [incr $count]
   set srcName "src$count"
   set dstName "dst$count"
   puts $log "mv $srcName $dstName"
}

close $log
What I would like the output.txt to be is:

mv myfilename1.txt mynewfilename1.txt
..
..
mv myfilename4.txt mynewfilename4.txt


The actual output:
$ cat output.txt
mv src1 dst1
mv src1 dst1
mv src2 dst2
mv src1 dst1
mv src3 dst3
mv src1 dst1
mv src4 dst4
User avatar
heartbroken
Op
Posts: 110
Joined: Thu Jun 23, 2011 11:15 pm
Location: somewhere out there

Post by heartbroken »

Last edited by heartbroken on Tue Mar 24, 2015 1:17 am, edited 1 time in total.
Life iS Just a dReaM oN tHE wAy to DeaTh
User avatar
SpiKe^^
Owner
Posts: 831
Joined: Fri May 12, 2006 10:20 pm
Location: Tennessee, USA
Contact:

Post by SpiKe^^ »

Maybe this is closer to what you say you want...

Code: Select all

#!/usr/bin/tcl 

set src(1) "myfilename1.txt"
set src(2) "myfilename2.txt"
set src(3) "myfilname3.txt"
set dst(1) "mynewfilename1.txt"
set dst(2) "mynewfilename2.txt"
set dst(3) "mynewfilename3.txt"
set count 0 
set log [open output.txt w] 


while {$count < 3} { 
   incr count
   set srcName $src($count)
   set dstName $dst($count)
   puts $log "mv $srcName $dstName" 
} 

close $log

You should probably have the tcl script actually do the file renaming, and as mentioned above, mv is not a tcl command:)
SpiKe^^

Get BogusTrivia 2.06.4.7 at www.mytclscripts.com
or visit the New Tcl Acrhive at www.tclarchive.org
.
g
glennsftn
Voice
Posts: 2
Joined: Mon Mar 23, 2015 10:57 pm

Post by glennsftn »

Thanks guys,

That is exactly what I wanted. The output was to go to a text file which I could copy/paste into an SSH session. It would have been ideal to write this in Expect, as I could send the commands I want within the SSH session, but I didn't know how to do the loop with the variables. I think I have enough now to migrate this over to Expect. I appreciate the help.
Post Reply