Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8793691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:09:18+00:00 2026-06-13T23:09:18+00:00

Using the following code to select help select 1 file from a list of

  • 0

Using the following code to select help select 1 file from a list of many dated files and then copy to share. Using the following code very kindly provided by fellow Stacker

$pastdays = -7

$pastdate = [datetime]::Now.AddDays($pastdays)

$files = Get-ChildItem "V:\Capacity Manager Size reports\"

$filename = ($files | Where-Object {$_.Name -like "Database_Server1_Userdb1_" + $pastdate.Day  + "_" + $pastdate.Month + "_" + $pastdate.Year+ "*.pdf"})

Copy-Item -Path $filename.FullName "\\Server50\report_Archive"

I need to expand this process to run for many files in the folder – each with slightly different names but following a common template to the file name

Original filenames examples:

Database_Server1_UserDB1_30_10_2012_00_20_51
Database_Server2_UserDB2_30_10_2012_01_20_51

Etc

Etc

But im not sure how to best do this in a foreach ?

I was thinking create a template file to load into a variable that holds all the examples of the files to examined before copying

So:

$Report_Template = Get-Content "C:\Powershell\FileTemplate_Name.txt";

FileTemplate_name.txt:

Database_Server1_tempdb_

Database_Server1_UserDB1_

Database_Server1_UserDB2_

Database_Server2_UserDB2_

Database_Server2_UserDB4_


$Report_Template = Get-Content "C:\Powershell\QCM_Template_Name.txt";

$pastdays = -7

$pastdate = [datetime]::Now.AddDays($pastdays)

$files = Get-ChildItem "V:\Capacity Manager Size reports\"

foreach($template in $Report_Template)

$filename = ($files | Where-Object {$_.Name -like $template + $pastdate.Day  + "_" + $pastdate.Month + "_" + $pastdate.Year+ "*.pdf"})

Copy-Item -Path $filename.FullName "\\Server50\CapacityManagerReports_Archive\"
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T23:09:19+00:00Added an answer on June 13, 2026 at 11:09 pm

    Ive done the following which seems to work

    $Report_Template = Get-Content "C:\Powershell\QCM_Template_Name.txt";
    $pastdays = -3
    $pastdate = [datetime]::Now.AddDays($pastdays)
    $Results = Get-ChildItem "V:\Capacity Manager Size reports\"
    Foreach ($Files in $Results)
    {foreach($template in $Report_Template)
     {If ($files.Name -like $($template + $pastdate.Day + "_" + $pastdate.Month + "_" + $pastdate.Year +"*.pdf"))
      {Copy-Item -Path $files.FullName "\\Server50\CapacityManagerReports_Archive\"}
     }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to code the following query using Zend_Db api's: select * from
Using following code I try to get updated list of checkbuttons' corresponding text values,
I am using following code to get bitmap from url. This function is used
I am using the following code to fill the drop-down box and select a
I using following code: var search = 'test'; if ($('#sku').find(search) ){ //alert(search); $(document).find(search).css('color','red'); <TABLE>
I am creating date using following code try { newdatetime = new DateTime(2012, 2,
I am using following code for showing a MessageBox with ok and cancel button.
I am using following code to show a spinning wheel: $(#loading) .hide() .ajaxStart(function(){ $(this).show();
i am using following code. public class MyCount extends CountDownTimer { public MyCount(long millisInFuture,
I am using following code for inserting comments: <div id=2 class=789678> <div id=dynamic2> <span

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.