Link to home
Start Free TrialLog in
Avatar of michalek19
michalek19Flag for United States of America

asked on

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
Avatar of Qlemo
Qlemo
Flag of Germany image

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

Avatar of michalek19

ASKER

I tested but runs without any error but data is not exported or renamed
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.
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
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.
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
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

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
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.
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
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.
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
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.
that would be great and i a,m sorry for confusion
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Great works like charm.
is possible to add function to generate log file "output file"
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.
ok, thank you
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

https://www.experts-exchange.com/questions/28668436/Powershell-script-that-would-remove-printers-from-print-servers-base-on-CSV-file-and-add-new-printer-objects-to-print-servers.html