Replacing character in mutiple files with batch
#1
Posted 06 October 2005 - 07:17 AM
Thanks in advance for any help.
#2
Posted 06 October 2005 - 09:39 AM
#3
Posted 06 October 2005 - 10:43 AM
Quote
TestReplace = "SOME-TEXT-TO-REPLACE"
NewReplace = Replace(TestReplace,"-",".")
msgbox TestReplace & vbCrLf & NewReplace, 0 +32,"Test"
#4
Posted 06 October 2005 - 02:24 PM
- _my_file_.ext
to be named
.my.file..ext
@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
Posted 06 October 2005 - 10:26 PM
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 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
#7
Posted 15 October 2005 - 05:16 AM
Yzöwl, on Oct 6 2005, 02:24 PM, said:
- _my_file_.ext
to be named
.my.file..ext
@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
Posted 15 October 2005 - 06:18 AM
@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
Posted 15 October 2005 - 08:14 AM
Doc Symbiosis, on Oct 15 2005, 11:16 AM, said:
set "old=%%?"&set "new=!old:_=.!" if not exist "!new!" (
- ren "E:\My Projects\MyFolder\A_File.txt" "E:\My Projects\MyFolder\A.File.txt"
- ren "E:\My Projects\MyFolder\A_File.txt" "A.File.txt"
@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
Posted 15 October 2005 - 08:25 AM
echo %%? already exists!
#11
Posted 15 October 2005 - 09:10 AM
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.



Help

Back to top









