Automation Software (File Renaming, File Size Auditing)

Status
Not open for further replies.

PiERiT

2[H]4U
Joined
Oct 8, 2010
Messages
2,601
I need to track down two different pieces of software for two different uses.

1. I have a few thousand files named xxx-xxx-xxxx.mpg. I also have a .csv file with four columns, one of which (column4) matches that syntax. I need a program that will scan a folder, find files that match column4, and rename them based on the remaining columns in the .csv. The end result would be named column1_column2_column3_xxx-xxx-xxxx.mpg. Lastly, this all needs to be possible with command line so I can automate it. The only program I've been able to find is Rename Expert, but the command line aspect of that program seems to be broken in that it can only rename folders and not files.

2. The same files mentioned above are supposed to be between 1.5gb and 2gb. Anything less or more is a problem. I need a program that can watch a folder, be it constantly or on a schedule, and if a file is too small or too large, rename it or send an email or etc. I can't find anything to do this.

Any help would be appreciated. Thanks!
 
For #1 I assume? This is what I came up with:

Code:
import-csv -header a,b,c,d shows.csv | foreach {rename-item -path ($_.c + '.mpg') -newname ($_.d + '_' + $_.b + '_' + $_.a + '_' + $_.c + '.mpg' -replace '\s', '')}

Seems to work. Thanks! Any idea for #2? Or can PowerShell do that too?
 
Last edited:
Any idea how to make it send an email only if it actually finds files that match the condition? This script properly finds and renames small files, but it's sending the email even when there aren't any.

Code:
dir *.mpg | where {$_.length -lt 1gb} | rename-item -newname {'---TOOSMALL---'+$_.BaseName+('---TOOSMALL---')} | Send-MailMessage -From "[email protected]" -to "[email protected]" -smtpserver "herp.derp.com" -subject "test2" -body "test2"

Edit: I can't figure it out, so I think I'll just have them moved to a new folder called "corrupt" or something, and then have another program watch that folder and email when things are placed in it. That's a lot easier to setup.

Thanks for the help. PowerShell is pretty nifty, but I've had my fill for the day.
 
Last edited:
$myPath = [IO.Directory]::GetFiles("C:\temp\");
foreach ($myFile in $myPath) { if ( ((get-item $myFile).length -lt 2000000000) -and ((get-item $myFile).length -gt 1500000000) ) { Write-Host "between range" $myFile; }
else { Write-Host "outside range" $myFile; }}
 
So now that I'm actually implementing this, I've encountered an unforeseen problem. Here is my code.

Code:
$shows = 'x:\videos'
$csv = 'k:\schedules'

copy-item $csv\*.csv $shows\shows.csv

get-childitem $shows\*.mpg | foreach {rename-item $_ $_.Name.Replace('(','').Replace(')','').Replace(' ','-').Replace('1-800-','800-')}
import-csv -header a,b,c,d $shows\shows.csv | foreach {rename-item -path ($shows + '\' + $_.c + '.mpg') -newname ($_.d + '_' + $_.b + '_' + $_.a + '_' + $_.c + '.mpg' -replace '\s', '')}

dir $shows\*.mpg | where {$_.length -lt 800mb -or $_.length -gt 900mb} | move-item -dest $shows\corrupt

This works in practice, but my problem is that it cycles through every single row in the .csv -- all ten thousand of them. Only a few of the rows will be applicable at any given time. I need the command to run on every file in the folder, not every file in the .csv. It should be pretty simple, but I'm drawing a blank tonight. Will try some more tomorrow.
 
Last edited:
I gots it. I put in an if statement with test-path, and it finishes within 30 seconds now. Plus a few more tweaks, everything is perfect. Thanks!

Code:
$shows = 'x:\shows'
$schedules = 'k:\schedules'

cd $shows
get-childitem *.mpg | foreach {rename-item $_ $_.Name.Replace('(','').Replace(')','').Replace(' ','-').Replace('1-800-','800-')}

$date = get-date -format 'MMddyyyy'
$csv = $schedules + '\shows_' + $date + '.csv'
copy-item $csv shows.csv
import-csv -header a,b,c,d shows.csv | foreach {
$old = $_.c + '.mpg'
$new = $_.d + '_' + $_.b.replace(':','').replace('\','').replace('/','') + '_' + $_.a + '_' + $_.c + '.mpg' -replace '\s',''
if (test-path $old) {rename-item $old $new}}

dir *.mpg | where {$_.length -lt 800mb -or $_.length -gt 900mb} | move-item -dest corrupt
 
Last edited:
Status
Not open for further replies.
Back
Top