How can I adjust this script to add MMYYYY to the file name before the extension on the Copy?
thanks
Before:
dys_ihhist.txt
After Copy to Dest
dys_ihhist_072023.txt
$source="c:\fileloading" #location of starting directory
$destination="c:\filecopy"; #location where files will be copied to
$files=@("*dys_ihhist*")
New-Item -ItemType Directory -Force -Path ($destination); Get-ChildItem -recurse ($source) -include ($files) | Copy-Item -Destination ($destination)
July 25, 2023 at 2:33 pm
Well your file copy could look something like,
$file | Copy-Item -Destination ($destination + ($file.BaseName + "_" + (Get-Date -Format "MMyyyy") + $file.Extension ))
July 25, 2023 at 2:47 pm
Is it trying to create a New directory
Copy-Item : Access to the path 'C:\filecopy_072023' is denied.
At line:5 char:112
Thanks.
July 25, 2023 at 2:57 pm
Yes you would still need to have your directory create logic in there, that was just to handle adding the date stamp.
July 25, 2023 at 3:00 pm
$source="c:\fileloading" #location of starting directory
$destination="c:\filecopy"; #location where files will be copied to
$files=@("*dys_ihhist*")
New-Item -ItemType Directory -Force -Path ($destination); Get-ChildItem -recurse ($source) -include ($files) | Copy-Item -Destination ($destination + ($file.BaseName + "_" + (Get-Date -Format "MMyyyy") + $file.Extension ))
I have this now maybe I misunderstood.
July 25, 2023 at 3:03 pm
I'm just trying to copy file with new MMYYYY into the folder filecopy
July 27, 2023 at 11:21 am
Any help getting this to work?
THanks.
July 27, 2023 at 1:23 pm
This was removed by the editor as SPAM
July 27, 2023 at 1:25 pm
This was removed by the editor as SPAM
July 27, 2023 at 3:13 pm
Everyone says that PowerShell is an essential skill for the modern DBA... bumping this for Bruin to give people the opportunity to demonstrate that skill. 😀
ZZartin did make a submission but I don't believe the OP knows how to incorporate that suggestion.
--Jeff Moden
Change is inevitable... Change for the better is not.
July 27, 2023 at 3:24 pm
Thanks Jeff.. correct on incorporating the solution. I don't want a NEW folder created just a MMYYYY inserted into filename as it's copied to Dest folder.
Thanks again.
July 27, 2023 at 3:35 pm
The example I gave was just add the MMYYYY to a file, it looks like you already have the logic to create the destination directory if it doesn't exist.
So now you need to run all the files from your source through it.
So your copy logic would look something like
Get-ChildItem | ForEach-Object {$_ | Copy-Item } and you would use $_ instead of $file to reference the object in each iteration.
July 27, 2023 at 4:08 pm
Thats maybe the issue I don't want a new directory just copy files from Source that conatain "$files=@("*dys_ihhist*")"
in there file name to DEST
Before:
dys_ihhist.txt --> found on Source
Copy to Dest
dys_ihhist_072023.txt
July 27, 2023 at 4:11 pm
Then just take out your logic that's creating the destination directory?
If it doesn't exist the copy-item will error out.
July 27, 2023 at 4:29 pm
I don't see where anyone has already said it, so I'll say it… “MMYYYY” is a date format to avoid. Using “YYYYMM” instead will allow you to actually sort those files chronologically.
Viewing 15 posts - 1 through 15 (of 20 total)
You must be logged in to reply to this topic. Login to reply