Page 1 of 1 1
Topic Options
#133571 - 2005-02-10 03:49 PM Directory list array with exclusion and rename files
brewdude6 Offline
Hey THIS is FUN

Registered: 2000-10-21
Posts: 280
Loc: Nashville, TN
I've got the create array, with exclusion part done with help of UDF's, but I need to take the array with file names and rename the files to specific file names and append each filename with a number if more than one file is found ie.

file.log
file1.log
file2.log

Here is what I have so far.

Code:


If Exist($folder)
$dir=Dirlist($folder,4)
If Exist($folder+"\keep.log")
$rem=remfromarray($dir,$archive)
EndIf
For Each $log in $rem
;Shell "rename command to use"
?$rem
Next
EndIf

_________________________
I could have made a neat retort but didn't, for I was flurried and didn't think of it till I was downstairs.
-Mark Twain

Top
#133572 - 2005-02-10 09:04 PM Re: Directory list array with exclusion and rename files
Bryce Offline
KiX Supporter
*****

Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
How about this?

Code:

$folder = "%temp%"

$dir = dirplus($folder,"/a-d /f log")
for each $file in $dir
$file.move($folder + "\file"+$i+".log")
$i = $i+1
next



Will rename all fo the *.log files in the %temp% folder to file#.log


Edited by Bryce (2005-02-10 10:09 PM)

Top
#133573 - 2005-02-11 12:42 AM Re: Directory list array with exclusion and rename files
brewdude6 Offline
Hey THIS is FUN

Registered: 2000-10-21
Posts: 280
Loc: Nashville, TN
Thank you. I'm having one issue now using this and the remfromarray() udf. I just get all the output filenames redirected to the console. The code you posted works excellently, and I could move or copy the file before creating the array, but I'd rather leave the file in place and remove the element if possible.


$folder = "c:\temp"
$archive=$folder+"archive.log"

$dir = dirplus($folder,"/a-d /f log")
$dir1=remfromarray($dir,$archive)

For Each $file in $dir1
$file.Move($folder + "\file"+$i+".log")
$i = $i+1

Next
_________________________
I could have made a neat retort but didn't, for I was flurried and didn't think of it till I was downstairs.
-Mark Twain

Top
#133574 - 2005-02-11 01:40 AM Re: Directory list array with exclusion and rename files
Bryce Offline
KiX Supporter
*****

Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
just want to make sure i am 100% on what you are wanting to do.

You want to rename all *.log files in a folder to "file#.log" # equals a increasing counter. But you dont want to rename "archive.log" is that correct?

While you answer taht, a few pointers that i am spotting that will lead to problems.

(be sure that you got DIRPlus 2.31 (i had a small bug fix)

1.

$folder = "c:\temp"
$archive=$folder+"archive.log"

$archive is equal to "c:\temparchive.log"
you are missing a "\"

$archive=$folder+"\archive.log"


2.

DirPlus returns an array of FSO objects, so i am unsure how well remfromarray() will handle this.

one method would be to do something like this.

Code:

$folder = "c:\temp"
$archive= "archive.log"

$dir = dirplus($folder,"/a-d /f log")

For Each $file in $dir
if $file.name <> $archive
$file.Move($folder + "\file"+$i+".log")
$i = $i+1
endif
Next








Top
#133575 - 2005-02-11 02:40 AM Re: Directory list array with exclusion and rename files
brewdude6 Offline
Hey THIS is FUN

Registered: 2000-10-21
Posts: 280
Loc: Nashville, TN
That's exactly what I needed to do. Thanks a lot!
_________________________
I could have made a neat retort but didn't, for I was flurried and didn't think of it till I was downstairs.
-Mark Twain

Top
#133576 - 2005-02-11 06:00 PM Re: Directory list array with exclusion and rename files
brewdude6 Offline
Hey THIS is FUN

Registered: 2000-10-21
Posts: 280
Loc: Nashville, TN
If you don't mind...I want to make sure I understand (at least a little anyway) of what is going on with a couple sections of the code you posted. In this part
Code:

if $file.name <> $archive ;how does kix know what to do with "$file.name"? Is that just another way of expressing a variable?
$file.Move($folder + "\file"+$i+".log") ; same thing with this. It seems like this is a way of creating two different variables ($file.name and $file.move)?
;Also, does the "move" command allow you to overwrite the files in the $folder directory?
$i = $i+1
endif



Thanks for any explanation.
_________________________
I could have made a neat retort but didn't, for I was flurried and didn't think of it till I was downstairs.
-Mark Twain

Top
#133577 - 2005-02-11 06:37 PM Re: Directory list array with exclusion and rename files
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
$file is not just a simple var, it is a handle to the COM FSO object. $file.name then is its name, and $file.move is a move/rename operation.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#133578 - 2005-02-11 06:50 PM Re: Directory list array with exclusion and rename files
Bryce Offline
KiX Supporter
*****

Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
eep! LONG LINES!!

The $file.name and $File.Move are FSO object commands/references. The UDF DIRPlus returns an array of FSO objects of the requested files and or folders.

other FSO items.. are.

$file.path ;this is the full path to the file.
$file.datelastmodified ;the date of the last change to the file.

there are a lot....

here is a simple example of what DIRPlus is doing (minus directory recursion, and the filter options.)

Code:



$folder = "%temp%"

;this returns the specified Folder's FSO handle
$Folder_FSO = CreateObject("Scripting.FileSystemObject").getfolder($folder)

;this will list all subfolders in the root of $folder
for each $Subfolder in $Folder_FSO.subfolders
? $SubFolder.name
next

;this will list all files on the root of $Folder
For each $file in $Folder_FSO.files
? $File.name
next




DIRPlus takes those 2 simple steps and expands on them. It gives you the options to do a recursive directory crawl, with some strong filter options built into it. The UDF was built and modeled after a DOS DIR command.

For more information on all what you can do with FSO object.

Bryce





Top
#133579 - 2005-02-11 06:58 PM Re: Directory list array with exclusion and rename files
Bryce Offline
KiX Supporter
*****

Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
the .move FSO command does just that, it moves the file from one location to another. if you tell .move to move to the file to the same folder, but use a different name, the file is renamed.


Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
1 registered (Allen) and 1607 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.07 seconds in which 0.034 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org