for循环在Windows .bat文件中不起作用 [英] for loop not working in windows .bat file
本文介绍了for循环在Windows .bat文件中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要一个 windows
I need a windows batch-file which will be placed in the server path. My data files are placed in another path.
- 运行bat文件后,它应该在给定路径中搜索文件列表(可以在bat文件放置的路径中).
- 它应搜索正斜杠
/
,反斜杠\
,特殊字符并替换为空格
- Once I run the bat file, it should search the list of files in the given path, (could be in same path where bat file placed).
- It should search for forward slash
/
, back slash\
, special characters and replace with the SPACE
我使用下面的代码:-它适用于单个文件.当我开环时,它不起作用.请帮忙.
I used below code:- it work for single file. When I open loop it is not working. Please help.
以及以* .err结尾的文件都不应从此循环中选取.
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
::----------------------------------------------------------------------------------
)
推荐答案
尝试一下,告诉我这项工作是否可行:
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
::----------------------------------------------------------------------------
这篇关于for循环在Windows .bat文件中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文