Reputation: 7
I need a windows batch-file which will be placed in the server path. My data files are placed in another path.
/
, back slash \
, special characters and replace with the SPACEI used below code:- it work for single file. When I open loop it is not working. Please help.
and also file which ends with *.err should NOT be picked from this loop.
@echo off
Title Replace String using Regex with vbscript
setlocal EnableDelayedExpansion
Set "MYDIR=\\server01\Import\LoadError\dump"
for /F %%x in ('dir /B/D %MYDIR%') do (
Set "InputFile=%MYDIR%\%%x"
::Set "InputFile=\\server01\Import\LoadError\dump\KEG_OAP671A4_55555.txt"
Set "TmpFile=%Tmp%\%~n0.txt"
:: To write Result in a temporary file
Call :Search_Replace "%InputFile%" "%TmpFile%"
:: Replace and move contents from the temporary file to the original
Move /Y "%TmpFile%" "%InputFile%">nul
Start "" "%InputFile%" & Exit
::-----------------------------------------------------------------------------------
:Search_Replace <InputFile> <TmpFile>
(
echo WScript.StdOut.WriteLine Search_Replace(Data^)
echo Function Search_Replace(Data^)
echo Dim strPattern, strReplace, strResult,oRegExp
echo Data = "%~1"
echo Data = WScript.StdIn.ReadAll
echo strPattern = "[\\\/\/]"
echo strReplace = " "
echo Set oRegExp = New RegExp
echo oRegExp.Global = True
echo oRegExp.IgnoreCase = True
echo oRegExp.Pattern = strPattern
echo strResult = oRegExp.Replace(Data,strReplace^)
echo Search_Replace = strResult
echo End Function
)>"%tmp%\%~n0.vbs"
cscript //nologo "%tmp%\%~n0.vbs" < "%~1" > "%~2"
If Exist "%tmp%\%~n0.vbs" Del "%tmp%\%~n0.vbs"
Exit /B
::----------------------------------------------------------------------------------
)
Upvotes: 1
Views: 261
Reputation: 18827
Give a try and tell me if this work or not on your side :
I added a newline to backup your dump folder and its contents if something went wrong!
@echo off
Mode 85,35 & color 0A
Title Replace Multi String using Regex with vbscript into Folder with text files
Set "Source_Folder=\\server01\Import\LoadError\dump"
Set "Backup_Folder=%userprofile%\Backup_dump\"
Rem :: Just make a backup of your folder and its contents if something went wrong!
If Not Exist "%Backup_Folder%" XCopy "%Source_Folder%" "%Backup_Folder%" /D /Y /E /F >%~dp0BackupLogFile.txt
Set "VBSFILE=%tmp%\%~n0.vbs" & Call :CreateVBS
Set "TmpFile=%Temp%\%~n0.tmp"
for /R "%Source_Folder%" %%f in (*.txt) do (
echo( ------------------------------------------
echo Replacing Contents of "%%f"
echo( ------------------------------------------
Call :Search_Replace "%%f" "%TmpFile%"
Move /Y "%TmpFile%" "%%f">nul
)
Timeout /T 2 /NoBreak>nul & Exit
::-----------------------------------------------------------------------------
:CreateVBS
(
echo WScript.StdOut.WriteLine Search_Replace(Data^)
echo Function Search_Replace(Data^)
echo Dim strPattern, strReplace, strResult,oRegExp
echo Data = "%~1"
echo Data = WScript.StdIn.ReadAll
echo strPattern = "[\\\/\/]"
echo strReplace = " "
echo Set oRegExp = New RegExp
echo oRegExp.Global = True
echo oRegExp.IgnoreCase = True
echo oRegExp.Pattern = strPattern
echo strResult = oRegExp.Replace(Data,strReplace^)
echo Search_Replace = strResult
echo End Function
)>"%VBSFILE%"
Exit /b
::----------------------------------------------------------------------------
:Search_Replace <InputFile> <OutPutFile>
Cscript //nologo "%VBSFILE%" < "%~1" > "%~2"
Exit /B
::----------------------------------------------------------------------------
Upvotes: 1
Reputation: 464
@Karun I think you better try for some bash script instead of bat.
You can use SED
Stream Editor to search and replace characters from file.
Example:
sed -i 's#\\##g' sample.txt # This will replace back slash from sample.txt
sed -i 's#/##g' sample.txt # This will replace forward slash from sample.txt
To execute for multiple files in a directory, you can use for
loop as well.
Save all your string replacement commands to a bash script like test.sh
and execute it using for
loop.
Example:
sed -i 's#\\##g' $1
sed -i 's#/##g' $1
Add above lines in test.sh and run below line of script.
for file in $(ls -l /home/abc/*.txt | awk '{print $NF}'); do bash test.sh $file; done
Upvotes: 0