Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In my SQL files, I have hardcoded the C:\ directory into all the output file paths, but there are times where the client wants to have the outputs stored on E:\, for example.

How do I go about doing this with only batch? I thought about using PowerShell, but it isn't enabled on the client machines.

What I'd like to do:

  1. Recursively go through all the directories.
  2. In each directory, find C:\ and replace with E:\.
share|improve this question
up vote 1 down vote accepted

Yuck, blindly doing wholesale substitution like that on an entire directory tree structure would make me nervous. But if you think it is safe...

It is potentially tricky do do what you want using pure batch, depending on your file content. But it is easy to do with a hybrid JScript/batch utility I wrote called REPL.BAT. The utility performs regex search and replace on stdin, and writes the result to stdout. It is pure script that runs on any modern Windows machine from XP forward, so it does not require installation of any 3rd party .exe file. The REPL.BAT utility and documentation is available here..

Assuming you have REPL.BAT in your current directory, or better yet, somewhere within your PATH, then the following simple one liner should do the trick directly from the command line, no batch required.

for /r %F in (*.sql) do @(type "%F"|repl "C:\" "E:\" >""&move /y "" "%F" L >nul)

You can make the search case insensitive by adding the I switch.

for /r %F in (*.sql) do @(type "%F"|repl "C:\" "E:\" I >""&move /y "" "%F" L >nul)

Or if you prefer a batch script (with I switch)

@echo off
for /r %%F in (*.sql) do (
  type "%%F"|repl "C:\" "E:\" LI >""
  move /y "" "%%F" >nul

I used the L literal switch throughout; the other option is to escape the \ instead.

repl "C:\\" "E:\\" I
share|improve this answer
I tried Option 3, but I get The process tried to write to a nonexistent pipe. Test Folder\REPL.bat(191, 1) Microsoft JScript runtime error: Syntax error in regular expression – Bob. Aug 29 '13 at 13:19
@Bob - Doh! Of course you get that error. I forgot that `\` is a regex meta character. Sorry about that. I've edited my answer - all should be fixed now. – dbenham Aug 29 '13 at 13:33
Awesome! I had to remove @echo off so I knew it was still working and not just sitting there. – Bob. Aug 29 '13 at 14:05
Is there a way to make it look in ./SQL recursively? If someone accidentally puts other directories that contain SQL, I don't want to overwrite those. – Bob. Aug 29 '13 at 14:07
@Bob - Rather than remove @echo off, I would echo %%F within the loop so that I could monitor the progress. I'm not sure I understand your follow-up question. Perhaps it is worth posting a new question. – dbenham Aug 29 '13 at 14:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .