Solved

Renaming using Powershell

Posted on 2011-09-19
6
643 Views
Last Modified: 2012-05-12
Question: I have over 400 directories and within each of those directories there is a *.ini file that I need to query for a value.  Once I have that value I need to rename the text document in that directory, that value.  I'm looking at accomplishing this task in powershell since the rest of the application is powershell.

Below is an example

File 1
   - File 1a
      - document1.ini (The value that I need resides in this *.ini file)
      -document1.txt (The value that I found in document.ini, I need to rename this file to that value)

File 2
   -File 2a
        -document2.ini (the value that I need resides in this *.ini file )
       -document2.txt (The value that I found in document2.ini, I need to rename this file to that value)

0
Comment
Question by:wnbtechnology
  • 3
  • 2
6 Comments
 
LVL 2

Expert Comment

by:gazm81
ID: 36563435
is there only one value or entry in the ini file?
is there only one file in each folder?
0
 

Author Comment

by:wnbtechnology
ID: 36563516
There is only one value in the ini file.. The entry format is like this: Value=123456789 (I need only "123456789", i need to parase out "Value").

There is only one *.text file in each folder and only one *.ini file in each folder
0
 
LVL 69

Accepted Solution

by:
Qlemo earned 125 total points
ID: 36563669
Something like this?
gci C:\StartFolder -recurse -Filter *.ini | % {
  $txt = gci ($_.DirectoryName + "\*.txt")
  rename-item $txt ((gc $_.Fullname).Split("=")[1] + ".txt")
}

Open in new window

0
Backup Solution for AWS

Read about how CloudBerry Backup fully integrates your backups with Amazon S3 and Amazon Glacier to provide military-grade encryption and dramatically cut storage costs on any platform.

 

Author Comment

by:wnbtechnology
ID: 36566879
Qlemo,
     I'm getting an error "Method invocation failed because [System.Object[]] doesn't contain a method named 'split' at line:1 char:139

Below is a little better example of the structure.

C:\folder\test

            -12345678.folder1
                        -Folder 1
                             -document 1.ini <<The value resides in this file ex: value=125458456
                             -test.txt<<the value that was captured in document 1.ini,I need to rename this file to that value 125458456 minus the "value" word. The end result will be 125458456.txt instead of test.txt

            -23452345.folder1
                        -Folder1
                            -document 1.ini <<The value resides in this file ex: value=123456789
                            -test.txt<<the value that was captured in document 1.ini,I need to rename this file to that value 123456789 minus the "value" word. The end result will be 123456789.txt instead of test.txt
       

          -0998767475.folder1
                         -Folder1
                              -document 1.ini <<The value resides in this file ex: value=987654321
                              -test.txt<<the value that was captured in document 1.ini,I need to rename this file to that value 987654321 minus the "value" word. The end result will be 987654321.txt instead of test.txt
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 36567748
The (already tested) script has been applied against your folder structure, and with success. I do not get that error message.
Is
    (gc "c:\folder\test\12345678.folder1\Folder 1\document 1.ini").Split("=")
working?
0
 

Author Closing Comment

by:wnbtechnology
ID: 36716484
I didn't use the exact solution that was provided.  I was able to use some of the powershell cmdlets that were provided to come up with another way of renaming the files that I needed renaming.

Thank you for all your help Qlemo
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Synchronize a new Active Directory domain with an existing Office 365 tenant
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question