Export-CSV and rename and Add date to file name

Hi
I need to migrate files to a different location, however file names are generated with random names like xderv.csv, hehsa.csv, gehskd.csv, etc...
PS Script should copy the most current file (the youngest) from \\server to \\new\share destination and rename it to supplydaymonthyear.csv
This script would be run 1st day of each month so I need to make sure daymonthyear is included in file name

Thx,Mike
michalek19Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Assuming you want to take over the file date into the file name:
get-childitem \\server\share -filter "*.csv" |
  sort CreationTime -desc |
  select -First 1 | 
  copy-item -dest {"\\new\share\supply{0:ddmmyyyy}" -f $_.CreationTime}

Open in new window

0
michalek19Author Commented:
I tested but runs without any error but data is not exported or renamed
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
The only time the script does nothing, and not show errors, is if there are no files found.
get-childitem \\server\share -filter "*.csv"

Open in new window

should show some files.
0
Challenges in Government Cyber Security

Has cyber security been a challenge in your government organization? Are you looking to improve your government's network security? Learn more about how to improve your government organization's security by viewing our on-demand webinar!

michalek19Author Commented:
correct, i forgot to copy some csv file for test.
dah...:)

However, when the files are renamed i want call them like DeviceUtalization correct (date, month, year)
Please check attachment how the current output look like.

Would that be possible to add to the script "log file" and send email with confirmation of successful migration

Thx, Mike
renamed-file.png
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
The code I showed does exactly what you asked for first.
I'm not certain I understand your request. Is The DeviceUtilization a CSV file, or how should we know which file to take? You didn't make the name column large enough in above screenshot, and it does not look at all like in your initial question. Please try to be clear and concise, and don't change test data nor targets if avoidable.
0
michalek19Author Commented:
Can you help me to customize this script so when the file is migrated over creates file as  DeviceUtalization(day,month,year) file.csv?

Right now, only create random number that is hard to follow which one is which
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Still not getting you, there is no "random number". The script creates a "supplyDDMMYYYY" file. Of course I can change that to "DeviceUtilizationDDMMYYYY.csv":
get-childitem \\server\share -filter "*.csv" |
  sort CreationTime -desc |
  select -First 1 | 
  copy-item -dest {"\\new\share\DeviceUtilization{0:ddmmyyyy}.csv" -f $_.CreationTime}

Open in new window

0
michalek19Author Commented:
Hi Qlemo

Please check this attachment. It is word document with all explanation and pictures.

1.DeviceUtilization file in not copy to correct directory
2.DeviceUtilization has wrong time stamp. The number is unknown for me

Thank you, Mike
DeviceUtilizexplanation.docx
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
That's much more clear now. You want to have the files in the folder DeviceUtiliization, but also use that as file name. We are almost there. The number is, btw, not random - it is (day, minute, year) of the script execution, as it seems. No clue why it isn't the file creation date, as that works fine for me:
get-childitem \\xxxxxxxx\C$\Windows\ServiceProfiles\NetworkService\AppData\Local\Hewlett-Packard\HPWebJetadmin\WjaService\Reports\DeviceUtilization\ -filter "*.csv" |
  sort CreationTime -desc |
  select -First 1 | 
   copy-item -dest {"\\xxxxxxxxx\Public\PrintMetrics\DeviceUtilization\DeviceUtilization{0:ddMMyyyy}.csv" -f $_.CreationTime}

Open in new window

BTW, my recommendation for file names is to use yyyyMMdd instead, as that sorts correct.
0
michalek19Author Commented:
I ran this script two times and here is command.

Now the file is created with correct(ddmmyyyy) but overwrite existing file with the same name.

is it possible for the script to don't overwrite "DeviceUtilization06052015" but create another file with "2-DeviceUtilization06052015"?

So, i can keep a copy of each migrated file.
file-migrated.docx
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
That does not make any sense, unless you run the script several times for different folders. But then you need something different to be able to assign file to original folder.
Running the same script for the same folder at the same date won't end in a different result, no? The script will find the same file each time, and it doesn't matter if it copies each time or not - no change of the resulting file.
And as soon as a new file is in that folder, that one will be copied.

 Probably you are not communicating sufficiently what you *really* want to achieve, or you have a flaw in your test scenario.
0
michalek19Author Commented:
OK, so if the script will run 3 times a day and migrates new "csv" file what would happen  to previous migrated file in destination folder.
Is it going to be overwrite ?  If yes then that's not good because i need all of these files in destination folder to be  archived.
They can't be deleted
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
What about your statement "This script would be run 1st day of each month"?
If you really need to run the script several times a day, we need to add the time portion to the filename, too.
0
michalek19Author Commented:
that would be great and i a,m sorry for confusion
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
get-childitem \\xxxxxxxx\C$\Windows\ServiceProfiles\NetworkService\AppData\Local\Hewlett-Packard\HPWebJetadmin\WjaService\Reports\DeviceUtilization\ -filter "*.csv" |
  sort CreationTime -desc |
  select -First 1 | 
   copy-item -dest {"\\xxxxxxxxx\Public\PrintMetrics\DeviceUtilization\DeviceUtilization{0:yyyyMMdd-HHmm}.csv" -f $_.CreationTime}

Open in new window

now changes format to yyyymmdd and appends (military) hours and minutes. Remember we are using the file dates here - if there is no new file, you won't see a change (the old file is overwritten with an identical copy).
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
michalek19Author Commented:
Great works like charm.
is possible to add function to generate log file "output file"
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
For what purpose? There is nothing more to log than the copy, and you see the result as a file ... Do you like to see which was the original file name? That's useless IMHO.
0
michalek19Author Commented:
ok, thank you
0
michalek19Author Commented:
Would you be willing to take a look on this ticket

I need assistance with this asap
If you need any other explanation please let me know

http://www.experts-exchange.com/Programming/Languages/Scripting/Powershell/Q_28668436.html
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.