Hi All,
I have been working on a script to move files based on two parts of their filename. For example I have a directory with 4 folders
M100
M105
M110
M115
within each folder contains 1-4 files that use the following naming convention
M100_Installation Report_yyyymmdd
M100_Service Report_yyyymmdd
M100_Other Documents_yyyymmdd
M100_Specifications_yyyymmdd
The script below will create a “M100” directory in the output folder if it does not exist along with an “Install Report” sub folder (and so on for each document type). It will then also move the files into the matching directories.
It is all working. However, I need the sub folder names to be the following
Install Report = Install Reports
Service Report = Service Reports
Specifications = Specifications
Other Documents = Other Doc
I tried adding an if statement to update a variable for each record but could not get it working (its currently commented out)
Any help on this is appreciated!
$dir = "C:\Test Indexed Files\docs to be reviewed" $output = "C:\Test Indexed Files\output" Set-Location $dir $filelist = @(Get-ChildItem -include *.pdf -Recurse -path $dir) ForEach ($file in $filelist){ # splits the file $folder1 = $file.Name.Split("_")[0] $folder2 = $file.Name.Split("_")[1] $folder3 = $file.Name.Split("_")[2] #check if folder exists Set-Location ($output+'\'+$folder1+'\'+$folder2) #create folder if doe snot exist if(!$?) {mkdir ($output+'\'+$folder1+'\'+$folder2)} #Move files to sub folders Move-Item $file.FullName ($output+'\'+$folder1+'\'+$folder2+'\'+$file.Name) }