+ Post New Thread
Results 1 to 5 of 5
Scripts Thread, SOLVED: Rename Script that ammends the "Owner" to the start of the filename in Coding and Web Development; As per title I am looking for a rename script that will ammend the ntfs "Owner" to the start (or ...
  1. #1

    Join Date
    Aug 2007
    Posts
    851
    Thank Post
    106
    Thanked 66 Times in 47 Posts
    Rep Power
    27

    SOLVED: Rename Script that ammends the "Owner" to the start of the filename

    As per title I am looking for a rename script that will ammend the ntfs "Owner" to the start (or end) of the filename.

    Eg.

    my_mp3_file.MP3 ----> 06John.Jones_my_mp3_file.MP3

    Problem:
    Students are abusing there network homedrive and filling with MP3 files. I cannot disbale all MP3 as they do legitimately use MP3 for blogs, music, ict work etc....
    I am planning to delete all MP3 from thier home drives and then ban mp3`s to be copied there.

    I will then create a common area for students to copy thier MP3 files to that they require for work. I would schedule this every 10min or so to rename any files that they copy to the share. They would not be able to rename delete etc only read.

    Can anyone please help? Im pretty dreadfull in VBS !!
    To make it harder, It will also have to not rename any files that already have been renamed on the last sweep! So if the filename allready has the owner then do not rename.

    Thanks in advance

    To start ive found this script that reads the owner of any files in "C:\Scripts" and puts it into a popup:

    Set objShell = CreateObject ("Shell.Application")
    Set objFolder = objShell.Namespace ("C:\Scripts")
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Dim arrHeaders(13)
    For i = 9 to 10
    arrHeaders(i) = objFolder.GetDetailsOf (objFolder.Items, i)
    Next
    For Each strFileName in objFolder.Items
    For i = 9 to 10
    If i <> 9 then
    Wscript.echo arrHeaders(i) _
    & ": " & objFolder.GetDetailsOf (strFileName, i)
    End If
    Next
    Next
    Last edited by burgemaster; 13th June 2010 at 10:03 PM.

  2. #2

    Join Date
    Aug 2007
    Posts
    851
    Thank Post
    106
    Thanked 66 Times in 47 Posts
    Rep Power
    27
    Just also thought, I could use this script also to run on the "Staff Share" in the "School Photos" folders.

    Staff are using all their quotas but have not idea which files they copied to the network over the years.

    Ive found this script that will Append the Date to the end of a file, if it allready has the date it doesnt touch it, so could this and the above be made into one script maybe?


    Dim file1, file2

    file1 = "C:\scripts\newfile.csv"
    ' file2 = "C:\scripts\newfile" & year(date) & month(date) & Day(Date) & ".txt"
    file2 = "C:\scripts\newfile_" & Year(Date) & Right("0" & Month(Date),2) & Right("0" & Day(Date),2) & ".csv"
    Set fso = CreateObject("Scripting.FileSystemObject")

    If fso.FileExists(file1) = true then
    If fso.FileExists(file2) = true then
    fso.DeleteFile(file2)
    End if
    fso.MoveFile file1, file2
    'else
    ' msgbox "File does not exist"
    End If

    Set FSO = Nothing
    Last edited by burgemaster; 13th June 2010 at 11:41 AM.

  3. #3

    Join Date
    Aug 2005
    Location
    London
    Posts
    3,159
    Thank Post
    116
    Thanked 529 Times in 452 Posts
    Blog Entries
    2
    Rep Power
    125
    Try the code below (you should be able to use your version with GetDetailsOf but I can't get it to work reliably (and it also returns the domain as part of the owner so that needs stripping out)

    How many people are likely to use this and how many files each? If the answer is "lots" and "lots" then might want to consider putting the files into folders for each user.

    Code:
    'make script continue if errors (will get errors if file open)
    on error resume next
    'folder to process - make sure it has trailing \
    sRoot="c:\temp\elonex\"
    'connect to WMI on local computer (\\.\)
    Set oWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
    'connect to filesystemobject and access folder to process
    set oFSO=createobject("scripting.filesystemobject")
    set oFolder=ofso.getfolder(sRoot)
    'step through each file in turn
    for each oFile in oFolder.files
      'get the owner
      sOwner=GetFileOwner(sRoot & oFile.name)
      'has this file already been renamed
      if left(oFile.name,len(sOwner))<>sOwner then
        'no, so work out the new name
        sNewName=sRoot & sOwner & "_" & ofile.name
        'and rename it
        ofso.movefile sRoot & ofile.name , sNewName
      end if
    next
    
    Function GetFileOwner(sFile)
      'use WMI to find the owner of the file
      Set colItems = oWMIService.ExecQuery("ASSOCIATORS OF {Win32_LogicalFileSecuritySetting='" & sFile & "'}" _
      & " WHERE AssocClass=Win32_LogicalFileOwner ResultRole=Owner")
      'WMI always returns a collection so get the item from it (will only ever be 1!)
      For Each oItem in colItems
        'and return it to main prog
        GetFileOwner = oItem.AccountName
      Next
    End Function

  4. Thanks to srochford from:

    burgemaster (13th June 2010)

  5. #4

    Join Date
    Aug 2007
    Posts
    851
    Thank Post
    106
    Thanked 66 Times in 47 Posts
    Rep Power
    27
    You are a star !!!!!

    Just tested and its working perfectly!
    I think that I will just devide the MP3 folder in the common drive into Year groups. I will also only allow students to enter their year group folder.

    Not sure if the ICT teachers will like this as sometimes they send work off to be marked and if their webpage links to a drive other than the website root it wont work.
    Maybe I could make the mp3 folder accessable on the internet? and get them to link to it from a weblink?

    ANYWAY... THANKS AGAIN MATE!!

  6. #5
    rh91uk's Avatar
    Join Date
    Sep 2008
    Location
    UK
    Posts
    877
    Thank Post
    137
    Thanked 132 Times in 114 Posts
    Rep Power
    36
    Quote Originally Posted by burgemaster View Post
    Maybe I could make the mp3 folder accessable on the internet? and get them to link to it from a weblink?
    Something you could use WebDav for? Just a thought ...



SHARE:
+ Post New Thread

Similar Threads

  1. Replies: 3
    Last Post: 10th May 2010, 11:59 AM
  2. Replies: 3
    Last Post: 11th September 2009, 10:01 AM
  3. make owner same as filename.
    By madcyryl in forum Scripts
    Replies: 0
    Last Post: 16th June 2009, 04:31 PM
  4. Word "normal.dot" deployment script
    By contink in forum Scripts
    Replies: 0
    Last Post: 17th October 2006, 09:33 AM
  5. Replies: 6
    Last Post: 28th September 2006, 08:06 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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