Mulitple Basename(s) to one destination folder

Welcome Forums General PowerShell Q&A Mulitple Basename(s) to one destination folder

This topic contains 1 reply, has 2 voices, and was last updated by

3 years, 3 months ago.

  • Author
  • #28740

    Points: 0
    Rank: Member

    Hi folks,

    How to multiple different file names without extension to a single folder? e.g

    we have two different path name (D:\Folder_1\aa.txt and D:\Folder_2\bb.txt). I want only two filename without extension ($file.BaseName) to the same folder (D:\OtherFolder\aa.csv and ....\bb.csv)

    Here is my sample script as below:

    $fileList = dir 'D:\Folder_1\aa.txt','D:\Folder_2\bb.txt'
    $destinaion = 'D:\OtherFolder\'
    $newExtension = ".CSV"
    foreach ($file in $fileList){
    Get-Content $file | ForEach-Object {($_ -replace "\s+",",")} |
    Out-File -FilePath $($destination + $newExtension) -Encoding OEM

    Any Idea? Thanks

  • #28744

    Points: 48
    Rank: Member

    The first problem I notice is that you're not using the file's basename in your out-file command. Therefore in effect what you're passing to the Out-File -FilePath parameter is in effect this: "C:\OtherFolder\.CSV".
    Try to create the whole filepath for the new file in one variable and pass that to the -FilePath parameter of Out-File
    My assumption from your code is that you're trying to take a tab delimited file and convert it into a CSV, however, have you considered the -Delimeter parameter of Import-Csv? You can copy the files over like this:

    Get-ChildItem 'D:\Folder_1\aa.txt','D:\Folder_2\bb.txt' | Copy-Item -Destination 'C:\OtherFolder'

    Then if you need to import the files into your console use:

    #note the backtick (`) before the t
    Import-Csv C:\OtherFolder\aa.txt -Delimeter "`t"

    EDIT: Actually I may have been hasty to respond. If your input file is delimited by "one space or more" then the "`t" won't work, it needs to be a tab character, so you will still end up having to replace the "once space or more".
    Could you share a sample of your input files? I could possibly provide a better solution knowing the structure of the content of the files.

The topic ‘Mulitple Basename(s) to one destination folder’ is closed to new replies.