• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 379
  • Last Modified:

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.
0
VillaMaria
Asked:
VillaMaria
  • 7
  • 6
2 Solutions
 
Krzysztof PytkoActive Directory EngineerCommented:
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
0
 
VillaMariaAuthor Commented:
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.
0
 
Krzysztof PytkoActive Directory EngineerCommented:
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
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
VillaMariaAuthor Commented:
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
0
 
VillaMariaAuthor Commented:
or should I split it into two scripts?
0
 
Krzysztof PytkoActive Directory EngineerCommented:
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
0
 
VillaMariaAuthor Commented:
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
0
 
Krzysztof PytkoActive Directory EngineerCommented:
Try that

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

get-childitem -path $username -recurse -include *.mp3,*.mp4,*.swf,*.avi,*.mov | Select name,Directory | Export-CSV $userfile

Krzysztof
0
 
VillaMariaAuthor Commented:
nope. Saves the file as System.collections.dictionaryEntry.txt
0
 
Krzysztof PytkoActive Directory EngineerCommented:
OK, I would check that tomorrow morning in my test lab and I post results here

Krzysztof
0
 
VillaMariaAuthor Commented:
Got it!

$user = dir env:username
$username = "c:\users\" + $user
$Userfile = "\\studentshareserver\students\logs\" + ($user).value + ".csv"

get-childitem -path ($username).value -recurse -include *.mp3,*.mp4,*.swf,*.avi,*.mov | Select name,Directory | ft directory, name |export-csv $userfile

Wierd that $username and $userfile are basically the same process for assigning the values, but $userfile has to be that way.
0
 
VillaMariaAuthor Commented:
Thanks so much. I never would have gotten there without your help.
0
 
Krzysztof PytkoActive Directory EngineerCommented:
Great! Glad I could help :)

Krzysztof
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now