Read directory and move files


Powershell is really cool and you can do a pile of stuff with it, I had dabbled with this a few years ago for some other bits.

Because of this I am going to create a category and as I write more will be posting them 🙂

We needed to move some files from one place to another and so after a few minutes / hour 🙂 I cam up with this.

# Folder to read to move files
$vDir = “F:\PackingSpecApproval\Temp_To_File\”
#$vDir = “X:\PackingSpecApproval\Temp_To_File\”

#Folders to move the specs to
$vMoveToDir = “F:\PackingSpecApproval\Approved\China\”
#$vMoveToDir = “X:\PackingSpecApproval\Approved\China\”
# The folder to read specs that need filing that would have been translated
$vtransdir = “F:\PackingSpecApproval\To_Be_Translated\”
#$vtransdir = “X:\PackingSpecApproval\To_Be_Translated\”
# This files the specs
$file = Get-ChildItem $vDir -Name
foreach ($i in $file)
{

if ($file -eq $null) {} else
{
$filechar = $i.Substring(0,1)
$filename = $i.PSChildName
$movetodirectory = $vMoveToDir+$filechar
$movefrom = $vDir+$filename
$moveto = $vMoveToDir+$filechar
MOVE $movefrom $moveto
}
}
# This files the specs that would need translating
$transfile = Get-ChildItem $vtransdir -Name
foreach ($i in $transfile)
{

if ($transfile -eq $null) {} else
{
$filechar = $i.Substring(0,1)
$filename = $i.PSChildName
$movetodirectory = $vMoveToDir+$filechar
$movefrom = $vtransdir+$filename
$moveto = $vMoveToDir+$filechar
MOVE $movefrom $moveto
}

}

# clean up the info files

cd “F:\PackingSpecApproval\Info\AmmendedAwaitingTranslation\”
Get-ChildItem | Remove-Item -Recurse
cd “F:\PackingSpecApproval\Info\AwaitingTranslation\”
Get-ChildItem | Remove-Item -Recurse
cd “F:\PackingSpecApproval\Info\WatcherFolder\”
Get-ChildItem | Remove-Item -Recurse

Advertisements


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s