Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 338
  • Last Modified:

Powershell formatting command

# 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
mooriginal
Asked:
mooriginal
  • 5
  • 3
1 Solution
 
QlemoC++ DeveloperCommented:
$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
 
mooriginalAuthor Commented:
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
 
QlemoC++ DeveloperCommented:
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
mooriginalAuthor Commented:
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
 
QlemoC++ DeveloperCommented:
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
 
mooriginalAuthor Commented:
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
 
mooriginalAuthor Commented:
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
 
mooriginalAuthor Commented:
Well explained and thought through
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now