Powershell string work -
this first question here , i'm new powershell.
i have folder $home\devoluciones\
several files named dtoxxxyyyymm.dat xxx company number, yyyy current year , mm stands current month.
what need copy files folders named company number, example if have dto509201506.dat , dto908201506.dat need copy files $destination\509\
, $destination\908\
respectively.
till have following code:
#list items , send a.txt ls $home\devoluciones\dto*.dat | select -exp name > $home\a.txt #from a.txt keep first 6 characters , send b.txt get-content $home\a.txt | foreach {$_.remove(6)} | add-content $home\b.txt #from b.txt replace dto "" , send c.txt get-content $home\b.txt | foreach {$_ -replace "dto",""} | add-content $home\c.txt #from c.txt copy files destination get-content $home\c.txt | foreach {copy-item $home\devoluciones\*$_*.dat $destination\$_\} #clean temp files remove-item -erroraction ignore $home\a.txt -force remove-item -erroraction ignore $home\b.txt -force remove-item -erroraction ignore $home\c.txt -force
i achieve same result doing "cleaner" this, want learn how manipulate string in 1 line , if possible copy 1 command.
thanks, nestor.
here simple implementation should self explanatory. i'm sure add more concise 1 line answer well.
$files = get-childitem -path "$home\devoluciones\*" -include *.dat foreach ($file in $files) { $company = $file.name.substring(3,3) copy-item $file.fullname (join-path (join-path $destination $company) $file.name) }
edit: fixed error in destination path
edit2: get-childitem "the include parameter effective when command includes recurse parameter or path leads contents of directory, such c:\windows*, wildcard character specified"
Comments
Post a Comment