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"|
ForEach-Object{
$newname= "{0}{1}_{2}.csv" -f $destinationDir, $_.BaseName,[datetime]::Now.ToString('MM-dd-yyyy-hh-mm-ss')
$_|Copy-Item -Include ,$sfiles -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.
Any help would be greatly Appreciated.
-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 06 '15 at 05:18I think it is worth to provide more details and conditions, eg. each file in directory
– maoizm Oct 25 '18 at 12:37/sourceincluding (excluding) subdirectories should be copied to directory/destand renamed according to information in filea.csv. files not listed ina.csvshould (not) be copied