Я пытаюсь переместить и переименовать цикл в файле CSV.
Я смотрел на другие темы и пытался реализовать, но продолжаю получать:
You cannot call a method on a null-valued expression.
At C:\Temp\test dir\dirtemp\oldkmove.ps1:6 char:6
+ $char = ($_.old).Substring(0, 1)
Я знаю, что эта ошибка относится к компоненту подстроки, но я уверен, что есть и другие проблемы.
Я хочу переместить папку с .\B.RVG\B000003
в .\1689
код:
$path = ".\"
Import-Csv -Delimiter "," -Path "C:\Temp\test dir\dirtemp\olddirmove.csv" | foreach {
$char = ($_.old).Substring(0, 1)
$rvg = $char + ".RVG"
$fromdir = $rvg + "\" + $_.old
$topath = Join-Path $path -ChildPath $_.new
$frompath = Join-Path $path -ChildPath $fromdir
Move-Item $frompath -Destination $topath
}
CSV:
old, new
B000003, 1689
...
С тех пор я обновил свой код, но когда я его запускаю, он не продолжается и останавливается на первой папке, которая отсутствует в этой проверке:
РЕДАКТИРОВАТЬ: замена «продолжить» на «возврат» исправила эту проблему.
#If File does not exist then skip.
If (!(Test-Path $frompath)) {
Write-Verbose "File $($frompath) Does not exist, skipping"
Continue
}
Есть предложения?
Кроме того, должен ли я начать новую тему, или я правильно сделал, добавив сюда?
Спасибо
#initialisation
CLS
$ErrorActionPreference = "continue"
$VerbosePreference = "continue"
#Settings
Start-Transcript -Path C:\temp\PSTranscript.txt -Append
$path = ".\"
$FileListFile = ".\FINALEXPORT.csv"
#Try to moving the files from the list to the the specified subfolder.
Import-Csv -Delimiter "," -Path "C:\Temp\FINALEXPORT.csv" | foreach {
$char = ($_.EXTNUM).substring(0,1)
$rvg = $char + ".RVG"
$fromdir = $rvg + "\" + $_.EXTNUM
$topath = Join-Path $path -ChildPath $_.matched_key
$frompath = join-path $path -ChildPath $fromdir
#If File does not exist then skip.
If (!(Test-Path $frompath)) {
Write-Verbose "File $($frompath) Does not exist, skipping"
Continue
}
# Check if files already exist in the sub folder.
If (Test-Path ($topath)){
Write-Verbose "File $($_.matched_pat_sys_id) exists already in the subfolder, skipping"
Continue
}
#try copying the file.
Try {
$frompath | Move-Item -Destination $topath;
Write-Verbose "File $($fromdir) succesfully moved."
}
Catch {Write-Warning "Could not move file $($fromdir), Skipping"}
}
Write-Verbose "Script finished." #, waiting for 5 seconds before closing."
start-sleep
Stop-Transcript