I need some help with renaming files using a batch script...

Say in a folder I have 2 html files. -
1.htm
2.htm

I need a bat file that will rename "1.htm" to "1a.htm" and then change "2.htm" to "1.htm"

In other words I need "2.htm" to become "1.htm"...

Anyone know how I can do this using a bat file?

cheers.

Dani AI

Generated

A handy, practical improvement to what and have shown: the two-step rename you use works, but it can leave the site in a broken state if the script stops midway. Use a short, unique temporary filename for an atomic swap (A -> TMP, B -> A, TMP -> B) and add existence/error checks so the script can restore or abort on failure.

@echo off
pushd "%~dp0"
rem safe two-file swap: A -> tmp, B -> A, tmp -> B
set "A=current.htm"
set "B=alternate.htm"
set "T=swp_%RANDOM%_%RANDOM%.tmp"
if not exist "%A%" echo Missing %A% & goto :done
if not exist "%B%" echo Missing %B% & goto :done
ren "%A%" "%T%" || (echo rename A->T failed & goto :restore)
ren "%B%" "%A%" || (echo rename B->A failed & goto :restore)
ren "%T%" "%B%" || (echo rename T->B failed & goto :restore)
:done
popd
goto :eof
:restore
if exist "%T%" ren "%T%" "%A%"
popd

Notes and tips: run the .bat from the same folder (the script uses %~dp0), test on copies first, and ensure files are not locked by IIS/other processes. If you need full safety for a single published page, consider writing the chosen state to a temporary file and then renaming that temp to the live filename (copy-then-rename) so the web server always has a complete file. Also remember browser/proxy caching and antivirus file-scanners can affect how quickly changes are visible. 's chuckle aside, this pattern dramatically reduces the “half-done” problem you flagged.

Recommended Answers

All 4 Replies

from the command prompt:
navigate to the directory (folder) containing your files to be renamed.

create the batch file like this:

copy con renameme.bat
ren 1.htm 1a.htm
ren 2.htm 1.htm

strike [enter] after each line
when done,
strike [ctrl]-z then [enter]

copy con
means copy the output of the console
renameme.bat is the file that will be created
the other two lines will be added in turn to renameme.bat
the [ctrl]-z key combination, when followed by the [enter] key tells the copy command that you are done, and writes the file to disk

then, from the command prompt, simply type

renameme

followed by the [enter] key

and, voila'! instant file rename!!

Thanks for the help.

I've changed it slightly. New scrip reads...

@ECHO OFF

ECHO Changing to status 2...

ren status1.htm status1a.htm
ren status2.htm status1.htm

PAUSE

ECHO Changing to status 3...

ren status1.htm status2a.htm
ren status3.htm status1.htm

PAUSE

ECHO Changing to status 1...

ren status1.htm status3.htm
ren status1a.htm status1.htm
ren status2a.htm status2.htm

ECHO Done

PAUSE

This alows me to have 3 static pages that display status info, to change at the click of a button without updating any pages or html links. Only draw back is pages can become muddled if the script ends half way through - (ie before all pages are back to their original file names.)

Thanks again.

slightly!!lol

lol indeed so!

now i see ~why~ you wanted this. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.