Link to home
Start Free TrialLog in
Avatar of VillaMaria
VillaMaria

asked on

Script to give a list of all MP3 files in a user folder and record a list to a central depository before deleting

Hello all,

I need help with a little project that I am working on. I need to make a script (can be Powershell, batch, or vbscript) that scans the userfolder of a user's computer for all music and video files. It must report the location of files to a central repository and then delete them.

I have a few batches that do this, but not all in one script. We use Kaseya for our computer management, so I would like to use it to deploy and execute the script.

Please note, the computer name is the same as the username of the user that would be logged in to the computer.

Scripts I have now,
*this one does not work right. it sometimes will give me the directory of the user's network user folder rather than the user folder on the computer*

cd c:\users\%username%
dir /s *.avi >> \\servername\students\logs\%username%-avi.txt
dir /s *.mov >> \\servername\students\logs\%username%-movi.txt
dir /s *.MP3 >> \\servername\students\logs\%username%-mp3.txt
dir /s *.M4A >> \\servername\students\logs\%username%-m4a.txt

*This one works, but I cannot figure out how to add a results file and it scans the entire hard disk*
for /f "tokens=*" %%j in ('dir /B /s *.mp3') do del /Q "%%j"
for /f "tokens=*" %%j in ('dir /B /s *.mp4') do del /Q "%%j"
for /f "tokens=*" %%j in ('dir /B /s *.m4a') do del /Q "%%j"
for /f "tokens=*" %%j in ('dir /B /s *.swf') do del /Q "%%j"
for /f "tokens=*" %%j in ('dir /B /s *.avi') do del /Q "%%j"
for /f "tokens=*" %%j in ('dir /B /s *.mov') do del /Q "%%j"


I suppose I could try combining the two into a single batch, but there has to be something more elegant.

Thanks.
Avatar of Krzysztof Pytko
Krzysztof Pytko
Flag of Poland image

I would suggest POwerShell script for that. You can do that using this syntax

Get-ChildItem -Path C:\ -Recurse -Include *.mp3,*.mp4,*.m4a,*.swf,*.avi,*.mov | Select name,Directory | Export \\UNCShare\output.csv

for logging

and then to remove them

Get-ChildItem C:\ -recurse -include *.mp3,*.mp4,*.m4a,*.swf,*.avi,*.mov | Remove-Item

Regards,
Krzysztof
Avatar of VillaMaria
VillaMaria

ASKER

So would something like this work?

Get-ChildItem -Path C:\users\%username% -Recurse -Include *.mp3,*.mp4,*.m4a,*.swf,*.avi,*.mov | Select name,Directory | Export \\UNCShare\output.csv

And

Get-ChildItem -Path C:\users\%username% -recurse -include *.mp3,*.mp4,*.m4a,*.swf,*.avi,*.mov | Remove-Item

My users can only save in their User folders so I do not want to delete any files used by software installed on their machines by doing this search at the root of the drive.
Actually for that you need to declare user's variable. Chect that please

$user = dir env:username
Get-ChildItem -Path C:\users\($user).value -recurse -include *.mp3,*.mp4,*.m4a,*.swf,*.avi,*.mov | Remove-Item

Krzysztof
OK so, this is what I've come up with.

$user = dir env:username
Get-ChildItem -Path C:\users\($user) -Recurse -Include *.mp3,*.mp4,*.m4a,*.swf,*.avi,*.mov | Select name,Directory | Export \\UNCShare\($user).csv
Get-ChildItem -Path C:\users\($user).value -recurse -include *.mp3,*.mp4,*.m4a,*.swf,*.avi,*.mov | Remove-Item
or should I split it into two scripts?
No, it's fine. It can be in one script. But in second script line is small error :) You need to also use ($user).value
So use these lines

$user = dir env:username
Get-ChildItem -Path C:\users\($user).value -Recurse -Include *.mp3,*.mp4,*.m4a,*.swf,*.avi,*.mov | Select name,Directory | Export \\UNCShare\($user).value".csv"
Get-ChildItem -Path C:\users\($user).value -recurse -include *.mp3,*.mp4,*.m4a,*.swf,*.avi,*.mov | Remove-Item

Krzysztof
This is what I have had to do to get the first part of the script working. I have not gotten the export to work. Export is not a valid command. I found two other ways of exporting export-csv and out-file..I have tried both and neither seem to like trying to name the file as the username. I can get it to work if I name the file with a fixed name, but not a name that comes from a variable.

$user = dir env:username
$username = "c:\users\" + $user
$Userfile = "\\studentshareserver\students\logs\" + $user + ".txt"

get-childitem -path ($username).value -recurse -include *.mp3,*.mp4,*.swf,*.avi,*.mov | Select name,Directory | out-file ($userfile).value
ASKER CERTIFIED SOLUTION
Avatar of Krzysztof Pytko
Krzysztof Pytko
Flag of Poland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
nope. Saves the file as System.collections.dictionaryEntry.txt
OK, I would check that tomorrow morning in my test lab and I post results here

Krzysztof
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks so much. I never would have gotten there without your help.
Great! Glad I could help :)

Krzysztof