powershell - How to rename a file under an unknown directory, using another directory's name -
i need rename file (or copy new name) based on directory name, under unknown directory name.
i have numerous dated folders, sub-directories. under each sub-directory, file named index.txt
. txt file needs renamed or copied name of directory above it. name of each of sub-directories known can used in script if necessary.
for example:
>tree c:\test /f c:\test ├───20170414124512 │ ├───directory1 │ │ index.txt │ │ │ ├───directory2 │ │ index.txt │ │ │ └───directory3 │ index.txt │ └───20170414134345 ├───directory1 │ index.txt │ ├───directory2 │ index.txt │ └───directory3 index.txt
i need end result this:
- c:\test\20170414124512\directory1\directory1.txt
- c:\test\20170414124512\directory2\directory2.txt
- c:\test\20170414124512\directory3\directory3.txt
- c:\test\20170414134345\directory1\directory1.txt
- c:\test\20170414134345\directory2\directory2.txt
- c:\test\20170414134345\directory3\directory3.txt
**sorry, should have included tried. tried , did not work.
for /d /r "c:\test" %%a in (*) echo ren "%%a\index.txt" "%%~na.txt"
to avoid trying rename non existing index.txt files additonal if aschipl's suggestion should do:
@echo off /d %%j in ("c:\test\*" ) /d %%i in ("%%~fj\*" ) ( if exist "%%~fi\index.txt" echo ren "%%~fi\index.txt" "%%~ni.txt" )
if output looks ok, remove echo in last line:
ren "c:\test\20170414124512\directory1\index.txt" "directory1.txt" ren "c:\test\20170414124512\directory2\index.txt" "directory2.txt" ren "c:\test\20170414124512\directory3\index.txt" "directory3.txt" ren "c:\test\20170414134345\directory1\index.txt" "directory1.txt" ren "c:\test\20170414134345\directory2\index.txt" "directory2.txt" ren "c:\test\20170414134345\directory3\index.txt" "directory3.txt"
Comments
Post a Comment