Link to home
Start Free TrialLog in
Avatar of E=mc2
E=mc2Flag for Canada

asked on

Trigger script to send an attachment based on inside the ini file

I have an ini file called LatestFile.ini found at C:\Users\User\INIFile

When you open the file it will have the following text:
CopiedFile_Name=11,14,2020.xls

Based on the contents of that ini file, I want the script to then copy the file named with the date prior to this to a directory.
It has to check in C:\Users\User\File and if it sees the file called 11,13,2020.xls then it should copy that file to C:\Users\User\WorkingFolder...  
The caveat is that if it already copied that file to that folder then it should not do it again.



Avatar of HainKurt
HainKurt
Flag of Canada image

my suggestion: don't use comma or space in your files unless absolutely necessary...

NO: 11,14,2020.xls
YES: 11142020.xls 11_14_2020.xls
MAYBE: 11-14-2020.xls 11.14.2020.xls

Avatar of E=mc2

ASKER

they are not my files - they are files generated from a faulty program - so I have to deal with what is existent
ASKER CERTIFIED SOLUTION
Avatar of HainKurt
HainKurt
Flag of Canada 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
Avatar of David Johnson, CD
$sourcepath = 'd:\documents\windowspowershell\scripts\'
$inifile   = 'LatestFile.ini'
$destinationpath  = 'c:\temp\'
$inifilename = $inipath + $inifile
if (Test-Path $inifilename)
{
  Write-Output -InputObject ('INI File Exists.. Checking for File to Copy')
  $contents = @(Get-Content $inifilename)
  foreach($content in $contents)
  {
    if($content.substring(0,15) -eq 'CopiedFile_Name')
    {
      $start = 16
      $length = $content.Length
      $emd = ($length - $start)
      $copyfile = $content.SubString($start ,$emd)
      $filetocopy = $inipath + $copyfile
      $destination_filename = $destinationpath + $copyfile
      
      Write-Output -InputObject ("$filetocopy to copy to $destinationpath")
      if(!(Test-Path $destination_filename)) 
      {
        Write-Output -InputObject ('File Not Exists')
        Copy-Item -Path $filetocopy -Destination $destinationpath
        
        Write-Output -InputObject ('File Copied to Destination')
      }
      else 
      {
        Write-Output -InputObject ('Destination File Exists. Not Copied')
      }
    }
  }
}
else
{
  Write-Output -InputObject ('File Not Found')
}

Open in new window

Avatar of E=mc2

ASKER

Thanks David, .. the script does not work though.
Avatar of E=mc2

ASKER

Thanks HainKurt, your script seems to work, and now I will open another question since I will need to tweak the script.