[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 896
  • Last Modified:

Script to replace known character in folder name with space

I need a VBScript or batch file that will replace a character in a group of folders. for example _ or . replaced with a space.

Thanks.
0
madlan
Asked:
madlan
  • 4
  • 3
1 Solution
 
käµfm³d 👽Commented:
VbScript
dim folders
dim fso
 
set fso = CreateObject("Scripting.FileSystemObject")
set folders = fso.GetFolder("C:\Documents and Settings\kaufmank\Desktop\NewFolder")
 
for each folder in folders.subFolders
  folder.name = replace(replace(folder.name, "_", " "), ".", " ")
next

Open in new window

0
 
käµfm³d 👽Commented:
Obviously, change the parent folder to the relevant parent on your system.
0
 
madlanAuthor Commented:
Hi, thanks - the script returns a file already exists error when running it?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
käµfm³d 👽Commented:
That means you are trying to name a folder to something that already exists in that directory. So if you have two directories:

Some.Directory
--and--
Some_Directory

The script will fail when it tries to name the second directory.

See below for a modified version that will create something like

Some Directory1
Some Directory2
dim folders
dim fso
dim temp
dim i
 
set fso = CreateObject("Scripting.FileSystemObject")
set folders = fso.GetFolder("C:\Documents and Settings\kaufmank\Desktop\NewFolder")
 
for each folder in folders.subFolders
  temp = replace(replace(folder.name, "_", " "), ".", " ")
  i = 1
  
  while fso.FolderExists(temp)
    temp = temp & cstr(i)
  wend
  
  folder.name = temp
next

Open in new window

0
 
madlanAuthor Commented:
Hi kaufmed, there are several hundred folders that need renaming - these folders contain several files of various types. Is it not possible to rename the original folders rather than create new?

Folder structure is:

-Parent folder
                     -Folder_1
                     -Folder_2
                     -Folder_3

I need the underscore removed from each folder.
0
 
AmazingTechCommented:
Give this a try.

Doesn't handle dups but is that required?

kaufmed and madlan,

I think the error file already exists may be due to no change in name.

SETLOCAL ENABLEDELAYEDEXPANSION
Set BaseFolder=C:\Folder
for /f "tokens=*" %%a in ('dir /ad /b "%BaseFolder%"') do (
    Set NewName=%%a
    Set NewName=!NewName:_= !
    Set NewName=!NewName:.= !
 
    if "!NewName!" NEQ "%%a" REN "%BaseFolder%\%%a" "!NewName!"
)

Open in new window

0
 
madlanAuthor Commented:
Hi, I get an expected identifier error (line 3 char 5)
(I have enclosed the basefolder in "")

Thanks.
0
 
käµfm³d 👽Commented:
The script is not creating a new directory, it is merely appending a numeral to the name it if the name is already found in the parent folder. The script was trying to set the name of the current directory to the same name (basically no change); that's why the file exists error came up. These modifications should correct that (actually tested :) ).
dim folders
dim fso
dim temp
dim base
dim i
 
set fso = CreateObject("Scripting.FileSystemObject")
set folders = fso.GetFolder("C:\Users\Kenneth\Desktop\NewFolder")
 
for each folder in folders.subFolders
  base = replace(replace(folder.name, "_", " "), ".", " ")
  
  if base <> folder.name then
  	if not fso.FolderExists(folder.ParentFolder.Path & "\" & base) then
  		folder.name = base
  	else
  		temp = base
  		i = 1
  		
  		while fso.FolderExists(folder.ParentFolder.Path & "\" & temp)
			temp = base & cstr(i)
			i = i + 1
	  	wend
	  	
	  	folder.name = temp
  	end if
  end if
next

Open in new window

0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now