Jump to content

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


Recommended Posts

Posted (edited)

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

Edited by burgemaster
Posted (edited)

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

Edited by burgemaster
Posted

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.

 

'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

  • Thanks 1
Posted

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!!

Posted

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 ... :)

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...