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.
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 :-)
Related
I'm new to PowerShell and have read many of the similar questions. The answers seem way to complicated for what I want to do.
I want to:
Copy a file, testVerifyLog.xlsm
Paste into a subfolder of the same mapped network drive named:
Copies of Logs
Rename the file with the current date (YYYYMMDD) added to the
end, so that the final result for June 1, 2020 is:
testVerifyLog_20200601.xlsm
The code below is what I have come up with but obviously does not work. The error I get in the PowerShell ISE is:
copy-item : Illegal characters in path.
At \TheMappedDrive\Share\AllDepts\OneDept\VerifyLogsMove.ps:2 char:1
copy-item testVerifyLog.xlsm -
destination'\TheMappedDrive\Share\AllDepts\OneDept...
CategoryInfo : NotSpecified: (:) [Copy-Item], ArgumentException
FullyQualifiedErrorID :
System.ArgumentException,Microsoft.PowerShell.Commands.CopyItemCommand
Set-Location \\TheMappedDrive\Share\AllDepts
copy-item testVerifyLog.xlsm -destination '\\TheMappedDrive\Share\AllDepts\OneDept\Copies of Logs\testVerifyLog ; testVerifyLog-$(((get-date).ToUniversalTime()).ToString("yyyyMMdd")).xlsm'
Something like this
$curDate = Get-Date -Format yyyyMMdd
$destinationFolder = '\\TheMappedDrive\Share\AllDepts\OneDept\Copies of Logs\testVerifyLog'
$destinationFile = Join-Path $destinationFolder -ChildPath ('testVerifyLog_' + $curDate + '.xlsm')
copy-item '\\TheMappedDrive\Share\AllDepts\testVerifyLog.xlsm' -destination $destinationFile
I need to iterate over an array of folder names, passing them into GCI so I can run operations on their contents. No matter what I do, it keeps giving me this error:
gci : Illegal characters in path.
At C:\Scripts\Arvest_submission_monitoring.ps1:86 char:5
+ gci -path "D:\subftp\$_" -recurse {
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (D:\SUBFTP\folder1:String [Get-ChildItem], ArgumentException
+ FullyQualifiedErrorId :
DirArgumentError,Microsoft.PowerShell.Commands.GetChildItemCommand
I've created an array:
$folders = "folder1","folder2","folder3"
And then iterate over it:
$folders | % {
gci -Path "D:\subftp\$_" -Recurse {
#do stuff here
}
}
I've tried many various options such as a regex replace to remove any possible illegal characters (although a $folder.Count shows me there are no invisible characters), I've tried turning $folders into a C# ArrayList but it's still a no go. I've even played around with the path variable itself concatenating it into a single string before use.
The weird thing is if I try and execute it from the command line it works fine. What gives? I'm running v5.
It is caused by the curly brace right at the end of your command gci -Path "D:\subftp\$_" -Recurse {.
Probably you wanted to do this:
$folders | ForEach-Object {
Get-ChildItem -Path "D:\subftp\$_" -Recurse | ForEach-Object {
#do stuff here
}
}
I'm trying to create a powershell script that will copy files from a source to the same path on multiple destination machines. I found this on the web and have pointed toward my test data, however, I continue to receive the following error (for each system it finds in the pc_list.txt).
Script:
$Computers = gc "C:\Temp\Script\PC_List.txt"
$Source = "C:\Temp\Script\AAAAA.txt"
$Destination = "C$\Temp\"
foreach ($Computer in $Computers) {
Copy-Item -Path $Source -Destination "\\$Computer\$Destination\" -Recurse
}
Error:
Copy-Item : Illegal characters in path.
At line:4 char:36
+ ... Computers) {Copy-Item $Source -Destination "\\$Computer\$Destination\ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Copy-Item], ArgumentException
+ FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.CopyItemCommand
PC Names in PC_List.txt:
DEPT-PC1
DEPT-PC2
DEPT-PC3
Can someone point me in the right direction?
BenH is correct. While I did not have any blank lines in the PC_List.txt, I did have a space at the end of each line... Great catch!!!
Now I’m getting “The target account name is incorrect”.
I’m thinking I’m going to need to add a $session credentials line but will need to figure out how to implement that piece...
I'm a Powershell novice so apologies for any wrong terminology. I've had a look on SE and I know how to recursively descend into a directory structure, however all the working examples I can find seem to pipe the output of Get-ChildItem, which I can't understand how to further work with the file before the loop iterates.
What I'm trying to do
Provide my script with a directory: C:\Users\Donglecow\myPictures\.
Recursively loop through all directories in the defined directory (I have an extensive file structure).
Find all files which match a filter thumbnail*.jpg, EG: thumbnail_001.jpg
Rename the file to BACKUP-thumbnail*.jpg
Check the dimensions of that image in pixels and print them out to the command line
What I've got so far
param (
[string]$dir = $pwd
)
write-output "Working from $($dir)"
$foundJpg = get-childitem -Recurse -Filter thumbnail*.jpg $dir
foreach($file in $foundJpg) {
write-output "Found $($file)"
#Rename to Backup...
#Check image dimensions...
#Do other stuff...
}
This works and outputs the correct file name, but when I try to rename the file I get an error.
Rename-Item -NewName { $_.Name.replace("thumbnail", "SAFE-thumbnail.jpg") }
Rename-Item : Cannot evaluate parameter 'NewName' because its argument is specified as a script block and there is no
input. A script block cannot be evaluated without input.
At C:\Users\Donglecow\myPictures\med.ps1:9 char:23
+ ... -Item -NewName { $_.Name.replace("thumbnail", "SAFE-thumbnail.jpg") }
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : MetadataError: (:) [Rename-Item], ParameterBindingException
+ FullyQualifiedErrorId : ScriptBlockArgumentNoInput,Microsoft.PowerShell.Commands.RenameItemCommand
I've deducted this is probably because I've tried to adapt from a command that pipes the output as shown in answers on this question:
Get-ChildItem -Recurse -Filter *.mp3 | Rename-Item –NewName { $_.name –replace 'xyz','abc' }
Whereas I'm using a different construct for my loop so that I can further interact with each file that I find.
My environment
For reference, a snippet of my file structure is below.
C:\Users\Donglecow\myPictures\
1\001\thumbnail_001.jpg
1\002\thumbnail_001.jpg
1\003\thumbnail_001.jpg
2\001\thumbnail_001.jpg
etc...
Where am I going wrong here?
Try this:
foreach($file in $foundJpg) {
$File | Rename-Item -NewName { $_.Name.replace("thumbnail", "SAFE-thumbnail.jpg") }
#Do other stuff...
}
You need to use $file as the input of the command (which you should be able to do via the pipeline as shown above) as this is what your ForEach loop is using to represent each item retrieved by Get-ChildItem into the $foundJpg variable.
I have a file move script that looks for files with keywords in the name and moves them to a specific folder. There's about a hundred lines that all work fine, except for one of them, and the only thing I can think of is that the file has brackets in the name.
I'm already using -LiteralPath to grab the full path, but the error is weird. I've tried double quotes, double backticks, and some other options that I've found elsewhere on the boards, but nothing is working.
Here is all the relevant parts of the script (without posting the whole thing):
# Source folders
$RecipesFolder = "F:\Downloads\Downloaded Recipes\"
# Destination Folders Drive I
$CoffeeRecipes = "I:\My Recipes\Coffee Recipes\"
# Get list of files
$Files = Get-ChildItem $RecipesFolder -File -Recurse
# Process Files
foreach ($File in $Files) {
if ($File -match "Coffee") {
Move-Item -LiteralPath $RecipesFolder$File -Destination "$CoffeeRecipes" -Force
}
It's a really simple script, and every other line works fine, except that one. All the lines are written identically.
This is the full error I get (sourcepath obfuscated for security), and thank you in advance for any assistance:
Move-Item : Cannot move item because the item at 'F:\Downloads\Downloaded Recipes\[1]
Coffee Recipe - 11[1].txt' does not exist.
At Path-to-powershell-script-file:153 char:13
+ Move-Item -LiteralPath $RecipesFolder$File -Destina ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Move-Item], PSInvalidOperationException
+ FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.MoveItemCommand
When I receive this error, the file disappears, and doesn't get moved to the folder, it just vanishes.