Recycle File

Just starting out? Need help? Post your questions and find answers here.
MikeB
Enthusiast
Enthusiast
Posts: 183
Joined: Sun Apr 27, 2003 8:39 pm
Location: Cornwall UK

Recycle File

Post by MikeB »

I wrote a text file viewer a couple of years ago (with PB) to display the file without letting it get modified by anyone viewing it as would be the case if viewed by an editor. (Edits require a password).

Anyway when 'edit' is selected the file is passed to the editor (Jarte) then passed to another PB prog that makes a few modifications which are mainly cosmetic to correct grammar etc..

Now the problem bit -

The modify program checks to see if a backup exists and if it does it calls 'RecycleFile()' to send it to the recycle bin, then copies the existing file with '.back' appended to my backup folder, deletes the existing file and saves the new modified file.

If I run the modify program on it's own there is no problem, but when it is called from the viewer with 'runprogram()' I get an error message saying that it cannot delete the file as it can't open it. Since it is called with 'runprogram()' I have to compile the programs to test them and debugging consists of placing messagerequesters at strategic points to see where the problem is. In this way I have tracked it to my 'RecycleFile()' bit.

Code: Select all

Procedure.l Exist(File$)                 ;- Check a drive + file exists, without system requesters etc. 
	; Check if a drive + file exists 
	; Returns   1 if exists, else 0 
	
	Protected EFlag.l, OldErrorMode.l, Junk.l 
	
	OldErrorMode = SetErrorMode_(1)   ; Turn off screen error messages 
	If GetFileAttributes_(@File$)=-1  ; Get file butes. -1 = fail 
		Junk.l=GetLastError_()          ; Get last error, to flush system 
		SetLastError_(0)                ; Set error to zero 
		EFlag.l = 0                     ; Return value to flag FAIL 
	Else 
		EFlag.l = 1                    ; Return value to flag a PASS 
	EndIf 
	SetErrorMode_(OldErrorMode)       ; Reset the error flags 
	ProcedureReturn EFlag 
EndProcedure 
;
Procedure RecycleFile(file$)  
        SHFileOp.SHFILEOPSTRUCT
        SHFileOp\pFrom=@file$
        SHFileOp\wFunc=#FO_DELETE
        SHFileOp\fFlags=#FOF_ALLOWUNDO | #FOF_NOCONFIRMATION ; without #FOF_NOCONFIRMATION you get a messagerequester asking to confirm sending to recycle bin
        SHFileOperation_(SHFileOp)
EndProcedure
;
NewName$=backuploc$+nameonly$+".bak"
; from here the files start getting overwritten, deleted and changed
If Exist(NewName$) ;  COMMENT NEXT LINE OUT AND NO PROBLEM
	null=RecycleFile(NewName$)         ; last backup version is deleted by sending to recycle bin
EndIf
The funny part is that as I said I wrote this a couple of years ago, it is only since modifying for PB4 that the problem has come about.

The other funny bit is that although I get the message saying it can't open the file, it actually works! The new file is where it should be, the old file becomes a '.bak' file and the old '.bak' goes to the recycle bin.

Any ideas as to why I get the error message when there appears to be no error would be appreciated.
Mike.
(I'm never going to catch up with the improvements to this program)
User avatar
Michael Vogel
Addict
Addict
Posts: 2799
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Post by Michael Vogel »

Hm,

seems to work all fine here - I just added...

Code: Select all

backuploc$="C:\"
nameonly$=ProgramParameter()
MessageRequester("?",backuploc$+" - "+nameonly$)

NewName$=backuploc$+nameonly$+".bak"
; from here the files start getting overwritten, deleted and changed
If Exist(NewName$) ;  COMMENT NEXT LINE OUT AND NO PROBLEM
   null=RecycleFile(NewName$)         ; last backup version is deleted by sending to recycle bin
EndIf 
...to your code and compiled it to trash.exe

Then...

Code: Select all

RunProgram("Trash.exe","Test",".")
...was compiled to RunTrash.exe

And it works, the Test.bak file has been moved to the recycle bin - ok, I had both exes in the same directory, the file path was fixed to C:\ to keep it simple but maybe you should check also these things in your app (maybe with a messagerequester)...
MikeB
Enthusiast
Enthusiast
Posts: 183
Joined: Sun Apr 27, 2003 8:39 pm
Location: Cornwall UK

Post by MikeB »

Thanks for the help.

I have discovered (I think) that the problem here was that the file was still open in the calling program as far as the system was concerned. As I said it worked if called on it's own.

I also realised that I was using a bit of overkill in that I was actually sending the last backup to the recycle bin, then backing up the edited file before modifying and saving the edited file. I did this in case of disaster with the modify bit, which has never gone wrong. So I have altered it to backup the original file then edit it, modify it and save it.

Although this has fixed the problem and now makes a more sensible backup of the original, I now cannot be certain as to what was the cause of the problem. Since it has worked until the last few weeks makes it the more weird.

By the way, changing old programs to PB4 has made me realise that time spent adding comments is never wasted. I sometimes wonder how I ever managed to write code that later takes 2 hours to comprehend a 20 line procedure that I probably wrote in 10 minutes!
Mike.
(I'm never going to catch up with the improvements to this program)
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Recycle File

Post by Kwai chang caine »

Hello

Is it possible to have the path of Recycle ??? :roll:
Because i use ReceiveNetworkFile and i want have a choice to not accept the file...
If the choice is "NO"...i have the idea to put the file not wanted directly in the recycle :D
ImageThe happiness is a road...
Not a destination
PureLust
Enthusiast
Enthusiast
Posts: 477
Joined: Mon Apr 16, 2007 3:57 am
Location: Germany, NRW

Re: Recycle File

Post by PureLust »

Kwaï chang caïne wrote:Is it possible to have the path of Recycle ??? :roll:
I don't think there is a path to "THE" Recycle-Dir, because each Drive has it's own one.
Furthermore I think that a Recycle-Entry needs some more information than just a file (e.g. the information where it should restore to (if requested by user)).

Why not just delete the file? Even when it's in the bin it still needs Space on the HD. :wink:
[Dynamic-Dialogs] - create complex GUIs the easy way
[DeFlicker] - easily deflicker your resizeable Windows
[WinFX] - Window Effects (incl. 'click-through' Window)
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Recycle File

Post by Kwai chang caine »

Yes you have right....

I wanted just put the file in a not important place and deletefile just after.
So, i have choose the "Temp" .

Thanks for your answer 8)
ImageThe happiness is a road...
Not a destination
PureLust
Enthusiast
Enthusiast
Posts: 477
Joined: Mon Apr 16, 2007 3:57 am
Location: Germany, NRW

Re: Recycle File

Post by PureLust »

Hi again, ...

maybe you are interested in deleting a file to the Recycle-Bin.
If so - i just found the following code in the CodeArchive: :wink:

Code: Select all

Procedure.l RECYCLEFile(File$); - Delete a file and move it in the RECYCLE-Bin  
  SHFileOp.SHFILEOPSTRUCT  
  SHFileOp\pFrom=@File$  
  SHFileOp\wFunc=#FO_DELETE  
  SHFileOp\fFlags=#FOF_ALLOWUNDO  
  ProcedureReturn SHFileOperation_(SHFileOp)  
EndProcedure
Greetz, PL.
[Dynamic-Dialogs] - create complex GUIs the easy way
[DeFlicker] - easily deflicker your resizeable Windows
[WinFX] - Window Effects (incl. 'click-through' Window)
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Recycle File

Post by ts-soft »

to delete more as one file you can use this:
http://www.purebasic.fr/english/viewtop ... 20#p290420
The Procedure "FilesDelete(Array sources.s(1), flags = #FOF_NOCONFIRMATION | #FOF_NOERRORUI)"
do the trick :wink:

Greetings
Thomas
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Recycle File

Post by Kwai chang caine »

Thanks a lot at you two 8)
ImageThe happiness is a road...
Not a destination
Post Reply