Doc Symbiosis Posted October 6, 2005 Share Posted October 6, 2005 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. Link to comment Share on other sites More sharing options...
IcemanND Posted October 6, 2005 Share Posted October 6, 2005 is it known number of _ in each name or does it vary with every file? Link to comment Share on other sites More sharing options...
gunsmokingman Posted October 6, 2005 Share Posted October 6, 2005 Here is a VBS script that replace the "-" with this "."Dim TestReplace,NewReplaceTestReplace = "SOME-TEXT-TO-REPLACE"NewReplace = Replace(TestReplace,"-",".")msgbox TestReplace & vbCrLf & NewReplace, 0 +32,"Test" Link to comment Share on other sites More sharing options...
Yzöwl Posted October 6, 2005 Share Posted October 6, 2005 (edited) This isn't the sort of mass rename to be taken lightly, are you happy for_my_file_.extto be named.my.file..extIf you are then you could try@echo off&setlocal enableextensions enabledelayedexpansionpushd E:\My Projects\MyFolderfor /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 :eofJust change the location after pushd Edited October 6, 2005 by Yzöwl Link to comment Share on other sites More sharing options...
gunsmokingman Posted October 7, 2005 Share Posted October 7, 2005 (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 thefolder. 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 ScriptThe start folder replace with yoursWhere you find Ts.writeline replace with Fso.moveFolder or Fso.MoveFile to change the namesof the folder or file.Save this on your desktop As ListFolder_File.vbsDim 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 FOLDERSet 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 NextEnd SubTs.Close Edited December 29, 2005 by gunsmokingman Link to comment Share on other sites More sharing options...
Doc Symbiosis Posted October 9, 2005 Author Share Posted October 9, 2005 Thanks to you all for the great help. Link to comment Share on other sites More sharing options...
Doc Symbiosis Posted October 15, 2005 Author Share Posted October 15, 2005 (edited) This isn't the sort of mass rename to be taken lightly, are you happy for_my_file_.extto be named.my.file..extIf you are then you could try@echo off&setlocal enableextensions enabledelayedexpansionpushd E:\My Projects\MyFolderfor /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 :eofJust 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 twolinesset "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 October 15, 2005 by Doc Symbiosis Link to comment Share on other sites More sharing options...
Doc Symbiosis Posted October 15, 2005 Author Share Posted October 15, 2005 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 enabledelayedexpansionpushd E:\testfor /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 Link to comment Share on other sites More sharing options...
Yzöwl Posted October 15, 2005 Share Posted October 15, 2005 (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 linesset "old=%%?"&set "new=!old:_=.!" if not exist "!new!" (Sorry my mistake, what is happening at the moment is that the ren command is sayingren "E:\My Projects\MyFolder\A_File.txt" "E:\My Projects\MyFolder\A.File.txt"when what it should be saying isren "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 enabledelayedexpansionpushd E:\My Projects\MyFolderfor /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 :eofHope 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 October 15, 2005 by Yzöwl Link to comment Share on other sites More sharing options...
Doc Symbiosis Posted October 15, 2005 Author Share Posted October 15, 2005 Thank you very, very much Yzöwl. Now it works. Just one thing: in the else expression, there should be an echo command:echo %%? already exists! Link to comment Share on other sites More sharing options...
Yzöwl Posted October 15, 2005 Share Posted October 15, 2005 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. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now