Thursday, October 23, 2008

Powershell: Accept Unknown number of Args and process them

Recently I needed a script to move some files for a user.  It might be two or three files but I wanted to exersise my noggin in powershell.  This code will take any number of arguments and copy the files from the input directory to the output directory. Below is the code.

::Start Code::
# Display Help
if (($Args[0] -eq "-?") -or ($Args[0] -eq "-help")) {
   "Usage: pullQualityAssesments.ps1 "
   "            copies the Faxjob over to the Quality Assesments folder."
   "Example: pullQualityAssesments.ps1 400001 400002"
$inputdir = "\\someserver\savedfaxjobs\"
$outputdir = "\\someotherserver\Quality Assessments\"
$x = $args.count
$i = 0
do {
   $ordernumber = $args[$i]
   #write-host $inputdir$ordernumber.faxjob.* $outputdir
   $filelist = get-childitem $inputdir$ordernumber.faxjob.*
   foreach ($file in $filelist){write-host $file}
while ($i -le $x)
::End Code::

Unfortunatly we have 23,000+ files in our Input directory and this script is slower than me pulling them manually.  Maybe someday I can revisit this to filter it down to something that works and pulls in only the files it needs.

No comments: