Question: I need to find a given file, 123.txt, in all the subdirectories I have, and then add the particular sub-directory's name to that file. And even FORCE the bat file to overwrite existing file each time it's run.
For example:
 -folder
 --subfolder1
 ---123.txt
 --subfolder2
 ---123.txt
To sum up, a given file 123.txt is in many subdirectories.
I would want to have all 123.txt files RENAMED as NEW files (overwrite each time) with folder names added to them (separated by dash). So I need files like 
 --subfolder1
 ---subfolder1-123.txt
 --subfolder2
 ---subfolder2-123.txt
Please help. A batch file doing this please.

Answer: 
I'd probably do something like this: Loop recursively over all folders, check if a file called 123.txt exists, and if so, rename it. Note that `~n` returns the name part of the directory contained in the `%%d` variable.

Save the following to a file with a `.bat` extension and run it from the directory where your 123 files are located.

```sh
@echo off
for /r /d %%d in (*.*) do (    
    if exist "%%d\123.txt" ( 
        rename "%%d\123.txt" "%%~nd-123.txt"
    )
)

```

Or as a oneliner to be run from a command prompt directly (note the single `%` characters):

```sh
for /r /d %d in (*.*) do (if exist "%d\123.txt" rename "%d\123.txt" "%~nd-123.txt")

```

Help for most of the above can be read by typing `for /?` on a command prompt.
Here's a PowerShell alternative:

```
gci C:\somewhere -r -filter 123.txt | % { rni $_.FullName -NewName ( "$( split-path (split-path $_.FullName -parent) -leaf )-123.txt" ) }

```

You can change the path in `gci` to your topmost directory and it will look for 123.txt in every subfolder there is and rename it with parentfolder-123.txt

run this from your PowerShell window