none
Powershell edit a file RRS feed

  • Question

  • Not sure the easiest way to do this.  I have a script file that is invoked using PowerShell.  One of the arguments in my script file is the output file location.  I would like to be able to pre-pend the 4 digit year and 2 digit month to the beginning of the filename (see example below).
    .EXPORT OUTFILE "\\server\dir1\dir2\dir3\YYYYMM_blah.csv"

    SAMPLE FILE CONTENTS BELOW:

    .LOGTABLE blah;
    .BEGIN EXPORT SESSIONS 24;
    .EXPORT OUTFILE "\\server\dir1\dir2\dir3\blah.csv"

    MODE RECORD FORMAT TEXT;

    More text after here...




    Wednesday, July 27, 2016 3:07 PM

Answers

  • In that case you can use .Split('\') on the path and use the last item in the replace. You could also use Split-Path.

    $path = '\\server\dir1\dir2\dir3\blah2.csv'
    
    $newPath = Join-Path -Path (Split-Path $path -Parent) -ChildPath "$(Get-Date -Format yyyyMM)_$((Split-Path $path -Leaf))"
    
    $newPath


    Wednesday, July 27, 2016 3:25 PM

All replies

  • I'm not clear as to what you're actually doing, but you can try something like this:

    $outputFile = '\\server\dir1\dir2\dir3\blah.csv'
    
    $outputFile = $outputFile.Replace('blah.csv',"$(Get-Date -Format yyyyMM)_blah.csv")
    
    $outputFile



    EDIT: If you need an actual file you could do something like this instead:

    Get-Content .\log.txt | ForEach {
    
        $_.Replace('blah.csv',"$(Get-Date -Format yyyyMM)_blah.csv")
    
    } | Out-File .\logFixed.txt
    
    
    
    PS C:\Scripts\PowerShell Scripts\Misc Testing\7-27-2016> cat .\log.txt
    .LOGTABLE blah;
    .BEGIN EXPORT SESSIONS 24;
    .EXPORT OUTFILE "\\server\dir1\dir2\dir3\blah.csv"
    
    MODE RECORD FORMAT TEXT;
    
    PS C:\Scripts\PowerShell Scripts\Misc Testing\7-27-2016> cat .\logFixed.txt
    .LOGTABLE blah;
    .BEGIN EXPORT SESSIONS 24;
    .EXPORT OUTFILE "\\server\dir1\dir2\dir3\201607_blah.csv"
    
    MODE RECORD FORMAT TEXT;


    Wednesday, July 27, 2016 3:12 PM
  • I have to pull the filename from the script so I need to be able to read from the file to figure out what to replace the filename would fall between the last backslash and the .csv on that line.

    \XXXXXXX.csv
    Wednesday, July 27, 2016 3:18 PM
  • In that case you can use .Split('\') on the path and use the last item in the replace. You could also use Split-Path.

    $path = '\\server\dir1\dir2\dir3\blah2.csv'
    
    $newPath = Join-Path -Path (Split-Path $path -Parent) -ChildPath "$(Get-Date -Format yyyyMM)_$((Split-Path $path -Leaf))"
    
    $newPath


    Wednesday, July 27, 2016 3:25 PM
  • This is close to what I would need but there is 1 more step I need to be able to dynamically determine the filename so it doesn't have to be hard coded in the PS script.  Do you know how that could be added in too?
    Wednesday, July 27, 2016 3:27 PM
  • This is close to what I would need but there is 1 more step I need to be able to dynamically determine the filename so it doesn't have to be hard coded in the PS script.  Do you know how that could be added in too?

    See my last post, I added an edit.


    Wednesday, July 27, 2016 3:28 PM
  • Still having problems making this work what does the Join-Path do?

    Join-Path : Cannot bind argument to parameter 'Path' because it is an empty string.
    At line:1 char:24
    + $nef = Join-Path -Path (Split-Path $ef -Parent) -ChildPath "$(Get-Date -Format y ...
    +                        ~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidData: (:) [Join-Path], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.Join
       PathCommand

    Wednesday, July 27, 2016 4:43 PM
  • Still having problems making this work what does the Join-Path do?

    Join-Path makes it easy to create paths:

    PS C:\> Join-Path -Path C:\Temp\Folder1\Folder2 -ChildPath test.txt
    
    C:\Temp\Folder1\Folder2\test.txt
    

    http://ss64.com/ps/join-path.html


    Wednesday, July 27, 2016 5:34 PM
  • Okay my problem before was me trying to just set the filename instead of using the whole path.  I just changed the logic to pass in the whole path and it is working now.  Thanks so much for helping me through this!
    Wednesday, July 27, 2016 6:23 PM
  • Cheers, you're very welcome. Glad you got it working.


    Wednesday, July 27, 2016 6:30 PM