powershell - Moving specific files to specific folders -
i have list of xml file names (including full path file) in .txt document:
c:\files\www_site1_com\order\placed\test45.xml c:\files\www_site1_com\order\placed\test685.xml c:\files\www_site2_com\order\placed\test63.xml c:\files\www_site3_com\order\placed\test9965.xml c:\files\www_site4_com\order\placed\test4551.xml etc...
the idea need move xml files different folder, example:
c:\files\www_site1_com\order\placed\test45.xml moved c:\files\www_site1_com\order\in\test45.xml c:\files\www_site1_com\order\placed\test685.xml moved c:\files\www_site1_com\order\in\test685.xml
the problem not sure how can go move each file destination folder supposed go into.
below portion of script deals move. first portion takes list of xml files, , replaces \placed\ \in\ , end destination:
$content = get-content dump.txt foreach ($path in $content) { $path = $content -split "\\t" $paths = $path -notlike "*t*" $paths = $paths -replace ("placed","in") }
which ends giving me:
c:\files\www_site1_com\order\in c:\files\www_site1_com\order\in c:\files\www_site2_com\order\in c:\files\www_site3_com\order\in c:\files\www_site4_com\order\in
next, move files trying:
foreach ($path in $paths) { move-item -path $path -destination $paths }
but ending kind of errors:
move-item : cannot convert 'system.object[]' type 'system.string' required parameter 'destination'. specified method not supported. @ line:3 char:36 + move-item -path $path -destination $paths -whatif + ~~~~~~ + categoryinfo : invalidargument: (:) [move-item], parameterbindingexception + fullyqualifiederrorid : cannotconvertargument,microsoft.powershell.commands.moveitemcommand
i've tried few variations have managed files moved first destination folder , not correct ones. hope i've explained enough! help.
this line:
$path = $content -split "\\t"
produces array of strings (and $paths ends being array of strings well). -destination
arguments not accept array. doesn't seem robust (what if 1 of directories starts t?)
instead use split-path:
$sourcedirectories = $content | split-plath -parent
you can replace leaf of these directories hiving off parent , appending desired target directory:
$destdirectories = $sourcedirectories | split-path -parent | join-path -childpath "in"
then can move them, in single loop:
$content | foreach { $sourcedirectory = split-plath $_ -parent $destdirectory = split-path $sourcedirectory -parent | join-path -childpath "in" move-item $_ $destdirectory }
Comments
Post a Comment