2

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.

Tractor Boy
  • 21
  • 1
  • 2
  • 1
    Can you clarify what you mean by "be able to rename it back later"? – aphoria Apr 30 '14 at 14:30
  • If you are looking for ```vbs``` solution use Google or http://stackoverflow.com/questions/22636917/vbscript-for-moving-like-files for initial pointers. VBScript (or JScript) language is much more expressive and allows structuring your code much better. You'd then use ```cscript.exe``` to include the vbs solution into your batch file – xmojmr May 01 '14 at 11:50

3 Answers3

1

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"
foxidrive
  • 40,353
  • 10
  • 53
  • 68
  • @MichaelFreidgeim You're wrong. Which batch file did you put it into? http://i.imgur.com/eA51k2g.png That's what you see on the console without echo off. – foxidrive Jun 24 '16 at 06:44
  • Your answer uses %%x, but in image you used %x. Single percentage is working. – Michael Freidgeim Jun 24 '16 at 08:00
  • 1
    @MichaelFreidgeim No Michael, a batch script shows the single percent on the console when it runs but has two percent signs inside it. The single percent is used when from run directly from the command prompt. I gather you noticed stuff along those lines from your edit, and it's the subject line mentioning batch file which was relevant. I am often not as terse in my reply as I was here and understand that I commented very little. Ta. Note the correction I made my script above, and I included your info more prominently. – foxidrive Jun 25 '16 at 12:56
0

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

0

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.

Community
  • 1
  • 1
DavideBar
  • 75
  • 10