Computing.Net > Forums > Programming > Move the Oldest File by Last Modifi

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Move the Oldest File by Last Modifi

Reply to Message Icon

Name: JCH (by hurtado)
Date: June 27, 2006 at 07:39:19 Pacific
OS: Windows XP Pro
CPU/Ram: Athlon/ 376 RAM
Product: Compaq
Comment:

I have created but not complete a vbscript that can move the oldest file by last modified date(not current date). The code is the following:

Dim totFileCount,startFolder,folderdestination,objFile,totalFiles,fs

Set fso = CreateObject("Scripting.FileSystemObject")
startFolder = "C:\Documents and Settings\jhurtado\Desktop\VBS"
totFileCount = fso.GetFolder(startFolder).Files.Count
If totFileCount <= 1 Then
Wscript.Quit
End If
Set totalFiles = fso.GetFolder(startFolder)
folderdestination = "C:\Documents and Settings\jhurtado\Desktop\VBS_Old"
For Each objFile In totalFiles.Files
If objFile.DateLastModified Then
objFile.Move folderdestination & "\" & objFile.Name
End If
Next
Wscript.Quit

To give you an example I have two files, a text file and excel file. The text file have a last modified date of 1/2/2006 and the excel file have a last modified date of 1/16/2006. I want to move the oldest file to another folder (in this case the text file)to another folder and leave the excel file (in this case) intact in place. Can you help me????Thanks for your help.




Sponsored Link
Ads by Google

Response Number 1
Name: Michael J (by mjdamato)
Date: June 27, 2006 at 08:45:42 Pacific
Reply:

Dim sourcePath,targetPath,objFile,totalFiles,fso

Set fso = CreateObject("Scripting.FileSystemObject")
sourcePath = "C:\Documents and Settings\mdamato\Desktop\VBS"

If fso.GetFolder(sourcePath).Files.Count <= 1 Then
Wscript.Quit
End If

Set sourceFolder = fso.GetFolder(sourcePath)
targetPath = "C:\Documents and Settings\mdamato\Desktop\VBS_Old"

Dim oldestFile, oldestDate

For Each objFile In sourceFolder.Files
If oldestFile = "" Then
Set oldestFile = objFile
Else
If objFile.DateLastModified < oldestFile.DateLastModified Then
Set oldestFile = objFile
End If
End If
Next

oldestFile.Move targetPath & "\" & oldestFile.Name

Wscript.Quit

Michael J


0

Response Number 2
Name: JCH (by hurtado)
Date: June 27, 2006 at 12:16:55 Pacific
Reply:

Thanks you Michael this is incredible. Thanks for help me you are excellent. It works as I wish.


0

Response Number 3
Name: Mechanix2Go
Date: June 28, 2006 at 06:10:14 Pacific
Reply:

You could also do it with this BAT:

::== move1old.bat
@echo off
set done=N
for /f "tokens=*" %%F in ('dir /b/od') do (
call :sub1 %%F )
goto :eof

:sub1
if %done% equ Y goto :eof
set done=Y
echo move %*
goto :eof
:: DONE



=====================================
If at first you don't succeed, you're about average.

M2



0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Move the Oldest File by Last Modifi

Copy files by date modified www.computing.net/answers/programming/copy-files-by-date-modified/15932.html

To find the oldest file among the given files www.computing.net/answers/programming/to-find-the-oldest-file-among-the-given-files/18834.html

move file by date last modified in a batch www.computing.net/answers/programming/move-file-by-date-last-modified-in-a-batch/20173.html