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

Move file and change it's attributes 1

Status
Not open for further replies.

theniteowl

Programmer
May 24, 2005
1,975
US
I wrote a script to move files from one of our Solaris servers to a Windows server but the files are coming across with the read only attribute set.
Is there a way to set that attribute at the same time I move the file or do I have to iterate through all the files after I have moved them to change the attributes?
I am using the Move command for the FileSystemObject.


At my age I still learn something new every day, but I forget two others.
 
What is your actual code ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Code:
'***************************************************************************************************
'* This script will check all files listed in the folder indicated by SrcFolder and if they are older than the number of 
'* seconds indicated in SecondsOld the files will be moved to the destination indicated in DestFolder.
'***************************************************************************************************
Option Explicit
Dim SrcFolder, DestFolder, RightNow, SecondsOld, FileExt, objFSO, refFolder, refFile, objFileCopy, LastModified, ErrMsg
Dim outstr, objTextFile

'######  Set user defined variables here.  ######
SrcFolder = "J:\"                   ' Source folder to look for files
DestFolder="\\servername\job\"  ' Destination to move files to
FileExt="jrf"                       ' File extension of files to process
SecondsOld=30                       ' Number of seconds old file must be before it can be moved
Const ForAppending = 8

On Error Resume Next       
If Right(DestFolder, 1) <> "\" Then DestFolder = DestFolder & "\"  ' Make sure DestFolder ends with a back slash
RightNow=Date & " " & Time    ' Current Date and Time to use to compare against files DateLastModified property
Set objFSO = CreateObject ("Scripting.FileSystemObject")
  Set refFolder = objFSO.GetFolder(SrcFolder)
      If Err.Number Then
        ErrMsg = Now() & " Could not open source folder.  VB Error: " & Err.Description & vbCrLf
		WriteLog(ErrMsg)  ' Write error message to log file and then quit script
      End If
    'Process files in directory
    For Each refFile in refFolder.Files    ' Loop through all files in folder
      If Err.Number Then
        ErrMsg = Now() & " Lost access to files.  VB Error: " & Err.Description & vbCrLf
        WriteLog(ErrMsg)  ' Write error message to log file and then quit script
      End If
      LastModified = (refFile.DateLastModified)  ' DateLastModifed is in format of  MM/DD/YYYY HH:MM PM
      If ((DateDiff("s",LastModified,RightNow)) > SecondsOld) Then    ' If number of seconds since file was last modified exceeds SecondsOld
        Set objFileCopy = objFSO.GetFile(refFile.Path)   ' Create an object pointing to current file
          If LCase(Right(objFileCopy.Path, 3)) = FileExt Then  ' If the last three letters of the file name match FileExt
            objFileCopy.Move (DestFolder)  ' Move the file to the folder indicated by DestFolder
            If Err.Number Then
              ErrMsg = Now() & " Could not write file to " & DestFolder & ".  VB Error: " & Err.Description & vbCrLf
              WriteLog(ErrMsg)  ' Write error message to log file and then quit script
            End If
          End If
      End If
    Next
  Set refFolder = Nothing
Set objFSO = Nothing


Function WriteLog(outstr)
  Err.Clear  ' Clear existing error so that new errors in writing to log file can be detected.
  outstr = outstr & vbCrLf & vbCrLf
  ' If log file does not exist then create it
  If Not objFSO.FileExists("ErrorLog.txt") Then
    Set objTextFile = objFSO.CreateTextFile("ErrorLog.txt")
	objTextFile.Close
  End If
  'Open the log file.
  Set objTextFile = objFSO.OpenTextFile("ErrorLog.txt", ForAppending)
    If Err.Number Then
      'If error occured then log file could not be written to and script will abort.
      Set objFSO = Nothing
      Wscript.Quit
    End If
    objTextFile.Write outstr
    objTextFile.Close
  Set objTextFile = Nothing
  Set objFSO = Nothing
  Wscript.Quit
End Function

At my age I still learn something new every day, but I forget two others.
 
You may try to replace this:
objFileCopy.Move (DestFolder)
with this:
If objFileCopy.Attributes And 1 Then
objFileCopy.Attributes = objFileCopy.Attributes - 1
End If
objFileCopy.Move DestFolder

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
The Unix box folder will not allow me to change the permissions BUT I was able to do it after the move.
I had originally thought I would have to open a new connection to the newly moved file which seemed a dirty way to do things but in fact the object reference was maintained after the move so I just executed your If statement above after the move.

Thanks PH.

At my age I still learn something new every day, but I forget two others.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top