for 循环在 windows .bat 文件中不起作用
for loop not working in windows .bat file
我需要一个 windows batch-file ,它将被放置在服务器路径中。我的数据文件放在另一个路径。
- 一旦我 运行 bat 文件,它应该搜索给定路径中的文件列表,(可以与放置 bat 文件的路径相同)。
- 它应该搜索正斜杠
/
、反斜杠 \
、特殊字符并替换为 SPACE
sample Original file
Corrected file
我使用了以下代码:- 它适用于单个文件。当我打开循环时它不工作。
请帮忙。
并且不应从此循环中选取以 *.err 结尾的文件。
@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
::----------------------------------------------------------------------------------
)
@Ka运行 我认为你最好尝试一些 bash 脚本而不是 bat。
您可以使用 SED
Stream Editor 来搜索和替换文件中的字符。
示例:
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
要对目录中的多个文件执行,您也可以使用for
循环。
将所有字符串替换命令保存到 bash 脚本,如 test.sh
并使用 for
循环执行它。
示例:
sed -i 's#\##g'
sed -i 's#/##g'
在脚本行下方的 test.sh 和 运行 中添加以上行。
for file in $(ls -l /home/abc/*.txt | awk '{print $NF}'); do bash test.sh $file; done
试一试,告诉我这对你是否有效:
如果出现问题,我添加了一个换行符来备份你的转储文件夹及其内容!
@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
::----------------------------------------------------------------------------
我需要一个 windows batch-file ,它将被放置在服务器路径中。我的数据文件放在另一个路径。
- 一旦我 运行 bat 文件,它应该搜索给定路径中的文件列表,(可以与放置 bat 文件的路径相同)。
- 它应该搜索正斜杠
/
、反斜杠\
、特殊字符并替换为 SPACE
sample Original file
Corrected file
我使用了以下代码:- 它适用于单个文件。当我打开循环时它不工作。 请帮忙。
并且不应从此循环中选取以 *.err 结尾的文件。
@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
::----------------------------------------------------------------------------------
)
@Ka运行 我认为你最好尝试一些 bash 脚本而不是 bat。
您可以使用 SED
Stream Editor 来搜索和替换文件中的字符。
示例:
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
要对目录中的多个文件执行,您也可以使用for
循环。
将所有字符串替换命令保存到 bash 脚本,如 test.sh
并使用 for
循环执行它。
示例:
sed -i 's#\##g'
sed -i 's#/##g'
在脚本行下方的 test.sh 和 运行 中添加以上行。
for file in $(ls -l /home/abc/*.txt | awk '{print $NF}'); do bash test.sh $file; done
试一试,告诉我这对你是否有效:
如果出现问题,我添加了一个换行符来备份你的转储文件夹及其内容!
@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
::----------------------------------------------------------------------------