Jump to content

REQUEST: Script 2 copy from multiple source folders 2 one folder BUT keep permissions


Recommended Posts

Posted

As srochford has kindly sorted my other script, I can now move all of the students MP3 out of their home drives to a shared folder.

 

I want to move all MP3`s that students have from any folder in their homedrives ( N: ) to a single folder on the common drive. e.g (O:\Student Music\Year 9)

 

I need to also copy the permissions (or just owner), so that my other new script can then rename the files from maybe mysong.mp3 to 09Jack.Jones_mysong.mp3

 

Ive found that xcopy can copy the permissions with the /o switch. and xxcopy can copy from multiple directories to a single folder. But both cant do both (i think)

 

Can anyone please suggest a way I can do this?

 

Thanks in advance

Posted
Why you need to copy multiple directories? I've not looked at your other script but i imagine it would recursively go throught all folders and rename all the mp3. Could you not just move it after its been renamed or even copy it to the desired location but with the desired name. This way you wouldnt need to copy the permissions.
Posted

Hi mate,

Students will hide there MP3 collection in eg. N:/school work/ict/homework/dont go in here/music.....

 

I want to be able to move them from all these various directories to one central location. For the other script that renames it needs to read the owner of the file.

If I copy it would replace me as the owner. If i was to rename first, then when they are moved it would then ammend myself as the owner when the script next runs.

Posted

Think I'm being abit slow today.. put it down to the Mondays.

 

From what you said, theres an issue with renaming it first then moving it because when its moved you are the owner and therefore when the script runs again your name will be used.. why does the rename script need to be run twice?

 

The way i say it this is what should happen:

 

For all files in directory

if .mp3 then

copy file to desired location (process of coping will rename file e.g. copy file.mp3 O:\directory\newfile.mp3)

Delete file

end if

loop

 

Thats what i think it should do.. atleast in pseudo.

 

maybe i could look at your other script and i could adapt that one.

Posted

hehe,

 

The rename Script will be run regularly in that share, as in the future the only place that they will be able to save MP3 files to will be the new share.

So they copy a file into there and it then gets renamed. This script will only be run once to MOVE all their current MP3 files.

 

Thanks

Tim

Posted

Ah right ok, i didnt get why you needed to run the rename script more then once. No probs, the pseudo should look something like this then:

 

for all files in directory

if .mp3 then

xcopy file to desired location (i think xcopy keeps the permissions intact)

delete file

end if

loop

 

Is this what you want to happen?

Posted
Correct thankyou :)

 

So you would be using xcopy in a batch script?

 

Im more a vbs guy so this is the script i'd probably use:

 

strSourceFolder = "" 'eg d:\users\students
strDestFolder = "" 'eg d:\share
strLocationXcopy = "" 'eg c:\xcopy.exe

set objFSO = createobject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")

GetFiles strSourceFolder

sub GetFiles(byval strDirectory)
set objFolder = objFSO.GetFolder(strDirectory)
for each objFile in objFolder.Files
	If right(objFile.path,3) = "mp3" Then
		wshShell.run strLocationXcopy & " '" & objFile.path "' " & "'" & strDestFolder & "' /o"
	End If 
Next	
for each objFolder in objFolder.SubFolders
	GetFiles objFolder.Path
next
end sub

 

btw its untested so please test it first before you run it for real.

  • Thanks 1
Posted (edited)

great stuff !! thankyou for trying this for me, I dont know where id be without all you edugeek scripting experts!

 

Unfortunetly im getting:

 

Script: C:\test.vbs

Line: 14

Char: 56

Error: Expected end of statement

Code: 800a0401

Source: MS VBScript Compilation Error

 

hope this means more to you than it does to me!!! :)

 

Heres the exact code I used:

 

strSourceFolder = "\\nas\Student_Drives\05\teststudent\My Music" 'eg d:\users\students
strDestFolder = "\\nas\Student_media\Music Files" 'eg d:\share
strLocationXcopy = "c:\xcopy.exe" 'eg c:\xcopy.exe

set objFSO = createobject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")

GetFiles strSourceFolder

sub GetFiles(byval strDirectory)
set objFolder = objFSO.GetFolder(strDirectory)
for each objFile in objFolder.Files
	If right(objFile.path,3) = "mp3" Then
		wshShell.run strLocationXcopy & " '" & objFile.path "' " & "'" & strDestFolder & "' /o"
	End If 
Next	
for each objFolder in objFolder.SubFolders
	GetFiles objFolder.Path
next
end sub

Edited by ChrisH
Posted

You are missing an ampersand

 

			wshShell.run strLocationXcopy & " '" & objFile.path "' " & "'" & strDestFolder & "' /o"

 

should be

 

			wshShell.run strLocationXcopy & " '" & objFile.path & "' " & "'" & strDestFolder & "' /o"

 

its the bit after objFile.path

  • Thanks 1
Posted

Were getting somewhere :) thankyou

Now getting lots of cmd windows opening and closing, but they are all failing with

"Invalid Number of parameters"

 

Nothing gets copied :(

Posted
The command line is wrong then try doing it manually from the the command prompt. Does the "/o" want to got after strLocationXCopy?
Posted

Also I never like concatenating a string in the run statement, especially when dealing with cmdline apps. try

If right(objFile.path,3) = "mp3" Then
strCmdLine = strLocationXcopy & " '" & objFile.path "' " & "'" & strDestFolder & "' /o"
wshShell.run  strCmdLine
End If 

Posted
The command line is wrong then try doing it manually from the the command prompt. Does the "/o" want to got after strLocationXCopy?

 

XCOPY source [destination] option

 

xcopy "\\nas\Student_Drives\05\teststudent\My Music" "\\nas\Student_media\Music Files" /o

 

works fine.... must be a problem with the script (i think)

Posted (edited)

Lets try a little debugging to see if any paths look wrong or anything. This will pop up a box and not do any copying.

 

strSourceFolder = "\\nas\Student_Drives\05\teststudent\My Music" 'eg d:\users\students
strDestFolder = "\\nas\Student_media\Music Files" 'eg d:\share
strLocationXcopy = "c:\xcopy.exe" 'eg c:\xcopy.exe

set objFSO = createobject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")

GetFiles strSourceFolder

sub GetFiles(byval strDirectory)
set objFolder = objFSO.GetFolder(strDirectory)
for each objFile in objFolder.Files
	If right(objFile.path,3) = "mp3" Then
		strCmdLine = strLocationXcopy & " '" & objFile.path & "' " & "'" & strDestFolder & "' /o"
Wscript.echo strCmdLine
'wshShell.run  strCmdLine
	End If 
Next	
for each objFolder in objFolder.SubFolders
	GetFiles objFolder.Path
next
end sub

Edited by ChrisH
Posted
Look at my second post and try that sometimes it just works doing it that way.

 

Popups appear alot faster than before but still contain "Invalid Number of parameters"

 

Also did you cut and paste from original post and not mean to miss out the & after objFile.path ?

 

Thanks

Tim

Posted (edited)

It looks good apart from it has single ' instead of " on the file paths? is that allowed?

 

Output:

c:\xcopy.exe '\\nas\Student_Drives\05\teststudent\My Music\Skindred - Nobody.mp3' '\\nas\Student_media\Music Files' /o

Edited by burgemaster
Posted

Try this line instead

strCmdLine = strLocationXcopy & " " & chr(34) & objFile.path & chr(34) & " "  & chr(34) & strDestFolder & chr(34) & " /o"

Posted

Chris thanks so much !!

This is now 99% working !!!!

 

It is copying all the files perfectly, but it is NOT copying the permissions over....

It is copying the "Owner" which i guess is the /o switch.

 

MS say "To preserve permissions when files and folders are copied or moved, use the Xcopy.exe utility with the /O or the /X switch"

I have also tried swapping to X, still no good :(

 

But I think the scripting part is doing its job!

Thanks again

 

Script now:

 

strSourceFolder = "\\nas\Student_Drives\05\teststudent\My Music" 'eg d:\users\students

strDestFolder = "\\nas\Student_media\Music Files\" 'eg d:\share

strLocationXcopy = "c:\xcopy.exe" 'eg c:\xcopy.exe

 

set objFSO = createobject("Scripting.FileSystemObject")

Set WshShell = WScript.CreateObject("WScript.Shell")

 

GetFiles strSourceFolder

 

sub GetFiles(byval strDirectory)

set objFolder = objFSO.GetFolder(strDirectory)

for each objFile in objFolder.Files

If right(objFile.path,3) = "mp3" Then

strCmdLine = strLocationXcopy & " " & chr(34) & objFile.path & chr(34) & " " &chr(34 )& strDestFolder & chr(34) &" /o" &" /x"

wshShell.run strCmdLine

End If

Next

for each objFolder in objFolder.SubFolders

GetFiles objFolder.Path

next

end sub

Posted
I prefer robocopy to preserve permissions.

 

I think it must be my permissions on the destination folder as it works perfectly when set to: C:\files

 

Thanks for all your help mate!!!

Posted

Nice one ChrisH.. debugged my buggy script. Sorry burgemaster, i should have checked it for ya and i did wonder if the command would accept ' instead of ", my bad. FYI permission will move fine if its drive to drive on the same machine, you dont even need to use xcopy to do it e.g. vbs command objfso.movefile could work. I dont think this would make a difference but you could try mapping the drives.

 

ChrisH does have a good point about RoboCopy.

  • Thanks 1

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