#14346 - 2001-11-15 05:15 PM
renaming files using variables
|
mikew50
Fresh Scripter
Registered: 2001-11-15
Posts: 17
Loc: NJ
|
I am new kix script user. What I am trying to do is copy and rename files using variables. They are log files but I want the new name to be the date it was created. Any help would be appreciated. mikehere is a sample of what i have already: $file="j:\drs1\drs1backup\*.log" $newname="j:\drs1\drs1backup\@Date.log" if exist $file shell '%comspec% /c ren $oldfile $newfile'
|
|
Top
|
|
|
|
#14347 - 2001-11-15 08:15 PM
Re: renaming files using variables
|
Kdyer
KiX Supporter
   
Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
|
Mike,You will probably need to parse out the relevant Date information.. code:
BREAK ON CLS?@Date ; -- Assuming @Date yields, 2001/11/15 $Mo = SUBSTR(@date, 6, 2) $Day = SUBSTR(@date, 9, 2) $Year = SUBSTR(@date, 1, 4) $Date = "$Mo$Day$Year" ?$Date
Once you get this, change the following line to be -$newname="j:\drs1\drs1backup\$Date.log" HTH, - Kent
|
|
Top
|
|
|
|
#14348 - 2001-11-15 10:08 PM
Re: renaming files using variables
|
mikew50
Fresh Scripter
Registered: 2001-11-15
Posts: 17
Loc: NJ
|
Kent Thanks I'll give it a shot! Mike
|
|
Top
|
|
|
|
#14349 - 2001-11-16 02:11 AM
Re: renaming files using variables
|
MCA
KiX Supporter
   
Registered: 2000-04-28
Posts: 5152
Loc: Netherlands, EU
|
Dear,Welcome to the board. We are using in our version the GetFileTime function to get the date of last modification of a file. The change all "*.LOG" files to the format "YYYYMMDD.LOG". No renaming when file is in use or new file already exist. Our version:
code:
$log_directory="j:\drs1\drs1backup" ; $tmp_file="%temp%\zzzzz.tmp" SHELL '%comspec% /c dir "$log_directory\*.log" /b >$tmp_file ' IF (Open(1,$tmp_file,2) = 0) $old_file=ReadLine(1) WHILE (@error = 0) ? $old_file+" -> "+GetFileTime($old_file) $fdate=GetFileTime($old_file) IF (len($fdate) <> 0) $new_file= substr($fdate,1,4)+substr($fdate,6,2)+substr($fdate,9,2) ; $new_file=$new_file+"_"+substr($fdate,12,2)+substr($fdate,15,2)+substr($fdate,18,2) $new_file=$new_file+".log" ; IF ($old_file <> $new_file) AND (Exist($log_directory+"\"+$new_file) = 0) ? 'rename "$log_directory\$old_file" "$new_file" ' shell '%comspec% /c ren "$log_directory\$old_file" "$new_file" ' ELSE ? '-skip- "$log_directory\$old_file" (duplicate entry)' ENDIF ELSE ? '-skip- "$log_directory\$old_file" ' ENDIF $old_file=ReadLine(1) LOOP IF Close(1) ENDIF ENDIF DEL $tmp_file
It is also possible to change it to "YYYYMMDD_HHMMSS.LOG" format. You only need to remove ; symbol in line 12. greetings.
|
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 696 anonymous users online.
|
|
|