2
votes

I am trying to write a PowerShell script to do the following.

  1. Rename files in source (FTP folders) directories with it's "current_name_datetime.csv" as per a source file "Source_list.csv" this file has the directories "source,destination" I want this script to look into.

  2. Copy newly renamed files to backup directories as per destination in Source_list.csv this file has the directories "source,destination" I want this script to look into.

  3. Move newly renamed files to final destination directory which is not in my current script.

Source_list.csv contents

cscenter,Costume_Supercenter

fkimports,FKImports

My Script:

$sdfiles = Get-Content c:\!tony\Source_list.csv
$sourceDir = "c:\test\"
$destinationDir = "c:\testing\"

Get-ChildItem $sourceDir -Recurse -Include $sdfiles "*.csv"|
ForEach-Object{
       $newname= "{0}{1}_{2}.csv" -f $destinationDir, $_.BaseName,     [datetime]::Now.ToString('MM-dd-yyyy-hh-mm-ss')
    $_|Copy-Item -Include ,$sdfiles -Destination $newname -whatif }

Error:

What if: Performing operation "Copy Directory" on Target "Item: C:\test\cscenter Destination: C:\testing\cscenter_10-01-2015-12-22-24.csv".

I see in the error that it is trying to copy the directory not the single file in each directory and creating a new folder using the original folder name and renaming the folder and appending the date/time stamp.

1
Can we see what a couple of lines in Source_list.csv look like?Matt
Also whatever $sfiles is. Unless that is a typo then that would be an issue.Matt
Source_list.csv cscenter,Costume_Supercenter fkimports,FKImportsTonyrcom
$sfiles should be sdfilesTonyrcom
Please edit the question with these details so I can see the structure betterMatt

1 Answers

0
votes

Confused. The -Include parameter should only be accepting a single array of strings, throwing "*.csv" on to the end of it won't work AFAIK. Additionally It will be interpreting the whole line of the CSV, ie searching for the file "cscenter,Costume_Supercenter" so shouldn't be returning anything. At least that's what I see when I replicate this on my computer.

Lastly you've tried to filter the files, piped that to Copy-Item and tried to filter it again?

I'd take a more straightforward approach:

$sdfiles = Import-CSV c:\!tony\Source_list.csv -Header @("File", "Folder")

$sourcedir = "c:\test\"
$destinationdir = "c:\testing\"

$sdfiles | ForEach-Object {
    $path = $sourcedir + $_.File + ".csv"
    $folder = $destinationdir + $_.Folder + '\'
    if (!(Test-Path $folder)) { New-Item -Type Directory -Path $folder }
    if (Test-Path ($path))
    {
        $newname = "{0}{1}_{2}.csv" -f $folder, $_.File, (Get-Date).ToString('MM-dd-yyyy-hh-mm-ss')
        Copy-Item -Path $path -Destination $newname -whatif
    }
    else { Write-Error "File $($_.File) not found" }
}

It's a bit chunkier but much easier to read and tweak to your liking. Note that Import-CSV does require PowerShell v3. Let me know if you've got v2 and need help tweaking it for a two-dimensional array.

I also recommend looking into Microsoft's MVA courses on PowerShell, they are excellent resources for starting out.