1

enter image description here

As can be seen in the image I have folders with "." in them I would like to replace these with a "_" using CMD is there a method to do this.

Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
bdawes
  • 11
  • 1
  • Possible duplicate of [Replace or delete certain characters from filenames of all files in a folder](http://stackoverflow.com/questions/16636996/replace-or-delete-certain-characters-from-filenames-of-all-files-in-a-folder) – Mathias R. Jessen Feb 10 '16 at 00:20

2 Answers2

1

cmd.exe shell scripting is the worst approach for anything more than @echo off :-)

But ok.

You can use the enhanced shell command set to replace characters in a variable:

set DUH=FBB
echo %DUH:B=O%  -> FOO

So, for your problem, you need to read all folders and get them in a variable, so you can replace .=_ and then rename.

First batch: rena.cmd iterates over your folders

@echo off
for /D %%i in ( *.* ) do call rena2.cmd %%i

Second batch: rena2.cmd handles the rename

@echo off
setlocal enableextensions
setlocal enabledelayedexpansion

set TONAME=%~1

move %1 "%TONAME:.=_%"

exit /B

This can be done in one script, feel free to fiddle it together, I won't :-)

thst
  • 4,592
  • 1
  • 26
  • 40
1
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=U:\sourcedir\t w o"
FOR /f "delims=" %%a IN (
  'dir /b /ad "%sourcedir%\*.*" '
 ) DO (
 SET "dirname=%%a"
 SET "dirname=!dirname:.=_!"
 IF "!dirname!" neq "%%a" ECHO(REN "%sourcedir%\%%a" "!dirname!"
)

GOTO :EOF

You would need to change the setting of sourcedir to suit your circumstances.

The required REN commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(REN to REN to actually rename the files.

Dimply perform a directory-list, change the . to _ and if a change was made, perform the rename.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • I normally used a CALL and label in the FOR body to keep the code clean, but as long as the env variables hold the correct value, then you are good. – CoveGeek Feb 10 '16 at 06:24