We help IT Professionals succeed at work.
Troubleshooting Question

Powershell: Rename one file instead of all files.

Can
Can asked
on
31 Views
Last Modified: 2020-09-24
Hi,

An application is dropping daily a CSV file into a folder. This file is then picked up by another application. The condition for picking up the CSV file is that the file has to have a specific name and should be a TXT file.

Until now the application dropped once a day this file. So running the current script once a day (which converts the CSV to TXT and gives the specific name) gets the job done. But now, since a new update, the application drops 3 CSV files at the same time. The problem I'm facing now is that those 3 files cant have the same name in the same folder. therefor only one file is picked up by the application.

What i need: I need the PowerShell script to convert just one of the CSV files to TXT and give it the specific name. But since the CSV files contain unique names (DateTime).CSV i cant get this coded in my script.
If the script converts just one file i can schedule this script to once an hour. That will get my job done. 
Comment
Watch Question

Olivier MARCHETTACitrix Support and Infrastructure Engineer
CERTIFIED EXPERT

Commented:
You can try this:

$csv = get-childitem *.csv |Select-Object Name -Last 1
$csv = $csv.Name
rename-item $csv file.txt
CanSystem Administrator

Author

Commented:
Hi Olivier

Unfortunately, it doesn't work. The script looks now like this:

$Source = "C:\DIR"
$csv = get-childitem -path $Source\*.csv |Select-Object Name -Last 1
$csv = $csv.Name
rename-item $csv -NewName "File.txt"

When i do write-host $csv its shows the correct name of the file which it should process. But when i run the script it says: rename-item : Cannot rename because item at '202009251830.csv' does not exist.

Any other idea? Thanks in advance
Olivier MARCHETTACitrix Support and Infrastructure Engineer
CERTIFIED EXPERT

Commented:
You have to include the source path in the rername-item command:

$Source = "C:\DIR"
$csv = get-childitem -path $Source\*.csv |Select-Object Name -Last 1
$csv = $csv.Name
rename-item $Source\$csv -NewName "File.txt"
CanSystem Administrator

Author

Commented:
Hi Olivier,
Thank you for your reply. Since the $CSV variable contained the $source\ path i thought it was not needed to add it to the rename-item cmdlet as well . But this worked, almost.. This is the script i have now:
$Source = "C:\Localdata"
$csv = get-item -path $Source\*.csv |Select-Object Name -Last 1
$csv = $csv.Name
rename-item $Source\$csv -NewName 'ConvertedFile.txt' 
This now works as it should be. The problem i am facing now is that the script also renames the source folder from "Localdata" to "ConvertedFile.txt"  when there is no CSV file in the folder. Since this script will be used in the task scheduler (triggered every hour), renaming folder name will cause problems.
Should we add some kind of attribute to the rename-item cmdlet?

Thanks in advance.

CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018

Commented:
You can just pass the csv item to Rename-Item (and even pass it through if you need the item for further processing):
$Source = "C:\Localdata"
$csv = Get-Item -Path $Source\*.csv | Select-Object Name -Last 1 | Rename-Item -NewName 'ConvertedFile.txt' -PassThru
If (-not $csv) {
	Write-Warning "No csv file found!"
	Exit 1
}

Open in new window

Citrix Support and Infrastructure Engineer
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CanSystem Administrator

Author

Commented:
Hi Olivier MARCHETTA & oBdA,

Thank you both for your replies.
@oBdA: This code returns me this error:
@Olivier MARCHETTA: Thank you very much. This works like a charm. Thanks for you input. 
CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.