« Posts tagged automation

Quickly rename a large number of files with PowerShell and Regular Expressions

There have been a few times in the past where I’ve had to rename a large number of files for various reasons (ie: remove a common piece of text from the name) and I’ve always resorted to PowerShell.

Piping dir into a where and matching the files I wanted to rename was effective but tedious. Cue the mass_rename.ps1 script:

$ext = $args[0];
$dir = $args[1];

$what = $args[2];
$with = $args[3];

$whatif = $args[4];

$count = 0;

if ($args.length -lt 4) {
    write-host "Invalid parameters" -fore red;
    ""
    write-host "   .\mass_rename.ps1 <ext> <dir> <what> <with> [-whatif]";
    ""
    write-host " Example (don't do any replacing, -whatif):";
    write-host "   .\mass_rename.ps1 .docx c:\Documents 'version 1\.1' 'version 1.2' -whatif";
    ""
    exit 1;
}

ls -recurse -path $dir | ?{ ($_.name.endswith($ext)) -and ($_.name -imatch $what) } | %{
    if ($whatif -eq "-whatif") {
        write-host("whatif: '" + $_.fullname + "' -> '" + ($_.name -ireplace $what,$with) + "'");
    }
    else {
        $from = $_.fullname;
        $to = ($_.name -ireplace $what,$with);
        mv -literalpath $from -destination ($_.directoryname + "\" + $to) -force;
        write-host "Renamed '$from' -> '$to'" -fore yellow;
        $count++;
    }
}

write-host "Done. Processed $count files." -fore green

The script will accept 4 parameters with an optional -whatif as the 5th. Fairly self explanatory with one mention: the <what> parameter is a regular expression. Keep this in mind when, for example, you are trying to match for a period (.) as you would have to escape it (as per the example usage).

The -whatif parameter will only output the before and after file names thout modifying the files themselves.

That’s it, set the execution policy and enjoy.

SMS Trace64 and Trace32 for WinPE

SMS Trace is a great tool for troubleshooting deployment issues from within WinPE, no doubt about that. It makes errors and warnings clearly visible in thousands of lines of markup logs.

Trace64 has been removed from the MDT Healthcare package, because “it is a System Center tool” and they don’t have permission to release it (See Dan’s blog).

Adding Trace32 and Trace64 to your winpe.wim is easy:

imagex /mountrw c:\winpe_x86.wim 1 c:\mount
copy /y c:\sms_trace\trace32.exe c:\mount\windows\
imagex /unmount /commit c:\mount

imagex /mountrw c:\winpe_x64.wim 1 c:\mount
copy /y c:\sms_trace\trace64.exe c:\mount\windows\
imagex /unmount /commit c:\mount

You should also create a package that copies these files to the %SystemRoot%. If your task sequence fails outside of WinPE you won’t have access to SMS Trace.

Remember to update your Distribution Points before creating the ISOs.

Download SMS Trace32 and Trace64