Read or parse a numberset within a filename

This topic contains 6 replies, has 4 voices, and was last updated by  Sohail Gustasab 1 month ago.

  • Author
    Posts
  • #80204

    Sohail Gustasab
    Participant

    I'm using PowerShell to check that certain files are present in a folder location. If they are all present then the script will just state that 0 files are missing and if there are missing files it will tell you which ones are missing.

    The files that I'm checking have a date at the end of the filename. I need my script to ignore that part of the filename or read the date part and so long as the rest of the filename is present return no errors.

    Filename example: 0106-pos-20170917123002.data

    The bit of the filename I'm really interested in is 0106-pos-.data.

    Here is my script:

    $storenum = Read-Host -Prompt "Store number (include leading zero)"
    #$mbo_ip = Read-Host -Prompt "Back office IP"
    $missing = 0
    
    $sdc_pump = "\\10.xxx.xxx.xxx\gkretail\sdc\stores\device-pump"
    $pos_data_file = $storenum + "-pos-"
    $sdc_path = $sdc_pump + "\" + $pos_data_file
    
    "--"
    "Device Pump: config\parameter files"
    if (-not $(Test-Path $sdc_path\.data)) {
        $missing++; "$sdc_path\.data not present"
    }
    
    "=="
    "Store $storenum is missing $missing entries"
    "=="
    
    if (0) {
    }
    

    This is the result I get when I run the script.

    Store number (include leading zero): 0106
    --
    Device Pump: config\parameter files
    \\10.201.70.126\gkretail\sdc\stores\device-pump\0106-pos-[1-14]*.data\.data not present
    ==
    Store 0106 is missing 1 entries
    ==
    

    The reason it states that the file is missing is because it cant read the date omn the end of the filename which will always be changing.
    Thanks in advance for any help.

  • #80228

    Snak3d0c
    Participant

    Could you post some more examples of good data and bad data?
    Based on your description i'm not fully following.

  • #80236

    Chris Bakker
    Participant

    Some extra info would be nice, what are gooed and bad files.
    But shit looks promising:

    $pos_data_file = $storenum + "-pos-"
    $sdc_path = $sdc_pump + "\" + $pos_data_file
    
    "--"
    "Device Pump: config\parameter files"
    if (-not $(Test-Path "$sdc_path*.data")) {
        $missing++; "$sdc_path*.data not present"
    }
    "=="
    "Store
  • #80239

    Snak3d0c
    Participant

    I'm not sure i got the question right but here goes.
    So i made a testfolder that contains:

        gci -Path C:\temp\pos\ 
    
        Mode                LastWriteTime         Length Name                                          
        ----                -------------         ------ ----                                          
        -a----       20-09-2017     15:36              0 0106-pos-20170917123002.data                  
        -a----       20-09-2017     15:36              0 0107-pos-20170917123002.data                  
        -a----       20-09-2017     15:36              0 0108-pos-20170917123002.data                  
        -a----       20-09-2017     15:36              0 0109-pos-20170917123002.data                  
        -a----       20-09-2017     15:36              0 0110-pos-20170917123002.data         
    
    
        $items  = gci -Path C:\temp\pos\ | Select Name -ExpandProperty Name
    
        $lookfor = "0111-pos-"
        $found = $null
        foreach($item in $items){
            $r = [REGEX]::Matches($item, '^(\d{4}[-]pos[-])')
        
            if($r.value -match $lookfor){
                write-host $r.value -ForegroundColor green
                $found++
            }
        }
        if($found -lt 1){
                write-host $lookfor "was not found" -ForegroundColor red
            }
    

    $lookfor = "0111-pos-"
    Would have the output: 0111-pos- was not found in a red color.

    $lookfor = "0106-pos-"
    Would output "0106-pos-" in a green color.

    Other way to go about it :

       
        $items  = gci -Path C:\temp\pos\ | Select Name -ExpandProperty Name
    
        $lookfor = "0106-pos-"
        $found = $null
        foreach($item in $items){
        $r = [REGEX]::Matches($item, '^(\d{4}[-]pos[-])')
        if(($r.value -match $lookfor) -eq "true"){
            $r.value
        }
        }
        # This would only show if true. You can change that to false ofc;
    
       
    
  • #80246

    Sohail Gustasab
    Participant

    Hi guys,
    Thanks for the suggestions.
    Here's the script looking for a different set of files.

    This is the script with the files I'm looking for, manually entered in at line 12

    $storenum = Read-Host -Prompt "Store number (include leading zero)"
    #$mbo_ip = Read-Host -Prompt "Back office IP"
    $missing = 0
    
    $sdc = "\\10.xxx.xxx.xxx\gkretail\sdc\stores"
    $sdc_store = "STORE-SDC-" + $storenum
    $sdc_path = $sdc + "\" + $sdc_store
    
    
    "--"
    "SDC: config\parameter files"
    if (-Not $(test-path $sdc_path\config\parameter\booking.properties)) {$missing++; "$sdc_path\config\parameter\booking.properties not present"}
    if (-Not $(test-path $sdc_path\config\parameter\common.properties)) {$missing++; "$sdc_path\config\parameter\common.properties not present"}
    if (-Not $(test-path $sdc_path\config\parameter\device-status.properties)) {$missing++; "$sdc_path\config\parameter\device-status.properties not present"}
    if (-Not $(test-path $sdc_path\config\parameter\export-to-netweaver-llp.properties)) {$missing++; "$sdc_path\config\parameter\export-to-netweaver-llp.properties not present"}
    if (-Not $(test-path $sdc_path\config\parameter\export.properties)) {$missing++; "$sdc_path\config\parameter\export.properties not present"}
    if (-Not $(test-path $sdc_path\config\parameter\external_locations.xml)) {$missing++; "$sdc_path\config\parameter\external_locations.xml not present"}
    
    
    
    "=="
    "Store $storenum is missing $missing entries"
    "=="
    
    
    if (0) {
    
    
    }
    

    This is the result when you give it a store number that has all the files listed in the script.

    Store number (include leading zero): 0344
    --
    SDC: config\parameter files
    --
    SDC: sm-dc-import folders
    --
    ==
    Store 0344 is missing 0 entries
    ==
    

    Here's the result when a store is missing the files.

    Store number (include leading zero): 0420
    --
    SDC: config\parameter files
    \\10.201.70.126\gkretail\sdc\stores\STORE-SDC-0420\config\parameter\booking.properties not present
    \\10.201.70.126\gkretail\sdc\stores\STORE-SDC-0420\config\parameter\common.properties not present
    \\10.201.70.126\gkretail\sdc\stores\STORE-SDC-0420\config\parameter\device-status.properties not prese
    nt
    \\10.201.70.126\gkretail\sdc\stores\STORE-SDC-0420\config\parameter\export.properties not present
    \\10.201.70.126\gkretail\sdc\stores\STORE-SDC-0420\config\parameter\external_locations.xml not present
    \\10.201.70.126\gkretail\sdc\stores\STORE-SDC-0420\config\parameter\file-transfer.properties not prese
    nt
    \\10.201.70.126\gkretail\sdc\stores\STORE-SDC-0420\config\parameter\item-repository.properties not pre
    sent
    \\10.201.70.126\gkretail\sdc\stores\STORE-SDC-0420\config\parameter\notification.properties not presen
    t
    \\10.201.70.126\gkretail\sdc\stores\STORE-SDC-0420\config\parameter\replication-firmware.properties no
    t present
    \\10.201.70.126\gkretail\sdc\stores\STORE-SDC-0420\config\parameter\replication.properties not present
    \\10.201.70.126\gkretail\sdc\stores\STORE-SDC-0420\config\parameter\scale-upload.properties not presen
    t
    \\10.201.70.126\gkretail\sdc\stores\STORE-SDC-0420\config\parameter\schedule-external-supplier-import-
    llp.properties not present
    \\10.201.70.126\gkretail\sdc\stores\STORE-SDC-0420\config\parameter\store-config.properties not presen
    t
    \\10.201.70.126\gkretail\sdc\stores\STORE-SDC-0420\config\parameter\store-hub.properties not present
    \\10.201.70.126\gkretail\sdc\stores\STORE-SDC-0420\config\parameter\utx.properties not present
    --
    SDC: sm-dc-import folders
    \\10.201.70.126\gkretail\sdc\stores\STORE-SDC-0420\sm-dc-import\BusinessUnitText not present
    \\10.201.70.126\gkretail\sdc\stores\STORE-SDC-0420\sm-dc-import\Currency not present
    \\10.201.70.126\gkretail\sdc\stores\STORE-SDC-0420\sm-dc-import\CurrencyRoundingRule not present
    \\10.201.70.126\gkretail\sdc\stores\STORE-SDC-0420\sm-dc-import\CustomerTenderGroup not present
    \\10.201.70.126\gkretail\sdc\stores\STORE-SDC-0420\sm-dc-import\CustomerTenderGroupTender not present
    \\10.201.70.126\gkretail\sdc\stores\STORE-SDC-0420\sm-dc-import\Denomination not present
    \\10.201.70.126\gkretail\sdc\stores\STORE-SDC-0420\sm-dc-import\ExchangeRate not present
    \\10.201.70.126\gkretail\sdc\stores\STORE-SDC-0420\sm-dc-import\Language not present
    \\10.201.70.126\gkretail\sdc\stores\STORE-SDC-0420\sm-dc-import\ReasonTender not present
    \\10.201.70.126\gkretail\sdc\stores\STORE-SDC-0420\sm-dc-import\TaxableGroup not present
    \\10.201.70.126\gkretail\sdc\stores\STORE-SDC-0420\sm-dc-import\TaxAuthority not present
    \\10.201.70.126\gkretail\sdc\stores\STORE-SDC-0420\sm-dc-import\TaxAuthorityRule not present
    \\10.201.70.126\gkretail\sdc\stores\STORE-SDC-0420\sm-dc-import\TaxGroupRule not present
    \\10.201.70.126\gkretail\sdc\stores\STORE-SDC-0420\sm-dc-import\Tender not present
    \\10.201.70.126\gkretail\sdc\stores\STORE-SDC-0420\sm-dc-import\TenderAdjustmentRule not present
    \\10.201.70.126\gkretail\sdc\stores\STORE-SDC-0420\sm-dc-import\TenderClass not present
    \\10.201.70.126\gkretail\sdc\stores\STORE-SDC-0420\sm-dc-import\TenderSaleReturnRule not present
    \\10.201.70.126\gkretail\sdc\stores\STORE-SDC-0420\sm-dc-import\Till not present
    \\10.201.70.126\gkretail\sdc\stores\STORE-SDC-0420\sm-dc-import\TillOperator not present
    \\10.201.70.126\gkretail\sdc\stores\STORE-SDC-0420\sm-dc-import\Worker not present
    \\10.201.70.126\gkretail\sdc\stores\STORE-SDC-0420\sm-dc-import\Workstation not present
    --
    MWB: config\parameter files
    --
    MWB: store root folders
    --
    LPS: config\parameter folder and files
    --
    LPS: store root folders
    ==
    Store 0420 is missing 36 entries
    ==
    

    These are the results I expect to see
    Does this make it any clearer as to what I'm trying to achieve or have is this even more confusing ?

    Again, thanks for any time you guys take to have a look at my issue.

  • #80278

    Curtis Smith
    Participant

    It seems like using get-childitem with a wildcard to account for the timestamp would be more effective.

    $storenum = Read-Host -Prompt "Store number (include leading zero)"
    $missing = 0
    
    $sdc_pump = "\\10.xxx.xxx.xxx\gkretail\sdc\stores\device-pump"
    $pos_data_file = $storenum + "-pos-"
    $sdc_path = $sdc_pump + "\" + $pos_data_file
    
    "--"
    "Device Pump: config\parameter files"
    if (-not (Get-ChildItem "$sdc_path*.data")) {
        $missing++
        "$sdc_path.data not present"
    }
    
    "=="
    "Store $storenum is missing $missing entries"
    "=="
    
  • #80297

    Sohail Gustasab
    Participant

    Hi Curtis,
    I've just tried your version of the script and it works....brilliant.
    I was trying the wildcard but just could not get it right.

    Many thanks Curtis, Chris and Snak3d0c for all your help on this thread.

You must be logged in to reply to this topic.