简体   繁体   中英

Powershell: Parse and copy multiple times

This command

Get-ChildItem -Filter "*$s*"

gives me this result:

    Mode                LastWriteTime         Length Name                                                  
----                -------------         ------ ----                                                  
-a----        1/21/2018  12:18 PM         337562 001938 H-22 and Note.pdf                   
-a----        1/24/2018   8:48 AM         319382 002104 H-22 and Note.pdf                   
-a----        1/21/2018  12:27 PM         339307 002351 H-22 and Note.pdf                   
-a----        1/24/2018   8:41 AM         338962 002454 H-22 and Note.pdf   

I want to copy the individual files into multiple files, eg:

001938 H-22 and Note.pdf
001938 H-22.pdf
001938 Note.pdf

3 copies of the same file - original and 2 duplicates.

Question: How can I tersely get the number for each file (eg, 001938) then copy that file into multiple files (eg, 001938 22.pdf and 001938 Note.pdf )?

I can't seem to find a good starting point for getting the number THEN using that to copy multiple times.

Example using regular expressions:

$fn = "001938 H-22 and Note.pdf"
$fn | Select-String '^([0-9]+) (\S+) and (\S+)(\.pdf)' | ForEach-Object {
  "Copy $fn to {0} {1}{2}" -f
    $_.Matches[0].Groups[1].Value,
    $_.Matches[0].Groups[2].Value,
    $_.Matches[0].Groups[4].Value
  # Outputs 'Copy 001938 H-22 and Note.pdf to 001938 H-22.pdf'
  "Copy $fn to {0} {1}{2}" -f
    $_.Matches[0].Groups[1].Value,
    $_.Matches[0].Groups[3].Value,
    $_.Matches[0].Groups[4].Value
  # Outputs 'Copy 001938 H-22 and Note.pdf to 001938 Note.pdf'
}

This might be obvious, but if the sections of the file names are fixed length, you could go with straightforward solution:

$a = "001938 H-22 and Note.pdf"
$a
$a.substring(0,7) + $a.substring(7,4) + ".pdf" 
$a.substring(0,7) + $a.substring(16)

The following uses the unary form of the -split operator to split the base name (filename root) into whitespace-separated tokens and then constructs an array of output filenames from these tokens:

Get-ChildItem -Filter "*$s*" | % {
  # Split the base name (filename root) into whitespace-separated tokens.
  # (For separators other than whitespace, use the binary form of -split;
  # e.g., `$_.BaseName -split '_'`)
  $tokens = -split $_.BaseName
  $ext = $_.Extension # save the input extemsion
  # Construct the array of output filenames from the tokens:
  $destNames = 
    $_.Name,                                        # e.g.: 001938 H-22 and Note.pdf
    ('{0} {1}{2}' -f $tokens[0], $tokens[1], $ext), # e.g.: 001938 H-22.pdf
    ('{0} {1}{2}' -f $tokens[0], $tokens[-1], $ext) # e.g.: 001938 Note.pdf
  # Use $destNames to copy the source file to multiple destinations...
  $destNames # for diagnostic purposes, simply output the destination filenames
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM