From: Ahmad on
Hi experts!

I've this piece of code:
############ code starts ###############
set pid [pid]
if {[file exists $myDir] && [file isdirectory $myDir]} {
## Check that the folder $myDir is not empty,
## if so, make a backup then delete its conents
if {[catch "file delete $myDir"]} {
file mkdir [file join $myDir backup_$pid]
## Now copy everything in $myDir to the backup
## then delete it
foreach file [glob *] {
if {[string equal $file backup_$pid]} {
file copy -force $file [file join $myDir backup_$pid $file]
file delete -force $file
}
}
} else {
## If $myDir doesn't exist, just create it
file mkdir $myDir
}
}

############ code end ###############

It runs without errors, but I don't know why it's not giving me the
expected results!!

Folder $myDir contents are not deleted when it's not empty.. The
backup dir is created but is always empty..

Also, if $myDir doesn't exist, it's not created!!

Can anyone see where's the problem?

Thanks,
Ahmad

From: Ahmad on
I may forgot to mention what is the objective of my code:

It's to:
- Take as an input certain dir name ($myDir)
- Check if it's there or not, if not --> Create it
- If existing:
- If not empty:
-make a backup inside it putting there all what it contained
- Then delete its contents (except the created backup dir)
- If empty:
- Just leave it. Or what I did actually is to re-create it after
deleting it.

Btw, I found one error in my code: I have to use "!" in this check: if
{![string equal $file backup_$pid]} {

Anyone can help, please?

Thanks,
Ahmad


On Jul 27, 5:21 pm, Ahmad <ahmad.abdulgh...(a)gmail.com> wrote:
> Hi experts!
>
> I've this piece of code:
> ############ code starts ###############
> set pid [pid]
> if {[file exists $myDir] && [file isdirectory $myDir]} {
>    ## Check that the folder $myDir is not empty,
>    ## if so, make a backup then delete its conents
>    if {[catch "file delete $myDir"]} {
>             file mkdir [file join $myDir backup_$pid]
>    ## Now copy everything in $myDir to the backup
>    ## then delete it
>             foreach file [glob *] {
>                     if {[string equal $file backup_$pid]} {
>                             file copy -force $file [file join $myDir backup_$pid $file]
>                             file delete -force $file
>                     }
>             }
>     } else {
>            ## If $myDir doesn't exist, just create it
>             file mkdir $myDir
>     }
>
> }
>
> ############ code end ###############
>
> It runs without errors, but I don't know why it's not giving me the
> expected results!!
>
> Folder $myDir contents are not deleted when it's not empty.. The
> backup dir is created but is always empty..
>
> Also, if $myDir doesn't exist, it's not created!!
>
> Can anyone see where's the problem?
>
> Thanks,
> Ahmad

From: Harald Oehlmann on
Dear Ahmad,

no expert here, just some remarks about the code:

On 28 Jul., 02:21, Ahmad <ahmad.abdulgh...(a)gmail.com> wrote:
> if {[file exists $myDir] && [file isdirectory $myDir]} {

IMHO it would be sufficient to do:
if { [file isdirectory $myDir] } {


>    ## Check that the folder $myDir is not empty,
>    ## if so, make a backup then delete its conents
>    if {[catch "file delete $myDir"]} {
>             file mkdir [file join $myDir backup_$pid]
>    ## Now copy everything in $myDir to the backup
>    ## then delete it
>             foreach file [glob *] {

I would instruct glob to only return the file name.
The "!" is IMHO missing in your code which causes that it does
nothing.

foreach file [glob -directory $myDir -nocomplain -tails
-- *] {
if { ! [string equal $file backup_$pid]} {
file copy -force [file join $myDir]
$file [file join $backupDir $file]
file delete -force [file join $myDir
$file]
}

>                     if {[string equal $file backup_$pid]} {
>                             file copy -force $file [file join $myDir backup_$pid $file]
>                             file delete -force $file
>                     }
>             }
>     } else {
>            ## If $myDir doesn't exist, just create it
>             file mkdir $myDir
>     }
>
> }

Hope this helps,
Harald