powershell - Copying files defined in a list from network location -
i'm trying teach myself enough powershell or batch programming figure out achieve following (i've had search , looked through couple hours of youtube tutorials can't quite piece figure out need - don't tokens, example, seem necessary in loop). also, not sure if below best achieved robocopy or xcopy.
task:
define list of files retrieve in csv (file name listed 13 digit number, extension unknown, .jpg might .png - achieved wildcard?)
list read like:
9780761189931 9780761189988 9781579657159
for each line in text file, do:
search network folder , all subfolders
if exact filename found, copy arbitrary target (say new folder created on desktop)
(not 100% necessary, nice have) once loop has completed, output list of files copied text file in newly created destination folder
i gather i'll maybe need couple of things first, define variables source , destination folders? found below elsewhere couldn't quite head around it.
set src_folder=o:\2017\by_month\covers set dst_folder=c:\users\%username&\desktop\getcovers /f "tokens=*" %%i in (isbn.txt) ( xcopy /k "%src_folder%\%%i" "%dst_folder%" )
thanks in advance!
this solution in powershell, way.
to subfiles of folder, use get-childitem
, pipeline, , can compare name insides of csv (which can using import-csv
, way).
get-childitem -path $src_folder -recurse | foreach{$_.fullname}
i'd use function edit name string, know isn't best way it. create function outside of pipeline, , have return modified path in such way can continue previous line this:
get-childitem -path $src_folder -recurse | foreach{$_.copyto (edit-path $_.fullname)}
where "edit-directory" function takes in path, , modifies return destination path. also, can alternatively use robocopy or xcopy instead of copyto, copy-item powershell native , doesn't require string manipulation (which in experience, less, better).
edit: here's function trick:
function edit-path{ param([string] $path) $modified_path = $dst_folder + "\" $modified_path = $path.substring($src_folder.length) return $modified_path }
edit: here's how integrate importing csv, copy happens files written in csv (which had left out, oops):
$csv = import-csv $csv_path get-childitem -path $src_folder -recurse | where-object{$csv -contains $_.name} | foreach{$_.copyto (edit-path $_.fullname)}
note have put whole csv path in $csv_path variable, , depending on how contents of file written, may have use $_.fullname, or other parameters.
Comments
Post a Comment