Solved I am looking for a way to mass update folder names

July 3, 2012 at 11:02:30
Specs: Windows 7
I am looking for a way to mass update folder names.
We have a folder hirarchie that is several levels deep. Some of the folder names contain the "&" symbol. (such as /Buyer & Seller/)

We want to move our documents to a sharepoint document library but special characters are illegal in folder file names.

I want to rename the folders that have "&" symbol in them to change the symbol to a dash, i.e.

IE: "Buyer & Seller" to: "Buyer - Seller", so we need to have a way to find "&" and replace with "-" in any of the folder names.

We are talking about thousands of folders with many hundreds containing the symbol in the folder name.

Please advise, thank you!!

See More: I am looking for a way to mass update folder names

Report •

July 3, 2012 at 14:06:09
✔ Best Answer
@echo off > temp.bat & setlocal enabledelayedexpansion
:this is the base of your tree-structure
set target=.\base
:this is recommended as a safety, but disabled here
:xcopy /s /t /e %target% .\backup
:two files will be created in this directory (one level below the target): temp., and. temp.bat. Make sure these aren't overwriting any existing valued files.

dir /b /s /a:d %target% | sort /r>temp
for /f "tokens=*" %%a in (temp) do (
set b=%%~nxa
set c=%%~pa%
set d=%%~nxa
set d=!d:^&=-!
if !d! neq !b! >> temp.bat echo ren "!c!!b!" "!d!"

I "safetied" this script so that you can examine the output (in file temp.bat) before doing any renaming. The reversed sort should avoid renaming of base directories ahead of their subdirectories (which would leave the subdirectories dangling out there with ampersands still in place.) If temp.bat looks ok, just run it. I would also suggest enabling the xcopy line for backup. If you want a logfile of the operation, pipe temp.bat out to a logfile: temp > temp.log.

Report •
Related Solutions

Ask Question