?
Solved

Powershell formatting command

Posted on 2013-02-07
8
Medium Priority
?
336 Views
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 ?

Thanks
0
Comment
Question by:mooriginal
[X]
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
8 Comments
 
LVL 70

Expert Comment

by:Qlemo
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?
0
 

Author Comment

by:mooriginal
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:
Database_DB-SERV33_DataBase01_30_01_2013_00_32_55.pdf

The original script was working when the format of the file being checked was like this
Database_DB-SERV33_DataBase01_30_10_2012_00_32_55.pdf
0
 
LVL 70

Accepted Solution

by:
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.
0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 

Author Comment

by:mooriginal
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
0
 
LVL 70

Expert Comment

by:Qlemo
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
   Database_DB-SERV33_DataBase01_
or else we have to change the like pattern.
0
 

Author Comment

by:mooriginal
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 ....
0
 

Author Comment

by:mooriginal
ID: 38875196
Ok.
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

:-)
0
 

Author Closing Comment

by:mooriginal
ID: 38875197
Well explained and thought through
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
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…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

765 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