Computing.Net > Forums > Programming > Srch Rplce Characters in FOLDER Nam

Srch Rplce Characters in FOLDER Nam

Reply to Message Icon

Original Message
Name: Barnster
Date: June 4, 2008 at 18:47:40 Pacific
Subject: Srch Rplce Characters in FOLDER Nam
OS: Win XP
CPU/Ram: 3Ghz 2GB RAM
Model/Manufacturer: Compaq
Comment:

Wondering if some one can help me write a batch job to execute the following.
Need to point script to a root folder.
Script scans all sub-folders.
Searches for the period character (.) and replaces it with dash character (-) however ONLY on FOLDER NAMES.
Script MUST NOT rename files ONLY FOLDERS NAMES.
Ideally can specify what the search character is, even possibility of specifying multiple characters. E.g replace (.) (:) (;) all with (-)


Report Offensive Message For Removal

Response Number 1
Name: Mechanix2Go
Date: June 4, 2008 at 19:26:11 Pacific
Subject: Srch Rplce Characters in FOLDER Nam
Reply: (edit)

Is this a homework assignment?


=====================================
If at first you don't succeed, you're about average.

M2


Report Offensive Follow Up For Removal

Response Number 2
Name: Barnster
Date: June 4, 2008 at 19:32:59 Pacific
Subject: Srch Rplce Characters in FOLDER Nam
Reply: (edit)

No not at all, we have a mixed environment (Mac & PC). I have an automated file/folder delete routine whereby if file date over 30 days old it is deleted. However system has issues deleting folders if the folder name contains any period characters. Which is why i want to run a script to search and replace these characters in folder names first, before file/folder deletion is executed.


Report Offensive Follow Up For Removal

Response Number 3
Name: Mechanix2Go
Date: June 4, 2008 at 20:08:17 Pacific
Subject: Srch Rplce Characters in FOLDER Nam
Reply: (edit)

@echo off
setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in ('dir/b/s/ad ^| find "."') do (
set str=%%~NXa
set str=!str:.=-!
echo ren %%a !str!
)


=====================================
If at first you don't succeed, you're about average.

M2


Report Offensive Follow Up For Removal

Response Number 4
Name: ShaqDiesel
Date: June 6, 2008 at 22:31:12 Pacific
Subject: Srch Rplce Characters in FOLDER Nam
Reply: (edit)

on the unix command line:

find . -type d -exec sed -i.ori 's/[.:;]/-/g' {} \;

This should find all directories starting at your current directory and if it finds the regex in the character class it will replace it.

Correct me if I'm wrong but I believe even if there is both a ':' and a ';' it will replace both with a '-'

Suppose the regex to search for was longer, is there another way to replace multiple matches other than using the -e option and retyping the s/old/new command again? For example:

sed -e 's/dog/cat/g'; 's/moose/bear/g' file


Report Offensive Follow Up For Removal

Response Number 5
Name: Barnster
Date: June 9, 2008 at 18:22:03 Pacific
Subject: Srch Rplce Characters in FOLDER Nam
Reply: (edit)

Sorry A little lost as to how do I use these commands, I have created a batch file 'RemoveUnwantedCharacetrs.cmd' and copied this text string into it, how do I specify the root folder where i want this to execute from?


Report Offensive Follow Up For Removal


Response Number 6
Name: Mechanix2Go
Date: June 10, 2008 at 00:45:17 Pacific
Subject: Srch Rplce Characters in FOLDER Nam
Reply: (edit)

pushd d:\mystuff


=====================================
If at first you don't succeed, you're about average.

M2


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Srch Rplce Characters in FOLDER Nam

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software