Append MMYYYY to File to Copy

  • 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)

     

     

  • Well your file copy could look something like,

     

    $file | Copy-Item -Destination ($destination + ($file.BaseName + "_" + (Get-Date -Format "MMyyyy") + $file.Extension ))
  • 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.

  • Yes you would still need to have your directory create logic in there, that was just to handle adding the date stamp.

  • $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.

  • I'm just trying to copy file with new MMYYYY into the folder filecopy

  • Any help getting this to work?

     

    THanks.

  • This was removed by the editor as SPAM

  • This was removed by the editor as SPAM

  • 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


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • 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.

  • 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.

  • 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

     

  • Then just take out your logic that's creating the destination directory?

     

    If it doesn't exist the copy-item will error out.

  • 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