Solved

Renaming using Powershell

Posted on 2011-09-19
6
633 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 68

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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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 68

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Why would I want to create a function for tracking messages? I am glad you asked. As with most monotonous/routine tasks, human error tends to creep in after doing the same task over and over again. By creating a function, you load the function once…
How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

760 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now