csv - Search for a string in name and move file according to it -
i trying import csv file , loop through entry search match ( file names have other string in name), afterwards, want put match in appropriate directory, named after csv file
for examples:
files have:
rapport ptp (12314)
test (124523)
i want:
./12314/rapport ptp (12314)
./12314/rapport ptp (12453)
my csv file contains numbers needs searched through directory.
edit: far have came this, reasons gives false...
set-location "d:\ptp" $folders = import-csv d:\data\documents\buse1.csv $filelist = get-childitem -path d:\ptp foreach ($folder in $folders) { foreach ($file in $filelist) { $contains = $file.name -like "*$($folder.name)*" if ($contains) { $destination = 'd:\test1\{0}' -f, $folder.name; mkdir -path (split-path -path $destination -parent) -erroraction silentlycontinue; move-item -path $folder.name -destination $destination -whatif; } } }
this trips logic:
$file.name -contains $folder.name; if($contains){ # ... }
-contains
works on collections (arrays, lists, enumerables), doesn't substring search:
ps> "long","string" -contains "string" true ps> "long string" -contains "string" false
use -match
or -like
when doing string comparison instead:
$file.name -like "*$($folder.name)*"
or:
$file.name -match [regex]::escape($folder.name)
furthermore, $contains
doesn't exist - need assign result of comparison if()
statement work:
$contains = $file.name -like "*$($folder.name)*" if($contains){ # got match! }
or perform comparison inside if()
statement:
if($file.name -match [regex]::escape($folder.name)){ # got match! }
Comments
Post a Comment