Bactch command to check if a file type exists and then rename it

In batch, check if a file type exists at a specific folder  
For example, if at C:\folder\file it sees any *csv file, it should rename it to File1.csv.
100questionsAsked:
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.

Jacob DurhamIT Support Analyst II (Lead Infrastructure Engineer)Commented:
Does it have to be batch? Will powershell work?


This will name them file01, file02, etc.

$i = 1
get-childitem *.csv | foreach-object {rename-item $_ -NewName ('file{0:D2}.csv' -f $i++)}

Open in new window


This will keep the name but just add a number to the end.

$i = 1
get-childitem *.csv | foreach-object {rename-item $_.Name -NewName  ($_.BaseName+'{0:D2}.csv' -f $i++)}

Open in new window

0
Bill PrewCommented:
For example, if at C:\folder\file it sees any *csv file, it should rename it to File1.csv.

I don't understand this, if a file already exists with a CSV extension, then there would be no need to rename it as a CSV file.


»bp
1
100questionsAuthor Commented:
Hi Bill.
Let me clarify.

1)
In batch, check if a file type exists at a specific folder.

2)
For example, if at C:\folder\file it sees any *csv file, (this would include any file name which ends in *csv)

3) it should rename that file to "File1.csv."
(So if it sees a file called adlk;adfjkl;akjf;lsjkdl;afds.csv, it should rename that file to "File.csv"
If it sees a file called "ABCDEF.csv" it should rename that file to "File.csv"
If it sees a file called 7890097890789070789.csv, it should rename that file to "File.csv"

If this is still not clear, please let me know.  Thanks.
0
Bill PrewCommented:
Okay, so that only works if there is only one CSV file in the folder, so I'll assume that is the case.  In which you can just do:

REN "C:\folder\file\*.csv" "*.csv"


»bp
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
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
Windows 10

From novice to tech pro — start learning today.