Jump to content

Recommended Posts

Posted

I have a folder with many subfolders and a lot of files in it. Many files and folders contain a "_" in their name. Now I want to replace this "_" with ".". Is it possible with a batchfile to solve this problem?

Thanks in advance for any help.


Posted

Here is a VBS script that replace the "-" with this "."

Dim TestReplace,NewReplace

TestReplace = "SOME-TEXT-TO-REPLACE"

NewReplace = Replace(TestReplace,"-",".")

msgbox TestReplace & vbCrLf & NewReplace, 0 +32,"Test"

Posted (edited)

This isn't the sort of mass rename to be taken lightly, are you happy for

  • _my_file_.ext
    to be named
    .my.file..ext

If you are then you could try

@echo off&setlocal enableextensions enabledelayedexpansion
pushd E:\My Projects\MyFolder
for /f "delims=" %%? in ('dir/b/s/a *_*') do (
set "old=%%?"&set "new=!old:_=.!"
if not exist "!new!" (
ren "%%?" "!new!"
) else (
echo/ %%? already exists!
pause
)
)
popd&endlocal&goto :eof

Just change the location after pushd

Edited by Yzöwl
Posted (edited)

Here is a VBS script that goes threw Folders And Sub Folders List files and uses Replace method.

I have it set so it produces a txt file that shows the changes. Without making any changes in the

folder. I Have included the test folder and a couple of blank text files in the rar file.

Are Comment Out So Can Be Removed From Script

The start folder replace with yours

Where you find Ts.writeline replace with Fso.moveFolder or Fso.MoveFile to change the names

of the folder or file.

Save this on your desktop As ListFolder_File.vbs

Dim Ts, Inta : Inta = 0

Dim Fso : Set Fso = CreateObject("Scripting.FileSystemObject")

Set Ts = Fso.CreateTextFile("ListChanges.txt")

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

strFolderName = "c:\TestSpace" '''' THIS IS THE START FOLDER

Set colSubfolders = objWMIService.ExecQuery("Associators of {Win32_Directory.Name='" & strFolderName & "'} " _

& "Where AssocClass = Win32_Subdirectory " _

& "ResultRole = PartComponent")

'Wscript.Echo strFolderName & vbCrLf & "Start Folder For Search"

Ts.WriteLine strFolderName & vbCrLf & "Start Folder For Search"

arrFolderPath = Split(strFolderName, "\")

strNewPath = ""

For i = 1 to Ubound(arrFolderPath)

strNewPath = strNewPath & "\\" & arrFolderPath(i)

Next

strPath = strNewPath & "\\"

Set colFiles = objWMIService.ExecQuery("Select * from CIM_DataFile where Path = '" & strPath & "'")

For Each objFile in colFiles

Wscript.Echo objFile.Name

Next

For Each objFolder in colSubfolders

GetSubFolders strFolderName

Next

Sub GetSubFolders(strFolderName)

Set colSubfolders2 = objWMIService.ExecQuery _

("Associators of {Win32_Directory.Name='" & strFolderName & "'} " _

& "Where AssocClass = Win32_Subdirectory " _

& "ResultRole = PartComponent")

'''' FOR THE SUB FOLDERS IN THE COLLECTION

For Each objFolder2 in colSubfolders2

Inta = Inta + 1

strFolderName = objFolder2.Name

' Wscript.Echo

' Wscript.Echo objFolder2.Name & Space(3) & " --> " & Inta & Space(3) & "OLD"

FolderR = Replace(objFolder2.Name,"_",".")

' WScript.Echo FolderR & Space(3) & " --> " & Inta & Space(3) & "NEW"

Ts.WriteLine objFolder2.Name & Space(3) & " --> " & Inta & Space(3) & "OLD" &_

vbCrLf & FolderR & Space(3) & " --> " & Inta & Space(3) & "NEW" & vbCrLf

arrFolderPath = Split(strFolderName, "\")

strNewPath = ""

For i = 1 to Ubound(arrFolderPath)

strNewPath = strNewPath & "\\" & arrFolderPath(i)

Next

strPath = strNewPath & "\\"

'''' FOR ALL FILES IN THE FOLDERS

Set colFiles = objWMIService.ExecQuery("Select * from CIM_DataFile where Path = '" & strPath & "'")

For Each objFile in colFiles

FileR = Replace(objFile.Name,"_",".")

FileR = Replace(FileR,"..",".")

' Wscript.Echo objFile.Name & Space(3) & " --> " & Inta & Space(3) & "OLD"

' Wscript.Echo FileR & Space(3) & " --> " & Inta & Space(3) & "NEW"

Ts.WriteLine objFile.Name & Space(3) & " --> " & Inta & Space(3) & "OLD" &_

vbCrLf & FileR & Space(3) & " --> " & Inta & Space(3) & "NEW" & vbCrLf

Next

GetSubFolders strFolderName

Next

End Sub

Ts.Close

Edited by gunsmokingman
Posted (edited)
This isn't the sort of mass rename to be taken lightly, are you happy for
  • _my_file_.ext
    to be named
    .my.file..ext

If you are then you could try

@echo off&setlocal enableextensions enabledelayedexpansion
pushd E:\My Projects\MyFolder
for /f "delims=" %%? in ('dir/b/s/a *_*') do (
set "old=%%?"&set "new=!old:_=.!"
if not exist "!new!" (
ren "%%?" "!new!"
) else (
echo/ %%? already exists!
pause
)
)
popd&endlocal&goto :eof

Just change the location after pushd

@Yzöwl: Hi there, I used the vbs to rename the files and solved my problem, but now I'm trying with your batchfile and get an syntax error for the rename command. I think there something wrong within these two

lines

set "old=%%?"&set "new=!old:_=.!"
if not exist "!new!" (

Don't I have to put % around the new, cause it's a variable? I tried some different things, but I definitely can't figure it out.

Edited by Doc Symbiosis
Posted

While playing a litlle with the script in came to the following question: why does in the following example the variable new only returns a value, when I use it outside of the for loop?

@echo off&setlocal enableextensions enabledelayedexpansion
pushd E:\test
for /f "delims=" %%i in ('dir/b/s/a *_*') do (
echo %%i
set new=%%i
echo new_inside_for: %new:_=.%
)
echo new_outside_for: %new:_=.%
popd&endlocal&goto :eof

Posted (edited)
@Yzöwl: Hi there, I used the vbs to rename the files and solved my problem, but now I'm trying with your batchfile and get an syntax error for the rename command. I think there something wrong within these two lines
set "old=%%?"&set "new=!old:_=.!"
if not exist "!new!" (

Sorry my mistake, what is happening at the moment is that the ren command is saying
  • ren "E:\My Projects\MyFolder\A_File.txt" "E:\My Projects\MyFolder\A.File.txt"

when what it should be saying is

  • ren "E:\My Projects\MyFolder\A_File.txt" "A.File.txt"

So the fix is with the first of the two lines you quoted

@echo off&setlocal enableextensions enabledelayedexpansion
pushd E:\My Projects\MyFolder
for /f "delims=" %%? in ('dir/b/s/a *_*') do (
set "old=%%~nx?"&set "new=!old:_=.!"
if not exist "!new!" (
echo/ren "%%?" "!new!"
)
else (
echo/ %%? already exists!
pause
)
)
popd&endlocal&goto :eof

Hope this helps!

PS As for your following question, it is due to the limitation of variable substitution within a for loop. Hence the reason I used delayed expansion.

Edited by Yzöwl
Posted

Yes I'll try to fix it in my post, it must have disappeared when farting around trying to get the formatting to work.

For some reason all the formatting of spaces, tabs etc. , in the last day or so, has suddenly gone funny in PMs and Forum Posts.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...