MSFN Forum: Replacing character in mutiple files with batch - MSFN Forum

Jump to content



Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Replacing character in mutiple files with batch Rate Topic: -----

#1 User is offline   Doc Symbiosis 

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 333
  • Joined: 03-August 04

Posted 06 October 2005 - 07:17 AM

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.


#2 User is offline   IcemanND 

  • MSFN Junkie
  • Group: Super Moderator
  • Posts: 3,239
  • Joined: 24-September 03
  • OS:Windows 7 x64
  • Country: Country Flag

Posted 06 October 2005 - 09:39 AM

is it known number of _ in each name or does it vary with every file?

#3 User is offline   gunsmokingman 

  • MSFN Addict
  • Group: Super Moderator
  • Posts: 1,991
  • Joined: 02-August 03
  • OS:none specified
  • Country: Country Flag

Posted 06 October 2005 - 10:43 AM

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

Quote

Dim TestReplace,NewReplace
TestReplace = "SOME-TEXT-TO-REPLACE"
NewReplace = Replace(TestReplace,"-",".")
msgbox TestReplace & vbCrLf & NewReplace, 0 +32,"Test"


#4 User is offline   Yzöwl 

  • Wise Owl
  • Group: Super Moderator
  • Posts: 4,117
  • Joined: 13-October 04
  • OS:Windows 7 x64

Posted 06 October 2005 - 02:24 PM

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

This post has been edited by Yzöwl: 06 October 2005 - 02:26 PM


#5 User is offline   gunsmokingman 

  • MSFN Addict
  • Group: Super Moderator
  • Posts: 1,991
  • Joined: 02-August 03
  • OS:none specified
  • Country: Country Flag

Posted 06 October 2005 - 10:26 PM

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

Quote

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

This post has been edited by gunsmokingman: 28 December 2005 - 11:00 PM


#6 User is offline   Doc Symbiosis 

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 333
  • Joined: 03-August 04

Posted 09 October 2005 - 11:28 AM

Thanks to you all for the great help.

#7 User is offline   Doc Symbiosis 

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 333
  • Joined: 03-August 04

Posted 15 October 2005 - 05:16 AM

View PostYzöwl, on Oct 6 2005, 02:24 PM, said:

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.

This post has been edited by Doc Symbiosis: 15 October 2005 - 05:18 AM


#8 User is offline   Doc Symbiosis 

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 333
  • Joined: 03-August 04

Posted 15 October 2005 - 06:18 AM

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


#9 User is offline   Yzöwl 

  • Wise Owl
  • Group: Super Moderator
  • Posts: 4,117
  • Joined: 13-October 04
  • OS:Windows 7 x64

Posted 15 October 2005 - 08:14 AM

View PostDoc Symbiosis, on Oct 15 2005, 11:16 AM, said:

@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.

This post has been edited by Yzöwl: 15 October 2005 - 09:13 AM


#10 User is offline   Doc Symbiosis 

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 333
  • Joined: 03-August 04

Posted 15 October 2005 - 08:25 AM

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!


#11 User is offline   Yzöwl 

  • Wise Owl
  • Group: Super Moderator
  • Posts: 4,117
  • Joined: 13-October 04
  • OS:Windows 7 x64

Posted 15 October 2005 - 09:10 AM

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.

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



All trademarks mentioned on this page are the property of their respective owners
Copyright © 2001 - 2011 msfn.org
Privacy Policy