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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

move files and folders into another folder

Status
Not open for further replies.

brlissab

Technical User
Oct 11, 2002
35
CH
hi there
here'a litlle script that i use here at work to move a directory into another folder.
didn't try on system directories, other wise on data directories it works fine.
i would log script action into a .txt file but there's a little problem. Permission denied on the .txt file for error code.
Appreciate your comments.
thanks
Code:
'Variable temps
t=Now

'Dossier a créer s'il n'existe pas
strDestination= "C:\FileMaker" & "_" & Day(Now()) & "_" & Month(Now()) & "_" & Year(Now())

'Instance en mémoire
Set fso=CreateObject("Scripting.FileSystemObject")



'Appel de la routine avec arguments
MaRoutine "C:\test", strDestination

'Désactivation instance en mémoire
Set fso=Nothing

sub MaRoutine(repSource, RepDest)
'Constante  pour ajout de données dans fichier log sans écrasement fichier existant
	Const FOR_APPENDING = 8

	'Fichier log définit par l'admin
	LogFile= "C:\Backup.log"

	'Teste existence du fichier log(s'il existe, rajoute données,cas contraire crée fichier)
	If fso.FileExists(LogFile) Then
		Set objTextStream = fso.OpenTextFile(LogFile, FOR_APPENDING)
			Else
		Set objTextStream = fso.CreateTextFile(LogFile)
	End IF
Set oFolder = fso.GetFolder(repSource)
if not fso.FolderExists(repDest) then fso.CreateFolder(repDest)
        For each oFile in oFolder.Files
                S=fso.Buildpath(RepSource,oFile.Name)
                D=fso.BuildPath(RepDest, oFile.Name)
                        On Error Resume next
                        fso.MoveFile oFile, D
                        objTextStream.WriteLine "Déplacement à " & t & "-" & oFile & ">>" & D
                                IF err.Number <> 0 Then
                                Wscript.Echo "Error Moving File!" & oFile
                                End IF
				
        Next
        For each eFolder in oFolder.SubFolders
                S=fso.BuildPath(RepSource, eFolder.Name)
                D=fso.BuildPath(RepDest, eFolder.Name)
                        MaRoutine eFolder, D
		fso.DeleteFolder eFolder
        Next
        Set oFolder = Nothing
End Sub
 
which part of the If Else End If statement for the logfile creation is being executed? what line errors? is there more than one script which is trying to access this c:\backup.log?
 
are you talking about this
Code:
objTextStream = Nothing
tried this but same result....
otherwise the error gets when
Code:
If fso.FileExists(LogFile) Then
        Set objTextStream = fso.OpenTextFile(LogFile, FOR_APPENDING)
normally this is correct(!?) but there is something that doesn't works.....
thanks for your survey
 
[1] The main reason for the error to occur is that you have a recursion and that logfile is not closed each time. You can either [1a] immediately close every time any message is written to it. If many messages are expected, it will slow down the script and stress the harddisk. Preferrably [1b] put the logfile path and open outside the maroutine, like the fso of global scope. And close it after all the job is done.

These are side comments.
[2] Do not rely on automatic conversion too much. Sometimes, it only reflects the fuzziness of scripter's idea. For instance, movefile passes two string parameters. If you pass oFile as the first, it automatically converts it to its default property .path. But, that is too subtile an alternative.
[tt] [red]'[/red]fso.MoveFile oFile, D
'use instead
fso.MoveFile oFile[blue].path[/blue], D
'or S --- otherwise what is the purpose to make an S?
'fso.MoveFile [blue]S[/blue], D
[/tt]

[3] When you control the err.number<>0, you have better to clear it. If you have multiple control within one on error resume next, you would pick up wrong message. In your case above, you don't have and that each time the execution encounters on error resume next directive, the err object would be clear. Hence you are okay there. But, in general, it's structure undesirable. Then you have to limit as much as possible the scope of the directive by on error goto 0.

[4] Put error control as closely as possible to the core function to control. In your script, the error can be either from the write or from the move.

Combining [3] and [4], it would be better script that part like this.
[tt]
objTextStream.WriteLine "Déplacement à " & t & "-" & oFile & ">>" & D
On Error Resume next
fso.MoveFile S, D
IF err.Number <> 0 Then
Wscript.Echo "Error Moving File!" & oFile[red].path[/red] 'or echo S
'or write to log as well?
'objTextStream.WriteLine "Error Moving Files! & oFile.path
err.clear
End IF
[blue]on error goto 0[/blue]
[/tt]
 
good catch tsuji, i didnt see that recursive call.

another possibility is to do a pre check before the

Set objTextStream = fso_OpenTextFile(LogFile, FOR_APPENDING)


something like

If IsNothing(objTextStream) Then

, this will prevent the error.

but, i think you are right, the objTextStream should be created/closed outside of this particular sub routine, adding another layer of extraction, the objTextStream can then be passed to this file manip sub or can be reference globally
 
>[tt]If IsNothing(objTextStream) Then[/tt]
That is a good use too. Only there is no parallel to isnull, isarray etc for nothing (I use the same method to memorize it and sometimes write it too quickly as well.) It is this.
[tt]If objTextStream [blue]is nothing[/blue] Then[/tt]

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top