I need to duplicate file many times I have a list of product numbers that need to be used as part of the new file name -- so example;
original file called photo.main.jpg
I need to copy the photo.main.jpg file many times (I am hoping to put the names to substitute in a file to read)
I have product names say 123, 456, 789, 345, 221 in the file and the product name must replace the word "photo" in the file.
this scenario i would want 5 new files called 123.main.jpg, 456.main.jpg, 789.main.jpg, 345.main.jpg and 221.main.jpg
I am hoping to do in Windows command prompt -- can someone help?
While this can be done in a cmd shell, PowerShell provides access to
regex processing that can make it very flexible.
The $replpat regex can be anything that provides what the
regex in the Copy-Item command needs. Using $matches is not required.
$orgfile = 'photo.main.jpg'
$prodfile = '.\prodlist.txt'
$replpat = '[^\.]*(.*)'
$ismatch = $orgfile -match $replpat
$filebase = $matches[1]
Get-Content -PSPath $prodfile |
ForEach-Object {
Copy-Item -Path $orgfile -Destination "$_$filebase" -WhatIf
}
Related
I have directory of files and my goal is matching a 5 digit zip code and copying it to another set of specific folders.
I know there are a number of ways to do this but sometimes the zip code shows up twice in the file name so I have to match the zip code in between ^ and % such that the entire filename is like: mjn22182aguygbc^12350%abc.pdf.
Another assumption is the destination location has preset folders named the zip code such as: d:\queries\12350.
My goal is to move mjn22182aguygbc^12350%abc.pdd to d:\queries\12350 if the zip code exists in some list i can read in and if it is not part of the zip codes, the file stays in the source folder.
So far I have the following but the all files are being copied into my root destination folder and not the folders that I've created eg 12350 and I'm having trouble :
$dstpath = "D:\queries"
$filterlist = #("12350","90182")
$fileList = Get-ChildItem -Path $srcpath -Force -Recurse
foreach ($currentfile in $fileList)
{
foreach($zip in $filterLists)
{
$currentzip = $currentfile.Name -match '(?<=\^)[0-9]+(?=%)';
if($currentzip -in $filterlist)
{
Move-Item -Path $($file.FullName) -Destination $dstpath
}
}
}
The regex is right, but all -match does is return true.
'mjn22182aguygbc^12350%abc.pdd' -match '(?<=\^)[0-9]+(?=%)'
True
The match would be contained in $matches.0 afterwards.
$matches.0
12350
You're moving to $dstpath. There's no reason it would go anywhere else.
I'm trying to search through a number of log files with different filenames. I want search for a hostname in each log and when a match is found have it copy that entire line to summary_[date].log and keep appending matching lines to it. So something I've started with is:
$captured = Get-ChildItem -recurse -Path \\nas1\share1 -Include *.log |
where { ($_.Name | Select-String -pattern ('PC1','PC2','PC3') -SimpleMatch) }
Now copy the line from each log file which contains the pattern and append it to a file with today's date stamp, so each week I'll have a file like \\nas1\share1\summary_03-07-2020.log
But this is not quite what I want as this will capture the filenames and append them to the $captured variable. It's also missing the code to copy any matching lines to a date stamped summary_[date].log
Each text file will contain, among other lines that start with a time stamp, something like this:
03-07-2020_14-36-17 - Backup of computer [PC1] is successfully
created.
So what I want is to search several text files on a share for several hostnames. If a text file contains the hostname have it append the line which contains the hostname to summary_[date].log. Lastly, since the matching lines will all start with a date/time stamp I need to keep the contents of summary_[date].log file sorted from newest date to oldest.
Essentially I should end up with a summary_[date].log every week that will look similar to this:
03-07-2020_14-36-17 - Backup of computer [PC1] is successfully created.
03-07-2020_13-21-12 - Backup of computer [PC3] is successfully created.
03-07-2020_11-36-29 - Backup of computer [PC2] is successfully created.
By doing this I get a summary of all log files from that day in a single file which I will then automatically email to a specific email address.
How do I accomplish this?
Your current code selects a string from the file name, not the content.
To do what you are after, you can use simething like this:
$logFolder = '\\nas1\share1'
$outFile = Join-Path -Path $logFolder -ChildPath ('summary_{0:dd-MM-yyyy}.log' -f (Get-Date))
$captured = Get-ChildItem -Recurse -Path $logFolder -Include *.log | ForEach-Object {
($_ | Select-String -Pattern 'PC1','PC2','PC3' -SimpleMatch).Line
} | Sort-Object # sorting is optional of course
#output on screen
$captured
# output to new log file
$captured | Set-Content -Path $outFile
Next send this file as attachment like:
# use splatting for cmdlets that take a lot of parameters
$params = #{
SmtpServer = 'yourmailserver'
From = 'logtester#yourcompany.com'
To = 'someone#yourcompany.com'
Subject = 'Summary'
Body = 'Hi, in the attachment a summary of the backup logs'
Attachments = $outFile
# etc.
}
Send-Mailmessage #params
How big are your log files?
This will work but it loads the entire content of all files into memory and maybe inefficient with large file and/or a large number of files.
If there is a large number of files or large files you could do some nested foreach loops and loop through each computer name for each file.
This also uses match rather than like in where-object, you can use. like if you get false positives.
$FileContent = Get-Content -Path "\\nas1\share1\*.log"
$ComputerArray = 'PC1','PC2','PC3'
$DateStamp = get-date -Format dd-MM-yyyy
$OutFile = 'summary_' + $DateStamp + '.log'
foreach ($ComputerName in $ComputerArray)
{
$FileContent | Where {$_ -match $ComputerName} | Add-Content -Path $OutFile
}
I've tried searching for a solution to my dilemma, I've even tried combining a few solutions from here, but have had no success.
My dilemma is that I download a lot of files all at once, and they tend to be given non descriptive names. I want a script that will read a CSV file with the following format:
current path,new path
Where current path is the full current path including file name, and new path is the full new path including file name.
I need this as a powershell script preferably, since that's my default, though cmd is usable as well. I'm currently running Windows 8.1 if that's needed info.
I would include the code I've already tried, but that would take up a lot of space, and I'm on my phone.
As a rule I wouldn't do this, but I had this one from a while back, it should put you on the right track.
$Files = "Path:\to\Files\"
$CSV = "Path:\to\CSV\file.csv"
$Import = import-csv $CSV
$RENAME = get-childitem $Files
$Import | % {
$old = $_.old #Column Title for Old file name
$new = $_.new #Column Title for New file name
rename-item $old $new
}
I have a lot of files that I need to rename.
The current filename is typical like 10002414892.PDF.
The new name is a document number typical 170668-R-UP011-VA01-0205.
No number sequence just a lot of different titles.
I have a two column list in Excel with the current file name and the new file name.
I need a command to rename the file from the current file name to the new file name.
I would use powershell but save the excel file as a CSV, the command for rename is
Rename-Item test.txt new_name.txt
then the load cmd is something like
$FilePath = "C:\Files\NewFileList.csv"
$serverList = Import-Csv $FilePath
-Header "OldName","NewName"
$serverList | where {$_.OldName -like ".PDF" } | Rename-Item OldName NewName
Thanks for viewing my question. I was unable to find any information online in regards to my question. I also have very basic experience in this area.
PowerShell Script:
-Query folder for files (list?)
-Move file based on filename to folder with same name. (Move with pipe to query?)
-Move will also parse second part of file name to include subsequent matching folder name for destination folder.
Files will contain many separate names so the move has to be on a loop.
Ex. File - "Name 1"
Scripts excutes moves file to folder with "name" then to subfolder "1".
Just to be clear there will be multiple names and numbers so multiple destination paths. Basically every file will have a different destination but the destination will correlate to the file name. If there is a language more accessible for this function please let me know.
Something like the following will get you started
$files = Get-ChildItem -File
foreach($f in $files) {
$dirname = $f -split " " -join "\"
New-Item -ItemType Directory -Path ".\$dirname"
Move-Item $f $dirname
}