Page 1 of 1 1
Topic Options
#190557 - 2008-11-12 11:33 AM copy files by creation date
serr57 Offline
Fresh Scripter

Registered: 2008-11-10
Posts: 18
Hello,

I use Kixtart since several time (only for login scripts), and I have to do a file operation, here is my target.

an automatic process generate a pdf file in a dedicated folder.
Those pdf are not always daily generated

I have to do a script that make a copy of each pdf on a network share in fonction of it's creation date, in a folder with its date (it is possible to have several files with same creation date)

When this file is copied the source must be deleted each one by one after a size control.

If I can do simple scripts, I am a stranger in this methodology (I'm not sure that it is possible)

But may be you can help me or give me a cap

Thanks a lot

serr57

Top
#190565 - 2008-11-13 03:59 PM Re: copy files by creation date [Re: serr57]
serr57 Offline
Fresh Scripter

Registered: 2008-11-10
Posts: 18
Nobody can help me ?
I have tried with getfiletime, but I haven't got results
May be it is not possible to do this or by a very complicated way

serr57

Top
#190569 - 2008-11-13 05:50 PM Re: copy files by creation date [Re: serr57]
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
This is not meant to be working code, only an example of one way you could do what you are thinking.

This code snippet makes use of the DIRPLUS() UDF found in the UDF libarary.

 Code:
$FileArray = DirPlus("\\server\share\")
For each $File in $FileArray
  $Date = Join(Split(Split(GetFileTime($File)," ")[0],"/"),"")
  If Not Exist ("\\server\share2\"+$Date)
    MD "\\Server\share2\"+$Date
  EndIf
  Copy $File "\\server\share2\"+$Date+"\"
  Del $File
Next
_________________________
Today is the tomorrow you worried about yesterday.

Top
#190571 - 2008-11-13 06:07 PM Re: copy files by creation date [Re: serr57]
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
What did you get for results?

The example in the Docs should serve you well.

Can you show us your code?

Thanks,

Kent
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

Top
#190603 - 2008-11-16 05:50 PM Re: copy files by creation date [Re: Kdyer]
serr57 Offline
Fresh Scripter

Registered: 2008-11-10
Posts: 18
Hello

Sorry to don't have answer earlier
I have tested this script, but it does nothing .... (I don't understand $File variable)

After lots of search on internet here is the code I used but there several error yet

Break OFF
Debug "ON"
SetConsole ("show")

;Use Y: "\\Myserver\share\"

$Swift = "C:\testswift1\"
$Acores = "C:\testswift2\"

; Scan dir for list of files

$nextfile = Dir("$Swift\*.*")

While $nextfile <> ""

$creationdate = GETFILETIME($nextfile, 1, 2)

$dirname = TRIM(JOIN(SPLIT(SUBSTR($creationdate, 1, 10), '/'), '-'))

$outputfile = $Acores + "\" + $dirname + "\" + $nextfile

If Exist ('$Acores' + $dirname)

Copy $nextfile $outputfile

ELSE

MD $dirname
Copy $nextfile $outputfile

ENDIF

;$nextfile = Dir()

Loop

;Use Y: /DEL

Top
#190604 - 2008-11-16 06:13 PM Re: copy files by creation date [Re: serr57]
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
I assume you are talking about this line in my example
 Code:
For each $File in $FileArray


The DirPlus UDF creates an array of files in the specified directory. The $File represents an Element within that Array.

The FOR EACH allows you to cycle through the array retrieving each element one at a time and do some process on it.

[example]
  • $filearray[0] = file1.ext
    $filearray[1] = file2.ext
    $filearray[2] = file3.ext
    $filearray[3] = file4.ext

[/example]

So as the FOR EACH loops through the array it will return FILEx.EXT incrementing by 1 as it goes.

As to your code, what errors are you getting and what is not working?
_________________________
Today is the tomorrow you worried about yesterday.

Top
#190605 - 2008-11-16 06:29 PM Re: copy files by creation date [Re: Gargoyle]
serr57 Offline
Fresh Scripter

Registered: 2008-11-10
Posts: 18
Hey what a quick answer congratulation,

When I used Debug "on", It does nothing only the loop
I think there is something wrong in my code.

I don't know how to have more information on what does the script
Do I have to redirect on an output txt file like in dos (> C:\folder\log.txt)?

Top
#190606 - 2008-11-16 08:10 PM Re: copy files by creation date [Re: serr57]
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
To find out what your script is doing use commands like this.

 Code:
Break OFF
Debug "ON"
SetConsole ("show")

;Use Y: "\\Myserver\share\"

$Swift = "C:\testswift1\"
$Acores = "C:\testswift2\"

; Scan dir for list of files

$nextfile = Dir("$Swift\*.*")
;What does the $nextfile contain
  $nextfile ?

While $nextfile <> ""
;show that we are in the While loop
  "In the while loop" ?

$creationdate = GETFILETIME($nextfile, 1, 2)
;What is the return
  "Creationdate = " + $creationdate + " Return code was " + @Serror ?

$dirname = TRIM(JOIN(SPLIT(SUBSTR($creationdate, 1, 10), '/'), '-'))
;Now what is our DirName giving us
  "Dirname = " + $Dirname + " Return code was " + @Serror ?

$outputfile = $Acores + "\" + $dirname + "\" + $nextfile
;What does our File name look like
  "The output file = " + $Outputfile

If Exist ('$Acores' + $dirname)
;Does it exist?
  "Exist was True.  Return code was " + @Serror ?

Copy $nextfile $outputfile
;Did the file copy?
  "File copy results " + @Serror ?

ELSE
;Exist failed
  "Directory did not exist" ?

MD $dirname
;Did we actually make a directory?
  "Make Directory command returned " +@Serror ?

Copy $nextfile $outputfile
;Did the copy work
  "File Copy results " + @Serror ?

ENDIF

;$nextfile = Dir()
;Remove comment above to return the next file in the directory
;This will continue to loop until you set the $nextfile to ""

Loop

;Use Y: /DEL 
_________________________
Today is the tomorrow you worried about yesterday.

Top
#190607 - 2008-11-16 08:52 PM Re: copy files by creation date [Re: Gargoyle]
serr57 Offline
Fresh Scripter

Registered: 2008-11-10
Posts: 18
Really many thanks
When I execute this script, it is normal that it doesn't find the folder
I don't why, the folder was created on my desktop, and not in "C:\testswift2\"

Is it a variable definition problem ?

Here is the debug log

serr57


?
FTest2.kix
0.
In the while loop
ssfully.date = 2008/09/20 17:05:15 Return code was The operation completed succe
Dirname = 2008-09-20 Return code was The operation completed successfully.
The output file = C:\testswift2\\2008-09-20\.Directory did not exist
ts.e Directory command returned Cannot create a file when that file already exis
File Copy results The system cannot find the path specified.
In the while loop

Top
#190608 - 2008-11-16 09:48 PM Re: copy files by creation date [Re: serr57]
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
Notice this in the Output file name

C:\testswift2\\2008-09-20\.
-------------^^
_________________________
Today is the tomorrow you worried about yesterday.

Top
#190738 - 2008-11-24 04:51 PM Re: copy files by creation date [Re: Gargoyle]
serr57 Offline
Fresh Scripter

Registered: 2008-11-10
Posts: 18
Many thanks but even after correction and lots of tests, it doesn't match.

I have tested you first proposition, with DirPlus, I have used your procédure to get more information in debuging mode, and it appears that dirplus is not reconised, is there some thing to do before using UDF library?

Here is my debug :

For each $File in $FileArray

0\\myserver\myshare\*.pdfdirplus

My script

Break OFF
Debug "ON"
SetConsole ("show")

$FileArray = dirplus("\\myserver\share\*.pdf")
$FileArray ?
For each $File in $FileArray
$Date = Join(Split(Split(GetFileTime($File)," ")[0],"/"),"")
If Not Exist ("C:\ZZArchivesSwiftPDF\"+$Date)
MD "C:\PDF\"+$Date
EndIf
Copy $File "C:\PDF\"+$Date+"\"
;Del $File
Next

In debug mode the windows close itself after "For each $File in $FileArray" fonction

£Any idea, mine is dirplus related,but I don't know how to use udf library, activation and so on ....

Tranks again

serr57

Top
#190739 - 2008-11-24 05:41 PM Re: copy files by creation date [Re: serr57]
BradV Offline
Seasoned Scripter
****

Registered: 2006-08-16
Posts: 686
Loc: Maryland, USA
Are you including the dirplus UDF in your code? You have to cut and paste the entire function and add it to your script.

Regards,

Brad

Top
#190745 - 2008-11-24 08:07 PM Re: copy files by creation date [Re: BradV]
serr57 Offline
Fresh Scripter

Registered: 2008-11-10
Posts: 18
Sorry I don't understand, I should copy this :
 Code:
Function DirPlus($path,optional $Options, optional $f, optional $sfflag)
	If not vartype($f)	DIM $f		EndIf
	If not vartype($sfflag)	DIM $sfflag	EndIf

	DIM $file, $i, $temp, $item, $ex1, $mask,$mask1,$maskArray,$maskarray1,
	$ex2, $code, $CodeWeight, $targetWeight, $weight, $masktrue
	DIM $tarray[0]

	$ex1 = SetOption(Explicit,on)
	$ex2 = SetOption(NoVarsInStrings,on)
	$codeWeight = 0

	If not Exist($path) 
		$temp = SetOption(Explicit,$ex1)
		$temp = SetOption(NoVarsInStrings,$ex2)
		Exit @ERROR
	EndIf

	If not vartype($f)
		$f = CreateObject("Scripting.FileSystemObject").getfolder($path)
	EndIf
	If @ERROR 
		$temp = SetOption(Explicit,$ex1)
		$temp = SetOption(NoVarsInStrings,$ex2)
		Exit @ERROR
	EndIf

 	For Each $temp In Split($options,"/")
		$temp=Trim($temp)
		Select
		   Case left($temp,1) = "s"
			If not vartype($sfflag)
				If Val(right($temp,-1)) = 0
					$sfflag = -1
				Else
					$sfflag = Val(right($temp,-1))
				EndIf	
			EndIf
		   Case Left($temp,1) = "a"
			Select
			   Case Right($temp,-1)="d"
				$codeWeight = $codeWeight + 1
				$temp = "if $file.attributes & 16 " ;"if $file.type = 'File Folder' "
			   Case Right($temp,-1)="-d"
				$codeWeight = $codeWeight + 1
				$temp = "if ($file.attributes & 16)=0 " ;"if $file.type <> 'File Folder' "
			   Case Right($temp,-1)="s"
				$codeWeight = $codeWeight + 1
				$temp = "if $file.attributes & 4 "
			   Case Right($temp,-1)="-s"
				$codeWeight = $codeWeight + 1
				$temp = "if ($file.attributes & 4)=0 "
			   Case Right($temp,-1)="h"
				$codeWeight = $codeWeight + 1
				$temp = "if $file.attributes & 2 "
			   Case Right($temp,-1)="-h"
				$codeWeight = $codeWeight + 1
				$temp = "if ($file.attributes & 2)=0 "
			   Case Right($temp,-1)="r"
				$codeWeight = $codeWeight + 1
				$temp = "if $file.attributes & 1 "
			   Case Right($temp,-1)="-r"
				$codeWeight = $codeWeight + 1
				$temp = "if ($file.attributes & 1)=0 "
			   Case Right($temp,-1)="a"
				$codeWeight = $codeWeight + 1
				$temp = "if $file.attributes & 32 "
			   Case Right($temp,-1)="-a"
				$codeWeight = $codeWeight + 1
				$temp = "if ($file.attributes & 32)=0 "
			EndSelect
			$code = $temp + "$weight=$weight+1 endif" +@CRLF + $code

		   Case Left($temp,1) = "m"
			$maskarray = Split(Right($temp,-2),"|")
			$codeweight = $codeweight + 1
			$code = "$masktrue=0 for Each $mask in $maskarray if instr($file.name,$mask) $masktrue=1 " +
			"EndIf Next If $masktrue $weight=$weight+1 endif" + @CRLF +$code
		   Case Left($temp,1) = "f"
			$maskarray1 = Split(Right($temp,-2)," ")
			$codeweight = $codeweight + 1
			$code = "$masktrue=0 for Each $mask1 in $maskarray1 if substr($file.name,Instrrev($file.name,'.')+1)" +
			"=$mask1 $masktrue=1 EndIf Next If $masktrue $weight=$weight+1 endif" + @CRLF +$code

		EndSelect
	Next
	$code = "$weight = 0 $targetWeight = " + $codeweight + @CRLF + $code
	$code = $code + "if $weight = $targetweight Exit 1 endif"

	For Each $file In $f.subfolders
		If Execute($code)
			$tarray[$i] = $file
			$i = $i + 1
			ReDIM preserve $tarray[$i]
		EndIf
		If $sfflag
			$temp = dirplus($file,$options,$file,$sfflag-1)
			For Each $item In $temp
				$tarray[$i] = $item
				$i = $i + 1
				ReDIM preserve $tarray[$i]
			Next
		EndIf
	Next
	For Each $file In $f.files
		If Execute($code)
			$tarray[$i] = $file
			$i = $i + 1

			ReDIM preserve $tarray[$i]
		EndIf
	Next

	If $i
		ReDIM preserve $tarray[$i-1]
		$i=0
	Else
		$tarray = 0
	EndIf

	$dirplus = $tarray
	$temp = SetOption(Explicit,$ex1)
	$temp = SetOption(NoVarsInStrings,$ex2)
	Exit @ERROR
EndFunction

into my script ?

serr57


Edited by Benny69 (2008-11-24 08:31 PM)
Edit Reason: I modified this post to include Code Tags

Top
#190747 - 2008-11-24 11:07 PM Re: copy files by creation date [Re: serr57]
Bryce Offline
KiX Supporter
*****

Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
yes,

here is the FAQ on how to use UDF'S
http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=81594&site_id=1#import


Edited by Bryce (2008-11-24 11:08 PM)

Top
#190846 - 2008-12-04 04:57 PM Re: copy files by creation date [Re: Bryce]
serr57 Offline
Fresh Scripter

Registered: 2008-11-10
Posts: 18
Hello,

I have finished my script, it works very well, and does what ever I would
If it can help somebody here is it

 Code:
				;Begin of Script
										
Break ON
Debug "OFF"
SetConsole ("show")

$Swift = "\\myserver\myshare"
$Acores = "\\myserver\myshare"

; Scan directory for files listing

$nextfile = Dir("$Swift\*.pdf")

While $nextfile <> ""  and @error = 0

; Variables creation to get files time, Using 10 string characters of the date, define the output

$creationdate = GETFILETIME($Swift + "\" + $nextfile, 0, 2)
$dirname = TRIM(JOIN(SPLIT(SUBSTR($creationdate, 1, 10), '/'), '-'))
$outputfile = $Acores + "\" + $dirname + "\" + $nextfile

; Folder existance checking before copy

IF NOT EXIST ($Acores + "\" + $dirname)
			MD $Acores + "\" +$dirname
ENDIF

IF NOT EXIST ($outputfile)
	Copy $Swift + "\" + $nextfile $outputfile
	DEL $Swift + "\" + $nextfile   
ENDIF

$nextfile = DIR ()

Loop

EXIT

				;end of Script



Many thanks for your help

serr57


Edited by Benny69 (2008-12-04 05:04 PM)
Edit Reason: Added Code Tags

Top
#190849 - 2008-12-04 05:18 PM Re: copy files by creation date [Re: serr57]
BradV Offline
Seasoned Scripter
****

Registered: 2006-08-16
Posts: 686
Loc: Maryland, USA
The only suggestion I would make is to check the status of the copy before deleting the source file. I'm just not comfortable progamatically deleting a file without first checking that the copy succeeded.

Regards,

Brad

Top
#190865 - 2008-12-04 09:34 PM Re: copy files by creation date [Re: BradV]
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
 Originally Posted By: BradV
The only suggestion I would make is to check the status of the copy before deleting the source file. I'm just not comfortable progamatically deleting a file without first checking that the copy succeeded.


Brad,

If memory serves, Howard has a CRC32 that can verify the content of the file being copied.

Kent
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

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
2 registered (morganw, mole) and 414 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.077 seconds in which 0.029 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