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

step 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.

step 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.

step 3

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

---------------------My Script ----------------------

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

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


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.

Any help would be greatly Appreciated.

  • Did you try to write on the screen the command you're trying to execute instead to try to execute it. It's a usual debug technique... often in the abstraction step it can happens to miss something that the application to a real case point out... :) – Hastur Oct 2 '15 at 15:34
  • Yes i have tried that also, no luck again I am very new to power-shell and are in need of help. – Tonyrcom Oct 2 '15 at 15:50
  • 2
    This looks weird to me: -Include $sdfiles. The -Include parameter takes a file pattern, not a file. I think you should break up your script to use intermediate variables (e.g. $csvFiles = Get-ChildItem $sourceDir -Recurse -Include $sdfiles "*.csv" ). Then single-step through the script in the PowerShell ISE and examine the contents of the intermediate variables to see if they contain what you are expecting. Basically you need to learn how to debug. – dangph Oct 6 '15 at 5:18
  • It would seem that this task cannot be completed in a few lines of code , I think the files need to be renamed in their current directory then copied to back up folder and finally moved to the end location. – Tonyrcom Oct 6 '15 at 14:08
  • Both the description of steps and example of script are ambiguous - I can not understand from above what exactly you are going to accomplish. I think it is worth to provide more details and conditions, eg. each file in directory /source including (excluding) subdirectories should be copied to directory /dest and renamed according to information in file a.csv. files not listed in a.csv should (not) be copied – maoizm Oct 25 '18 at 12:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.