I have a powershell script that will move files containing certain "key words" to the corresponding folder listed in various .csv files. While I seem to have the mechanism down to move the files. I keep getting the following error
Join-Path : Cannot bind argument to parameter 'Path' because it is null.
At C:\Users\superflysnooka\Documents\Support\Projects\REDTransferAnalysis\REDFileTransferR2.ps1:46 char:38
+ $destinationPath = Join-Path $destinationDirectory $fileName
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Join-Path], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.JoinPathCommand
I'm not sure if it's because of the way I am passing in required parameters for the main code to execute.
$sourceDirectory = "C:\Users\superflysnooka\Documents\Support\Projects\REDTransferAnalysis\staging\"
# Import analog and digital directories from CSV files
$analogDirectories = Import-Csv "C:\Users\superflysnooka\Documents\Support\Projects\REDTransferAnalysis\cfg\analog_folders.csv"
$digitalDirectories = Import-Csv "C:\Users\superflysnooka\Documents\Support\Projects\REDTransferAnalysis\cfg\digital_folders.csv"
# Create log file
$logFile = "C:\Users\superflysnooka\Documents\Support\Projects\REDTransferAnalysis\log\filetransferlog.txt"
# Get current date
$currentDate = Get-Date -Format "yyyy-MM-dd"
# Loop through files in the source directory
Get-ChildItem $sourceDirectory | ForEach-Object {
$fileName = $_.Name
$filePath = $_.FullName
# Check if file contains "analog" or "digital"
if ($fileName -like "*Analog*") {
$destinationDirectories = $analogDirectories.Directory
} elseif ($fileName -like "*Digital*") {
$destinationDirectories = $digitalDirectories.Directory
} else {
Add-Content -Path $logFile -Value "[$currentDate] Failed to transfer file: $fileName"
return
}
# Move or copy file to destination directories
foreach ($destinationDirectory in $destinationDirectories) {
$destinationPath = Join-Path $destinationDirectory $fileName
if ($fileName -like "*Analog*") {
Move-Item -Path $filePath -Destination $destinationPath
} elseif ($fileName -like "*Digital*") {
Copy-Item -Path $filePath -Destination $destinationPath
}
Add-Content -Path $logFile -Value "[$currentDate] Successfully transferred file: $fileName to $destinationDirectory"
}
}
Here is what the first column of one of the .csv files looks like
Directory
C:\Users\superflysnooka\Documents\Support\Projects\COBETransferAnalysis\sites\Area1\Analog
C:\Users\superflysnooka\Documents\Support\Projects\COBETransferAnalysis\sites\Area2\Analog
C:\Users\superflysnooka\Documents\Support\Projects\COBETransferAnalysis\sites\Area3\Analog
C:\Users\superflysnooka\Documents\Support\Projects\COBETransferAnalysis\sites\Area4\Analog
Resolved by the following