File pattern and Underscore

Welcome Forums General PowerShell Q&A File pattern and Underscore

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 7 years, 2 months ago.

  • Author
  • #5150

    Points: 0
    Rank: Member

    by bobsdesk at 2012-09-27 10:37:19

    I am at a loss in figuring out how to be able to include the underscore character in a pattern match file name. I have a list of files like
    When I use Copy-Item C:\PrintSpooler_*.log $Elsewhere it does not copy the files nor does it error.
    When I use Copy-Item C:\PrintSpooler*.log $Elsewhere it copies the files fine.
    How would I construct the code that will work in the first example? I've tried various ways of including quotes, single quote, escape. First using a variable but no avail.

    by jonhtyler at 2012-09-27 11:04:22

    While looking at the "full" help for the copy-item cmdlet, it shows that the -Path parameter does not accept wildcard characters. However, I have tested your first example and it appears to work for me. What version of Powershell (get-host) are you using. I am running Powershell V3 on my system, which may account for the difference if you are using Version 2.

    by bobsdesk at 2012-09-27 11:23:13

    Version 2.0, I have tried using the -Filter and -Include but the results are the same as using it directly in the -Path (or source).

    PrintSpooler*.log works
    PrintSpooler_*.log breaks it

    by jonhtyler at 2012-09-27 11:27:08

    Not sure what to tell you. I just tested it on V2 and it works just as well.

    by Klaas at 2012-09-27 12:48:22

    For me it works fine too. Maybe you have different settings.

    Have you tried with the backtick to escape the underscore?Copy-Item C:\PrintSpooler`_*.log $Elsewhere
    What is the outcome of dir c:\printspooler_* ?

    by bobsdesk at 2012-09-27 14:33:41

    Yes I did try escaping the underscore. My example was lacking the fact I'm using variables (tried to make it simple too quick).
    The actual code I have is

    If ($DataSet.Tables[0].Rows.Count) {
    ForEach ($Row in $DataSet.Tables[0]) {
    Write-Host $($Row["StoreID"]) $($Row["Name"]) $($Row["IP"])
    $Net = New-Object -ComObject WScript.Network
    $Net.MapNetworkDrive("", "\\" + $($Row["IP"]) + "\C$", $false, $($Row["POSIDriver1"]), $($Row["POSIDriver2"]))
    $HostDir = $HostShare + "\StoreFiles\CPB-" + $($Row["StoreID"]) + "\"
    $RemoteDir = "\\" + $($Row["IP"]) + "\C$"
    #Write-Host $RemoteDir $HostDir
    #Get-ChildItem $RemoteDir
    #Get-ChildItem $HostDir
    Copy-Item -Path "$RemoteDir\Program Files\Qsr Automations\ConnectSmart\BackOffice\Log\PrintSpooler_*.log" -Destination $HostDir
    #Close up

    This fails to do any copy action and does not error. If I remove the Underscore it copies, but it copies more files than I need. I could get around this but I'm curious about the effects and I'd like to underrstand why this is happening.

    by jonhtyler at 2012-09-27 17:21:09

    I have tried simulating again what you are doing with the new code, but I still cannot get it to break. My only suggestion at this point is that you verify that there are no additional spaces in the variables where you construct the path. Perhaps you could generate a string and assign it to a variable to use for the Copy-Item -Path parameter. Then you can output the variable before executing it to make sure you don't have something like this:

    \\ \C$\Program Files\... (note the intentional space between the "1" and "\")

    I would also do the same for your $HostDir variable as well to verify it is what you expect. Other than that, I am at a loss to tell you why it is not working.

The topic ‘File pattern and Underscore’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort