Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I move folders by last modified

Status
Not open for further replies.

borsker

Programmer
Jul 24, 2002
147
US
I am looking to move folders and files that were last modified 2 weeks ago to a backup location. However The folders are listed as job numbers so the folders always have different names. I need to move folders and files out when the server gets to crowded. How can I automate this process. I was going to use these commands, however this only lets me move folders I know the names of.

ofs = CreateObject("Scripting.FileSystemObject")
ofs.CopyFolder("C:\temp1\stuff", "C:\temp2\")
ofs.deletefolder("C:\temp1\stuff")
 

Use ADIR() to pick-up all the folder names into an array. And the third paramter gives the last modified date.

Code:
PUBLIC ARRAY myarray[1]
ADIR(myarray,'',"D")

Why not use the MoveFolder()

Code:
ofs = CreateObject("Scripting.FileSystemObject")
ofs.MoveFolder("C:\temp1\stuff", "C:\temp2\")




Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
Well these commands worked

Code:
PUBLIC ARRAY myarray[1]
ADIR(myarray,'',"D")
ofs = CreateObject("Scripting.FileSystemObject")
ofs.MoveFolder("C:\temp1\stuff", "C:\temp2\")

however when I tried to move a folder from the network I got some kind of unknown COM status code error.

Code:
ofs = CreateObject("Scripting.FileSystemObject")
ofs.MoveFolder("N:\temp1\stuff", "C:\temp2\")

Do I have to use a different command when moving form a network drive?

 
Code:
DECLARE INTEGER MoveFile IN kernel32;
     STRING lpExistingFileName,;
     STRING lpNewFileName

MoveFile("N:\temp1\stuff", "C:\temp2")

...this will move files or folders. Also, if the folder you have specified for lpNewFileName doesn't exist it will create it.

boyd.gif

 
Craig I get no response when I use these commands. Do I need to have a higher version of VFP I have 6.0.
 
borsker,

No, this is an API call and is only dependent on kernel32.dll being present, and if you are running Windows 95 or greater then it is definately present. There must be something else wrong, such as file/folder permissions, file or folder in use, or something that is misspelled otherwise incorrect.

Start with a simple test... create a folder called temp1 on your C, put a file in it and using the code above move the folder from temp1 to temp2 or something. If that works then continue to try different things until you isolate what exactly is causing the problem. The code posted above will work as advertised barring the above listed problems.

boyd.gif

 
If fact, you may want to add a result variable and check it's result. i.e.
Code:
Local lnResult
DECLARE INTEGER MoveFile IN kernel32;
     STRING lpExistingFileName,;
     STRING lpNewFileName

lnResult = MoveFile("N:\temp1\stuff", "C:\temp2")
IF lnResult <> 0
   DECLARE INTEGER GetLastError IN kernel32
   lnError = GetLastError()
   DO CASE
   CASE lnError = ??
   ...
   ENDCASE
ENDIF
Note: After reading deeper into the MoveFile() API, it appears you can only "move" bettween volumes (drives) if you use MoveFileEx() with the Copy_Allowed flag set.

Rick

 
unfortunately I am still not getting it. I am getting no error and the code looks like it is working correctly, but no results. It is like I am not even trying anything. I went back and used the copy and delete method. It is a little more code but it seems to do the trick. I am also new to this and without someone to teach it to me I am kind of on my own, so thanks to you guys I have someone to get feedback and answers from. Thank you for all of your help and if you can see an easier way than the way I am going please feel free to correct my code I am sure you guys would think it is quite primitive.

Code:
set defa to N:\Temp1
store date()-60 to cToday
PUBLIC ARRAY myarray[1]
nCount = ADIR(myarray,'',"D")
FOR i = 1 TO nCount
     cFileName = myarray(i,1) 
     cFileName2 = myarray(i,3) 
	if cfilename2=<cToday
		store alltrim(cmonth(cfilename2))+'_'+alltrim(str(year(cfilename2))) to ctoday2
		set defa to c:\Temp2
		PUBLIC ARRAY myarray2[1]
		nCount2 = ADIR(myarray2,'',"D")
		store 0 to gonow
		FOR r = 1 TO nCount2
     		cFileName99 = myarray2(r,1)
			if upper(cfilename99)=upper(cToday2)
				store 1 to gonow
			endif
		next
		if gonow=0
			ofs2 = CreateObject("Scripting.FileSystemObject")
			store 'ofs2.CreateFolder("c:\Temp2\'+alltrim(today2)+'")' to createfolder
			&createfolder
		endif
		ofs = CreateObject("Scripting.FileSystemObject")
		store 'ofs.CopyFolder("N:\Temp1\'+alltrim(cFileName)+'", "C:\Temp2\'+alltrim(cToday2)+'\")' to movestuff 
     	&movestuff
		store 'ofs.deleteFolder("N:\Temp1\'+alltrim(cFileName)+'")' to deletestuff 
		&deletestuff
	endif
	
next
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top