Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Powershell formatting command

Posted on 2013-02-07
Medium Priority
Last Modified: 2013-02-11
# QCM_Archive.ps1

# Load Template filename template
$Report_Template = Get-Content "C:\Powershell\QCM_Template_Name.txt";

# Load Days variable
# Set to -1 to take yesterdays date.
$days = -1

# Calculate PastDay variable using $days value
$pastdate = [datetime]::Now.AddDays($days)
#Write-Host "$pastdate"

# Specify where to read the files from
# A share locally on current server
$FolderPath = Get-ChildItem "V:\Capacity Manager Size reports\"

# Loop 1 

# Load $Files variable 
Foreach ($Files in $FolderPath)

# Loop 2
# Load template variable with formatted filenames
# IF Loop
# Use Like to load variable files using template to determine the correct day to load
# Like uses template filename at start of file and then '*' used to ignore the end of the file name which is variable
# Copy-Item cmdlet 
# Where to copy the calculated $files value 

    {foreach($template in $Report_Template)
        {If ($files.Name -like $($template + $pastdate.Day + "_" + $pastdate.Month + "_" + $pastdate.Year +"*.pdf"))
             {Copy-Item -Path $files.FullName "\\Db-occ14\CapacityManagerReports_Archive\"}

Open in new window

Please can someone take a look at the code and change it to use format command instead of the current method.

The script had been working when created in Oct 2012 - the script was run once at the end of each month.

But in the new year has stopped working because the $pastdate.month variable is holding value of '01' and the code cant handle the padding with the 01

Can someone help with a way to use powershell formatting to avoid the padding issue ?

Question by:mooriginal
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
LVL 71

Expert Comment

ID: 38863893
$pastdate.month will certainly not be '01' - it is 2 (integer). I assume your issue is that you NEED padding, but that should then be the case with the day from start. Can you clarify the expected filename format?

Author Comment

ID: 38867588
Im not sure of the format to be clear.

At present because the datetime is bringing back date formate of 01
the script is not working as expected.

I need a way to handle the 01, 02, etc for months until we get into a double digit month like Oct 10.

I thought the best way to do this would be ask for a way of avoiding the current method using formatting or working around it ...

Also we use the UK format for date here - so DD/MM/YY

For example a file format that is correct and should be copied is following:

The original script was working when the format of the file being checked was like this
LVL 71

Accepted Solution

Qlemo earned 2000 total points
ID: 38867886
Ah, I see, the day is always 2 digits as we are executing the script at the end of month anyway, so this "failure" is not relevant.
There are many methods to properly format the date. To stay with what you already have, change line 33 to:
        {If ($files.Name -like $($template + $pastdate.Day + "_" + ([String]$pastdate.Month).PadLeft(2,'0') + "_" + $pastdate.Year +"*.pdf"))

Open in new window

Another way is, using .NET formatting:
        {If ($files.Name -like "$template {0:dd_MM_yyyy}*.pdf" -f $pastdate

Open in new window

which looks much shorter and is more straight-forward.
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  


Author Comment

ID: 38868139
This looks a great answer - unfortunately both code snippets didnt work - or at least they ran but didnt copy any files...

The folder in question that is being read currently has files from 28th Jan - to 8th Feb.

So tried to run both changes - expecting to copy files from yesterdays date - 7th feb.

Nada so far ...

Also when I switched on the this  

$pastdate = [datetime]::Now.AddDays($pastdays)
Write-Host "$pastdate"
Result : 02/07/2013 15:13:53

Dont know if that helps
LVL 71

Expert Comment

ID: 38868204
The second code snippet expects an space before the date, which is "wrong"
        {If ($files.Name -like "$template{0:dd_MM_yyyy}*.pdf" -f $pastdate

Open in new window

However, the first one should have worked as-is, if your template is correct. The template needs to be
or else we have to change the like pattern.

Author Comment

ID: 38872883
ill give these a try on monday - the template has not changed as far as i know and as i said this process was working before 2013 ....

Author Comment

ID: 38875196
Your 2nd script also was missing a closing ')'
However it still did not work.

I checked the template - and there was 1 servername error in it - which is strange because it still worked in 2012 but just ignored this particular file in the copy.

Nevertheless after tidying up template - and running your
        {If ($files.Name -like $($template + $pastdate.Day + "_" + ([String]$pastdate.Month).PadLeft(2,'0') + "_" + $pastdate.Year +"*.pdf"))

Worked a treat


Author Closing Comment

ID: 38875197
Well explained and thought through

Featured Post

Looking for the Wi-Fi vendor that's right for you?

We know how difficult it can be to evaluate Wi-Fi vendors, so we created this helpful Wi-Fi Buyer's Guide to help you find the Wi-Fi vendor that's right for your business! Download the guide and get started on our checklist today!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question