Page 1 of 1 1
Topic Options
#175219 - 2007-04-04 07:52 PM rename files script
booey Offline
Getting the hang of it

Registered: 2005-07-25
Posts: 76
Loc: USA
I have the following script to rename the files in a directory and add a 1,2,3, etc to the end of the file. I want the number to be in the format of filenamedate-1.tif, filenamedate-2.tif, etc. When I run the script, the first file adds a hyphen at the end of the filename with no number after it, such as filenamedate-.tif.

Is there a way to start with the -1 on the first file? If not, can I prevent the "-" from appearing at the end of the first filename?

Here's what I have so far:
 Code:
$folder = "U:\test\"
$dir = dirplus($folder,"/a-d /f tif")

$date = Trim(Join(Split(@DATE,'/'),'-'))

For Each $file In $dir
		 $file.Move($folder + "AND" + $date + "-" + $i + ".tif")
		? @SERROR
		$1 = 1
	
Next

Top
#175222 - 2007-04-04 08:30 PM Re: rename files script [Re: booey]
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
I do net get your code totally but I think you forget to give an initial value to $i and afterwards you forget to add 1 to $i in every loop you make
something like:
 Code:
$folder = "U:\test\"
$Dir = dirplus($folder,"/a-d /f tif")
$i = 1

$date = Trim(Join(Split(@DATE,'/'),'-'))

For Each $file In $Dir
		 $file.Move($folder + "AND" + $date + "-" + $i + ".tif")
		? @SERROR
		$i = $i + 1
	
Next

Top
#175223 - 2007-04-04 09:24 PM Re: rename files script [Re: Witto]
booey Offline
Getting the hang of it

Registered: 2005-07-25
Posts: 76
Loc: USA
You're right. I was cleaning up my code a little when posting and deleted the wrong line. You're suggestion is what I have, but I still have the same issue.
Top
#175226 - 2007-04-04 10:10 PM Re: rename files script [Re: booey]
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
For clarity, please post your current code.
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#175227 - 2007-04-04 10:18 PM Re: rename files script [Re: Howard Bullock]
booey Offline
Getting the hang of it

Registered: 2005-07-25
Posts: 76
Loc: USA
Sorry for the confusion.

 Code:
$folder = "U:\test"
$dir = dirplus($folder,"/a-d /f tif")

$date = Trim(Join(Split(@DATE,'/'),'-'))


For Each $file In $dir
		 $file.Move($folder + "\AND" + $date + "-" + $i + ".tif")
		? @SERROR
		$i = $i+1
	
Next

Top
#175228 - 2007-04-04 10:26 PM Re: rename files script [Re: booey]
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
You are not initializing the variable "$i" before its first use.
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#175229 - 2007-04-04 10:39 PM Re: rename files script [Re: Howard Bullock]
booey Offline
Getting the hang of it

Registered: 2005-07-25
Posts: 76
Loc: USA
What do you mean by "initialize"? Are you talking about doing:
"Dim $i" at the top of the script

Top
#175232 - 2007-04-04 11:14 PM Re: rename files script [Re: booey]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
it means that your current code does not set initial value for the var.
so, as it's empty when you do the magic, there no number.

simple way to change it is to move this line:
$i = $i+1

before the file.move() line.
_________________________
!

download KiXnet

Top
#175233 - 2007-04-04 11:16 PM Re: rename files script [Re: Lonkero]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
btw, I would suggest that you use actually something like:
$file.Move($folder + "\AND" + $date + "-" + right("00"+$i,3) + ".tif")


to make the numbers sort correctly when 10 or more images present.
_________________________
!

download KiXnet

Top
#175234 - 2007-04-04 11:32 PM Re: rename files script [Re: Lonkero]
booey Offline
Getting the hang of it

Registered: 2005-07-25
Posts: 76
Loc: USA
That worked. Thanks everyone for the help.
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
0 registered and 895 anonymous users online.
Newest Members
batdk82, StuTheCoder, M_Moore, BeeEm, min_seow
17885 Registered Users

Generated in 0.061 seconds in which 0.025 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