Ihave started a simple script in order to move files of a certain prefix to a folder of the same name eg: W100_11.jpg W100_12.jpg in to folder W100.
Thanks to help from the answers below I have progressed and have a successful loop which can iterate through the file sin the folder, I am having issues with the -filter switch and when trying to use the move-item cmdlet I am getting errors
The current code is:
$sourceDir = read-host "Please enter source Dir:"
$format = read-host "Format to look for with . :"
#$length = read-host "length of folder name:"
gci -Path $sourceDir | % {
If( -not $_.PSIsContainer)
{
$path = $sourceDir + "\" + $_.Name.substring(0, 3)
$_
if(-not (Test-Path $path))
{
mkdir $path
}
move $_.fullname $path
}
}
I am still having issues when using the -filter switch. This is a partial solution to the issue
The code below shortens your for loop and shows an example of using substrings on file names. You get a FileInfo object from the Get-ChildItem (gci) call, so you need to use it’s property Name to do substrings. See MSDN for more info on FileInfo.