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

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -