Sign in to follow this  
Followers 0
addictedtofunk

Copy the last modified file in a directory to another directory

8 posts in this topic

I want to copy a *.zip file that was last modified in a directory to another directory. How would i do this?

Share this post


Link to post
Share on other sites



Use the helpfile and look at FileGetTime(). Also look at FileFindFirstFile(), FileFindNextFile (), FileClose(), and lastly while loops. Here is an example on how to use them, straight from the help file:

; Shows the filenames of all files in the current directory.
$search = FileFindFirstFile("*.*")  

; Check if the search was successful
If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf

While 1
    $file = FileFindNextFile($search) 
    If @error Then ExitLoop
    
    MsgBox(4096, "File:", $file)
WEnd

; Close the search handle
FileClose($search)

Now all you have to do is feed the $file variable into the FileGetTime() function and then compare it to other values. I suggest you use an array to store the name of the file and the time $timestamp[0][0] = [name of the file] and then use something like $timestamp[0][1] = [time] to store the time. Use a > or a < statement to compare it to the array. If the time is newer than the value in the array, change the value in the array and proceed to the next file. When it completes searching in the directory, it should then stop and the remaining values in the array will be what you need to use. Then simply use the FileCopy() or FileMove() function to move the file from that directory to a directory you choose.


"So man has sown the wind and reaped the world. Perhaps in the next few hours there will no remembrance of the past and no hope for the future that might have been." & _"All the works of man will be consumed in the great fire after which he was created." & _"And if there is a future for man, insensitive as he is, proud and defiant in his pursuit of power, let him resolve to live it lovingly, for he knows well how to do so." & _"Then he may say once more, 'Truly the light is sweet, and what a pleasant thing it is for the eyes to see the sun.'" - The Day the Earth Caught Fire

Share this post


Link to post
Share on other sites

Use the helpfile and look at FileGetTime(). Also look at FileFindFirstFile(), FileFindNextFile (), FileClose(), and lastly while loops. Here is an example on how to use them, straight from the help file:

; Shows the filenames of all files in the current directory.
$search = FileFindFirstFile("*.*")  

; Check if the search was successful
If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf

While 1
    $file = FileFindNextFile($search) 
    If @error Then ExitLoop
    
    MsgBox(4096, "File:", $file)
WEnd

; Close the search handle
FileClose($search)

Now all you have to do is feed the $file variable into the FileGetTime() function and then compare it to other values. I suggest you use an array to store the name of the file and the time $timestamp[0][0] = [name of the file] and then use something like $timestamp[0][1] = [time] to store the time. Use a > or a < statement to compare it to the array. If the time is newer than the value in the array, change the value in the array and proceed to the next file. When it completes searching in the directory, it should then stop and the remaining values in the array will be what you need to use. Then simply use the FileCopy() or FileMove() function to move the file from that directory to a directory you choose.

Sorry but i am a bit a nwebie to this, i have read up on all thse things and still dont really know what to do, can you start me off or explain in more detail.

Share this post


Link to post
Share on other sites

you could also use the dir parameter /O:-S /D ...

c:>dir *.* /O:-S /D>dir.txt

the first file found is the newest. Just read first line in dir.txt and move accordingly.


> there are 10 types of people in the world, those who understand binary and those who don't.

Share this post


Link to post
Share on other sites

you could also use the dir parameter /O:-S /D ...

c:>dir *.* /O:-S /D>dir.txt

the first file found is the newest. Just read first line in dir.txt and move accordingly.

Unable to parse line???

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

sorry, I was just showing how to do it in a dosbox, with c:> being the prompt.

try this in autoit instead.

RunWait(@ComSpec & " /c dir *.* /D /O:-S>dir.txt","", @SW_HIDE)

explanation:

/D makes dir just show the filenames (without the date and such)

/O sorts the dir in according to sortorder S (by size, smallest first) the minus sign reverse the sorting order.

Edited by jinxter

> there are 10 types of people in the world, those who understand binary and those who don't.

Share this post


Link to post
Share on other sites

sorry, I was just showing how to do it in a dosbox, with c:> being the prompt.

try this in autoit instead.

RunWait(@ComSpec & " /c dir *.* /D /O:-S>dir.txt","", @SW_HIDE)

explanation:

/D makes dir just show the filenames (without the date and such)

/O sorts the dir in according to sortorder S (by size, smallest first) the minus sign reverse the sorting order.

Ok so that worked but the txt file has columns in it so the first line has multiple files in it, how do i single out the first one?

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

add a /B for bare format...

RunWait(@ComSpec & " /c dir *.* /B /D /O:-S>dir.txt","", @SW_HIDE)

It somehow was a bit different from the output to the cmd-window.

If you want to single out the first entry in a tabbed row I would search the firts line for a tab and use that in a StringLeft (), or perhaps use a StringSplit().

Hope it'll help some :whistle:

Edited by jinxter

> there are 10 types of people in the world, those who understand binary and those who don't.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0