Page 1 of 1 1
Topic Options
#185796 - 2008-02-28 01:53 PM Renaming files
brewdude6 Offline
Hey THIS is FUN

Registered: 2000-10-21
Posts: 280
Loc: Nashville, TN
I need do a directory listing of files that has a file name format like this.

 Quote:

FooLog-0-YYYY-MM-DD.csv


I need to take this list of files and rename in this format.

 Quote:

servername_PROD_YYYYMMDD_HHMMSS_fooLOG_0000.csv


The "0" after foolog is the part that is causing me some problems. In the renamed format the series of "0's" is any number up to thousands but I need to pad 0's if it isn't 4 digits.


Also, when I run this against a directory, it will only process one file while deleting the rest of the directory.

Here is what I have so far. Any help would be appreciated:

 Code:
$path="c:\admin\foo"
$archive="c:\admin\archive"
$date=@DATE
$time=@TIME
$modtime=Join(Split($time,":"),"")
$moddate=Join(Split($date,"/"),"")



$Dir = dirscan($path,1)
For Each $file in $dir
  If InStr($file,"-")
   Move $path+"\"+$file $archive+"\"+@wksta+"_prod_"+$moddate+"_"+$modtime+"_foolog_0000.csv"
  EndIf
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
#185798 - 2008-02-28 03:17 PM Re: Renaming files [Re: brewdude6]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
It isn't deleting the files - it's moving each source file to the same destination file!

Don't you need to increment the sequence number or something, so each destination has a unique name?

Replace your Move... command with the following and view what's happening. You'll probably see an obvious problem...

 Code:
'Move  ' $path + "\" + $file ' ' $archive + "\" + @wksta + "_prod_" + $moddate + "_" + $modtime + "_foolog_0000.csv"


Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#185799 - 2008-02-28 03:57 PM Re: Renaming files [Re: Glenn Barnas]
brewdude6 Offline
Hey THIS is FUN

Registered: 2000-10-21
Posts: 280
Loc: Nashville, TN
I need to read the sequence number and pad it with zeros to make a 4 digit field. I was planning on doing this.

 Code:
$sequence=split($file,"-")[1]


I just don't know how I would take whatever that number is and pad "0's" in front of it to make it a 4 digit number.

Thank you for the tip, I will try that.
_________________________
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
#185800 - 2008-02-28 04:04 PM Re: Renaming files [Re: brewdude6]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
You could do something like this to see how many characters the number is and add 0's if needed.
Probably not the most elegant way but it does work.

 Code:
Break on

$number = "0002"

$lenght = Len(Val($number))
?$lenght
$add = 4 - $lenght
?$add
Select
	Case $add = 1
		$number = "0" + Val($number)
		?$number
	Case $add = 2
		$number = "00" + Val($number)
		?$number
	Case $add = 3
		$number = "000" + Val($number)
		?$number
EndSelect
Sleep 5
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#185801 - 2008-02-28 04:25 PM Re: Renaming files [Re: brewdude6]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
Padding with anything is easy, actually.
 Code:
$PadStr = "0000000000" ; pad char string - more than enough to pad
$PadLen = 4

$Value = 3
; left-pad the value with PadStr for PadLen characters
$PValue = Right($PadStr + CStr($Value), $PadLen)

Concept works with any pad character. You can right-pad as well by taking the Left() of the resulting string.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#185802 - 2008-02-28 04:48 PM Re: Renaming files [Re: Glenn Barnas]
brewdude6 Offline
Hey THIS is FUN

Registered: 2000-10-21
Posts: 280
Loc: Nashville, TN
Why is $Vaule=3? Does that assume the field is 3 characters?
_________________________
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
#185804 - 2008-02-28 05:03 PM Re: Renaming files [Re: brewdude6]
brewdude6 Offline
Hey THIS is FUN

Registered: 2000-10-21
Posts: 280
Loc: Nashville, TN
I abandoned the "move" command temporarily and tried using the rename udf. I think I still have the same problem, but I don't understand why. I get the first file renamed and the rest are ignored.

 Code:
$path="c:\admin\foo"
$archive="c:\admin\archive"

$date=@DATE
$time=@TIME
$modtime=Join(Split($time,":"),"")
$moddate=Join(Split($date,"/"),"")
$filename=@wksta+"_prod_"+$moddate+"_"+$modtime+"_foolog_0000.csv"


$Dir = dirscan($path,1)
For Each $file in $dir
  If InStr($file,"-")
	rename($path,$file,$filename)
  EndIf
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
#185805 - 2008-02-28 05:17 PM Re: Renaming files [Re: brewdude6]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
No files are ignored. Each file is renamed to the same name as the previous file. Like said above you will need to implement some kind of counter that creates a unique part of the filename.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#185806 - 2008-02-28 05:20 PM Re: Renaming files [Re: Mart]
brewdude6 Offline
Hey THIS is FUN

Registered: 2000-10-21
Posts: 280
Loc: Nashville, TN
Ahhhh...makes sense now! I thought that the time stamp in the file would make the filenames unique.
_________________________
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
#185807 - 2008-02-28 05:31 PM Re: Renaming files [Re: brewdude6]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Nope. You are generating the timestamp once and use it each time. If you generate the timestamp in the loop it would be unique for each file. You might need to add a small sleep in the loop after renaming the file just to be sure that the timestamps are unique. This depends on the speed of the script.

Edited by Mart (2008-02-28 05:31 PM)
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#185808 - 2008-02-28 06:14 PM Re: Renaming files [Re: brewdude6]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
No - sorry for the confusion - I just picked an arbitrary value for the example, should have displayed "0003"

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#185809 - 2008-02-28 06:20 PM Re: Renaming files [Re: brewdude6]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
If you followed my suggestion, you'd be displaying the commands on the screen instead of executing them. The problem would have become quite obvious!

Displaying Copy, Move, and Delete commands is something I do regularly so I can inspect what will be done before it actually happens.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#185815 - 2008-02-28 09:46 PM Re: Renaming files [Re: Glenn Barnas]
brewdude6 Offline
Hey THIS is FUN

Registered: 2000-10-21
Posts: 280
Loc: Nashville, TN
I did follow your instructions, and did see a problem, but I didn't understand exactly what was happening at the time. Thanks.
_________________________
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
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 1334 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

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

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