Link to home
Start Free TrialLog in
Avatar of mooriginal
mooriginalFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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
Avatar of Qlemo
Qlemo
Flag of Germany image

$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?
Avatar of mooriginal

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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.
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 ....
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

:-)
Well explained and thought through