| View previous topic :: View next topic |
| Author |
Message |
glennsftn Voice
Joined: 23 Mar 2015 Posts: 2
|
Posted: Mon Mar 23, 2015 11:26 pm Post subject: change filenames |
|
|
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: | #!/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 |
|
| Back to top |
|
 |
heartbroken Op

Joined: 23 Jun 2011 Posts: 106 Location: somewhere out there
|
|
| Back to top |
|
 |
SpiKe^^ Owner

Joined: 12 May 2006 Posts: 792 Location: Tennessee, USA
|
Posted: Tue Mar 24, 2015 1:16 am Post subject: |
|
|
Maybe this is closer to what you say you want... | Code: | #!/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
. |
|
| Back to top |
|
 |
glennsftn Voice
Joined: 23 Mar 2015 Posts: 2
|
Posted: Tue Mar 24, 2015 1:14 pm Post subject: |
|
|
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. |
|
| Back to top |
|
 |
|