Reputation: 31
I need to recursively search directories, and when a file of a specific name is found then rename it. The key is that I need to also be able to rename it back later.
for /r %%x in (*.aspx) do move %%x %%x.txt
The above works going forwards, but I cannot work out syntax to rename it back after.
My next thought was to instead of add an extension, maybe rename the extension, but I cannot work out how.
Anyone have any thoughts. I have searched by dos was never a great friend of mine.
I'd be happy if this where vbs if that's easier.
Upvotes: 3
Views: 3005
Reputation: 41224
Critical Note: These are command lines for use inside a batch file, and when executing them directly from a command prompt then use %x
instead of %%x
This renames the *.apsx to add .txt
to the end.
for /r %%x in (*.aspx) do move "%%x" "%%x.txt"
EDIT: There is a change below to handle recursive renaming which was left out.
This renames the *.apsx to remove the .txt
from the end.
for /r %%x in (*.aspx.txt) do move "%%x" "%%~dpx%%~nx"
Upvotes: 2
Reputation: 75
Possible duplicate Changing all files' extensions in a folder with one command on Windows.
But it'll be something like this
ren *.X *.Y
Where "X" is the original extension and "Y" is the extension you want to change.
Upvotes: 0
Reputation:
It seems you want to "hide" the files somehow. This can be done by stripping the extension from the filename. Unfortunately this cannot be done "inline" in the for
loop, so you need something like this.
for /r %%x in (*.aspx) do call :disable %%x
goto :eof
:disable
ren %1 %~n1.old
goto :eof
The sub-routine is required because you cannot write %~nX inside the for
loop. Assuming the for
loop found a file index.aspx
then the rename in the subroutine will expand to:
ren c:\some\dir\where\the\files\are\stored\index.aspx index.old
You can pass the "old" and "new" extension as a parameter
for /r %%x in (*.%1) do call :disable %%x %2
goto :eof
:disable
ren %1 %~n1.%2
goto :eof
Assuming you store the above in a batch file named "my_rename.cmd" you can run
my_rename aspx old
to "hide" the files.
To "enable" them again, you can run:
my_rename old aspx
If your files are stored in directories that contain spaces you need to change ren %1 %~n1.%2
to ren "%1" %~n1.%2
Upvotes: 0