0

I need to copy images generated by software (HWMonitorPro). Folders and sub-folders of images cannot be modified (or I did not find how).

The images are stored in the following path: C:\Users\hugo\Documents\DossierTest\logs\[JUN 13, 2022 - 11:06]\[LAPTOP-P15V]\[1280x960].

As you can see, a directory is created with each recording, with the date and time. So I decided to use the following technique to be able to take the images of several captures at once:

$C = "C:\Users\hugo\Documents\DossierTest\logs"

copy-item $C'\*\`[LAPTOP-P15V`]\`[1280x960`]\789.txt' -Destination $C\test3

The line works with "[JUN 14, 2022 - 9:22]" instead of "*". This is what I get when I use the line show above:

PS C:\Windows\system32> C:\Users\hugo\Documents\DossierTest\ScriptCC.ps1
Copy-Item : Cannot retrieve the dynamic parameters for the cmdlet. The specified wildcard character pattern is not valid: [LAPTOP-P15V
At C:\Users\hugo\Documents\DossierTest\ScriptCC.ps1:3 char:1
+ copy-item $C'\*\`[LAPTOP-P15V`]\`[1280x960`]\789.txt' -Destination $C ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Copy-Item], ParameterBindingException
    + FullyQualifiedErrorId : GetDynamicParametersException,Microsoft.PowerShell.Commands.CopyItemCommand

If anyone could help me solve my problem that would be great!

PS: I'm French, sorry if it's badly written.

phuclv
  • 37,963
  • 15
  • 156
  • 475
Hugo
  • 1
  • 3
  • Drop the quotation marks in the middle of the string expression: ```Copy-Item $C\`[*\`[LAPTOP-P15V`]\`[1280x960`]\789.txt -Destination $C\test3``` – Mathias R. Jessen Jun 15 '22 at 13:00
  • it gives me the same error : Cannot retrieve the dynamic parameters for the cmdlet. The specified wildcard character pattern is not valid: [* – Hugo Jun 15 '22 at 14:57
  • see [Copy file with square brackets `[ ]` in the filename and use `*` wildcard](https://stackoverflow.com/a/57778204/995714) for information on how to escape the wildcard – phuclv Jun 15 '22 at 16:01
  • Does this answer your question? [Copy file with square brackets \[ \] in the filename and use \* wildcard](https://stackoverflow.com/questions/21008180/copy-file-with-square-brackets-in-the-filename-and-use-wildcard) – phuclv Jun 15 '22 at 16:01
  • no, this one : [link](https://stackoverflow.com/questions/21008180/copy-file-with-square-brackets-in-the-filename-and-use-wildcard) don't help me. I saw it before and try it but nothing works – Hugo Jun 16 '22 at 06:42
  • Trying to test here and I can't create test directories because of the colon `:` in the time portion of the directory name. That's an illegal character for directory/file names. Is it using an alternate unicode character? – Keith Miller Jun 16 '22 at 16:10
  • In the `-Path` aparameter of your `Copy-Item` cmdlet, how about replacing your literal brackets with the single-character wildcard `?`? `copy-item $C\*\?LAPTOP-P15V?\?1280x960?\789.txt` – Keith Miller Jun 16 '22 at 16:31
  • @KeithMiller NTFS is POSIX-compliant so you can use [the \\?\ prefix](https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file) to create a file with any special character: `New-Item -ItemType file -Path "\\?\C:\test:file.txt"`. But yes `11:06` is indeed suspicious, you can't create or access that file in Win32 namespace – phuclv Jun 18 '22 at 10:06

1 Answers1

0

As it seems there's something strange with paths containing square brackets, you can use CopyTo method to copy files instead of copy-item cmdlet. E.g. like this:

$rootfolder = "E:\tmp\logs"
$destfolder = "E:\tmp\foldertest3"

$allfiles = gci $rootfolder -File -Recurse

foreach ($file in $allfiles) {
    $newfile = $file.CopyTo("$destfolder\$($file.Name)")
}

This copies all files in subdirectories to single destination folder = you'll need to deal with t further in case there are files with same name in different subfolders.

PS: Your $C is really a terrible variable name when dealing with file paths :-)

vasja
  • 4,732
  • 13
  • 15
  • *As it seems there's something strange with paths containing square brackets* no there's nothing wrong with it, because `Get-ChildItem`'s `-Path` receives a wildcard where `[]` is a pattern: [`How can I make PowerShell handle `[` or `]` in file name well?](https://stackoverflow.com/q/57755417/995714). And your method won't work because the folder has `:` in its name and `gci` won't be able to access it – phuclv Jun 18 '22 at 11:16