1

I have a file C:\dir1234\text.pdf. Need to rename that file as "dir1234_text.pdf". How can I do it? I have hundreds of directories with a similar names as "dir1234" containing one file each named "text.pdf". My guess is I need a for-next loop to go thru each directory to rename every file. Any ideas? Thanks a lot. Yury

3
  • 2
    Welcome to Super User! This isn't a script writing service, so you're unlikely to get an answer to your question. If you've already tried writing a script yourself, please share what you wrote and what doesn't work. Please also indicate what operating system you're using, as right now it's essentially impossible to properly answer this question.
    – Worthwelle
    Dec 5, 2019 at 16:27
  • 2
    Powershell would be the easiest tool to do this. You can use the dir command and pipe its content into an object, then loop through that object and continue from there, referring back to the first instance. That said, aside of us not writing scripts, your question lacks the necessary information to actually create a script too.
    – LPChip
    Dec 5, 2019 at 16:51
  • You could ask on software recommendations for an app which helps
    – Mawg
    Dec 6, 2019 at 8:50

1 Answer 1

1

Though I agree that one should not just give a man a fish, it may not be in the best spirit of helping to welcome them by saying "Come back when you at least have a boat...." We all wrote our first script and it may have been the first time someone figured hey I could solve this with code and/or used a forum.

too many new forum members (Here and elsewhere) get welcomed with "Hi glad to have you, lets assume you read all rules, and thought through a better plan than you asked." Post #1 is a dead giveaway one may not have adjusted to the culture yet.

Just my $.02...

On subject though, Assuming you are using Windows or Linux you can get some variant of this working, though it would not be my fist way of doing it on Linux.

Using the following as an example, should get you started both trying and reading a functional sample without outright teaching you nothing. I would suspect if you read and comprehend what is there, modifying it into a full solution that actually does the copy should require minimal effort with a basic understanding in hand. We would be happy to assist with any further specific questions if any of the below seems confusing. Remember almost all code has different ways to do the same thing, sometimes it is efficiency that dictates which is preferred, sometimes it is style. This is not even in my opinion the best way to do this, but it is decent enough for demonstration of how to learn to do things just like this.

$files = Get-ChildItem -Path c:\yourpath\ -Include *.txt -Recurse

# $files is a variable that will be assigned the return of the cmdlet Get-ChildItem
# Ref & Syntax =>  https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-childitem?view=powershell-6


    # Enumerate each file in the collection files
    # Ref and Syntax => https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/foreach-object?view=powershell-6
    # They will be of type System.IO.FileInfo
    # Ref and Syntax => https://docs.microsoft.com/en-us/dotnet/api/system.io.fileinfo?view=netframework-4.8 
    foreach ($file in $files)
    {
        #The rest is just getting to the sub parts you need, then combining them
        Write-Host "$($file.FullName) would be renamed $($file.Directory.Name)_$($file.Name)"
    }

If you want to engage further at that point , I or someone else would be happy to assist you further. And welcome to the forum :-)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .