Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Nov 2007
    Posts
    680
    Thanks
    319
    Thanked 1 Time in 1 Post

    "file not found" - shouldn't be looked for...

    I have the below code, the files are being moved from the root folder (not adding to the database for some reason but come to that issue when this one is fixed).

    The last error check is being triggered and I don't know why as there is only 3 images in the folder and they are all being moved...

    ASP
    Code:
    function move_files(root,whereto,upnum,datetime,modified)
    	on error resume next
    	'Prepare variables and objects
    	dim fs, folder, file, item
    	
    	'Create scripting object and open root folder
    	set fs = CreateObject("Scripting.FileSystemObject")
    	set folder = fs.GetFolder(root)
    	
    	'Move the files in the root first
    	for each item in folder.files
    		'Move the file
    		fs.MoveFile folder&"\"&item.name, whereto
    		response.write item.name&"<br />"
    		
    		if err.number <> 0 then
    			response.write "Error: Upload stopped (1), error code: "&err.description&" | "&item.name
    			'response.end
    		else
    			'File moved, add to database
    			sql="'" & item.name
    				sql=sql & "'," & upnum
    				sql=sql & ",0,"&datetime
    				sql=sql & ","&modified
    
    			'Add to database
    			handle = add_to_database("images","jpg,img_set,sold,upload_date_time,taken",sql)
    			if handle = true then
    				response.write "Error: Upload stopped (2), error adding to database: " & handle
    				'response.end
    			end if
    		end if
    	next
    	
    	'Now search through the sub folders and move the files
    	if folder.SubFolders.count > 0 then
    		response.write "true <br />"
    		for each item in folder.SubFolders
    			'Open new foler
    			set subfolder = fs.GetFolder(root&item.name)
    			for each subfile in subfolder.files
    				
    				'Move the files
    				'fs.MoveFile subfolder&"\"&subfile.name, whereto
    				
    				'Check for errors
    				if err.number <> 0 then
    					response.write "Error: Upload stopped (3), error code: "&err.description
    					response.end
    				else
    					'File moved, add to database
    					sql="'" & subfile.name
    						sql=sql & "'," & upnum
    						sql=sql & ",0,"&datetime
    						sql=sql & ","&modified
    	
    					'Add to database
    					handle = add_to_database("images","jpg,img_set,sold,upload_date_time,taken",sql)
    					if handle = true then
    						response.write "Error: Upload stopped (4), error adding to database: " & handle
    						response.end
    					end if
    				end if
    			next
    		next
    	end if
    	
    	'Check for errors
    	If Err.Number <> 0 Then
    		move_files = "Error: Unexpected error: "&err.description
    	End If
    end function
    
    result = move_files("D:\DCIM\","D:\Moved\",0,#12/07/2013 5:12#,#12/07/2013 2:12#)
    response.write result
    Error
    Code:
    Error: Unexpected error: File not found
    If I don't comment out the "response.end" statements only 1 image is moved at a time, have to reload for another one to be moved. So there is an error somewhere in the following loop

    Code:
    'Move the files in the root first
    	for each item in folder.files
    But the "response.write" statements within that loop are not being triggered at all...

  • #2
    Regular Coder
    Join Date
    Nov 2007
    Posts
    680
    Thanks
    319
    Thanked 1 Time in 1 Post
    Solved. I thought that the "item" is stored in memory whilst it is running the code for the current item, but if you refer back to "item" is seems to search for it inside the folder again.

    Code:
    for each item in folder.files
    Solved it like below:
    Code:
    for each item in folder.files
         itemTMP = item

  • Users who have thanked martynball for this post:

    Old Pedant (07-17-2013)

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,028
    Thanks
    75
    Thanked 4,324 Times in 4,290 Posts
    Nice catch! I wouldn't have realized that was a problem, either!

    Kudos.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •