Re: file problem
- From: gavino <gavcomedy@xxxxxxxxx>
- Date: Tue, 06 Nov 2007 12:32:17 -0800
On Nov 6, 10:52 am, Bezoar <cwjo...@xxxxxxxxx> wrote:
On Nov 6, 7:01 am, "Larry W. Virden" <lvir...@xxxxxxxxx> wrote:
On Nov 5, 9:41 pm, gavino <gavcom...@xxxxxxxxx> wrote:
12000 directories, 5 dir deep
/some/dir/with/some/name
:
move all directories(+content) that have an Licensing_Window_End
yesterday or older into a new directory called
/opt/out_of_date
So if /some/dir/with/some/name/file.xml has the older date, you want
to move
the directory "name", or "some/name", or "with/some/name" or what? How
far up the tree do you move?
Is "name" unique? if not, then moving them into /opt/out_of_date is
going to result in conflicts which are going to mess up your tree...
Sounds like a bit more detail on requirements are going to be needed.
Then, take a look at fileutil::find or findByPattern , some package
like tdom, perhaps, to parse and search the xml file, and file rename
to do the move. Also, take a look at the file man page - where the
manual shows a simulated find of files as well.
I would not use TDom as suggested... Why do all the parsing when a
regexp is sufficient.
The following script recursively descends through a directory and if
it finds a file it will
read it in and extract the date using a regexp. I dont check to see
if the file is an xml file
that is a refinement that you can add if you need to. I also did not
use the fileutil package from
tcllib because the functions like "find" or "findByPattern" follow
symlinks. This is rather retarded
default behavior which would not be too bad if they had a way to
prevent this behaviour but
there is none. This may not be a problem on windows but on unix it
can lead to some real
headaches. I did this on linux.
each file x.xml contains a variant of this where only the date value
changes :
<? xml version="1.0"?>
<tag1 name="X" >
Some data
</tag1>
<App_Data App="MOD" Name="Licensing_Window_End" Value="2007-10-30"/>
<tag2 name="Y">
Some more data
</tag2>
in /tmp/testdir the following exists (hoping to match your situation)
.
`-- a
|-- b
| |-- c
| | `-- d
| |-- h
| | |-- i
| | | `-- x.xml
| | `-- x.xml
| `-- x.xml
`-- e
|-- f
| `-- g
`-- x.xml
after running the script I have the following output:
processing /tmp/testdir/a/x.xml
/tmp/testdir/a/x.xml is to young to copy
processing /tmp/testdir/a/b/x.xml
/tmp/testdir/a/b/x.xml is old I am copying it to /tmp/mytest/tmp/
testdir/a/b/x.xml
processing /tmp/testdir/a/b/c/x.xml
/tmp/testdir/a/b/c/x.xml is to young to copy
processing /tmp/testdir/a/b/c/d/x.xml
/tmp/testdir/a/b/c/d/x.xml is to young to copy
processing /tmp/testdir/a/b/h/x.xml
/tmp/testdir/a/b/h/x.xml is old I am copying it to /tmp/mytest/tmp/
testdir/a/b/h/x.xml
processing /tmp/testdir/a/b/h/i/x.xml
/tmp/testdir/a/b/h/i/x.xml is old I am copying it to /tmp/mytest/tmp/
testdir/a/b/h/i/x.xml
processing /tmp/testdir/a/e/x.xml
/tmp/testdir/a/e/x.xml is old I am copying it to /tmp/mytest/tmp/
testdir/a/e/x.xml
processing /tmp/testdir/a/e/f/x.xml
/tmp/testdir/a/e/f/x.xml is to young to copy
processing /tmp/testdir/a/e/f/g/x.xml
/tmp/testdir/a/e/f/g/x.xml is to young to copy
Errors :
the resulting tree under /tmp/mytest is:
.
`-- tmp
`-- testdir
`-- a
|-- b
| |-- h
| | |-- i
| | | `-- x.xml
| | `-- x.xml
| `-- x.xml
`-- e
`-- x.xml
---------------- Script
proc OperateRecursively { dir command errorList } {
upvar $errorList err
set directories ""
foreach f [lsort -dictionary [glob -nocomplain -directory $dir --
* ] ] {
if { [ file isdirectory $f ] } {
lappend directories $f;
continue;
}
# note $f is the full path to a file
if { [ catch { eval $command \$f } err ] } {
lappend errorList "$f:$err"
}
}
foreach d $directories {
OperateRecursively $d $command err
}}
# test proc
proc ::printfilename { filename } {
puts $filename}
proc ::ifOldMove { filename } {
# compare time is time to beat
# destdir is directory to replicate tree for old file
# so if file is /a/b/c/d/e.xml and destdir is /backup
# then /backup/a/b/c/d/e.xml will be the destination
global compareTime destdir ; # delete me if no globals
puts "processing $filename"
set fd [open $filename "r" ]
set buffer [read $fd ]
if { [ regexp {Name=\"Licensing_Window_End\" Value=\"([0-9-]+)\"\/>} $buffer match date ] == 0} {
error "Could not find requisite Tag to determine age"
}
#will give secs since epoch at 00:00:00 am date
set filetime [clock scan $date ] ;
if { $compareTime > $filetime } {
set finaldestdir [file normalize [file join $destdir [string trim
[file dirname $filename] "/"] ] ]
catch { file mkdir $finaldestdir } ; #will make any parents
file copy $filename [file join $finaldestdir [file tail
$filename ] ]
puts "$filename is old I am copying it to [file join $finaldestdir
[file tail $filename ] ]"
} else {
puts "$filename is to young to copy"
}}
global compareTime destdir errors
set compareTime [clock scan "2007-10-18" ]
set destdir /tmp/mytest
file mkdir $destdir
set errors {}
OperateRecursively /tmp/testdir ifOldMove errors
puts "Errors : $errors "
---------------------End Script -----------------------------
Carl
all of the directories are at the same level 5 deep, and only the
directories with the line in their xml file showing a date yesterday
or earlier need be moved. The entire path is not needed, only the
directory itself.
if
/home/joe/porn/vids/20071004
/home/joe/porn/vids/20071209
were 2 directories
and the 1209 had a 'expiration' of older than yesterday, while 1004
had an expiration in 2008 (both in xml files in each); then 1209 would
be moved:
/opt/20071209
(The real problem has 12000 such directories)
In bash I did find to find all the xml files, looped over them greping
each then cut to find the date, then printing a line with date and
path, then sorting it; then cutting off the path from the sorted list,
then looped the sorted path with a mv. (I had to manually intervene
with vi to delete the dates today or newer from the sorted list of
paths). Then finally I had some problem with spaces in directory names
I had to manually mvoe then deletee from the sorted
list.........finally it worked...but I had to repeat the last 2 step
sicne it onyl worked half way, but with regenrating the list a second
time, the seocnd move did it. I then did the origional find and sort
with patha dn date to make sure /opt ahd only old titles, and orig dir
had one from today onward.
FHEW!!
.
- References:
- file problem
- From: gavino
- Re: file problem
- From: Larry W. Virden
- Re: file problem
- From: Bezoar
- file problem
- Prev by Date: How to add extra chinese char.to encoding file in HP unix system?
- Next by Date: Re: How to add extra chinese char.to encoding file in HP unix system?
- Previous by thread: Re: file problem
- Next by thread: Re: file problem
- Index(es):
Relevant Pages
|