用BAT或者VBS。批量替换文件中包含F://DATE为自己想要的字符,比如C://OPEN
比如我的文件格式是CSV格式。怎么用比较好些。谢谢啦如果是一个目录下多个CSV,怎么同时替换他们呢?...
比如我的 文件格式是CSV格式。怎么用比较好些。谢谢啦
如果是一个目录下 多个CSV ,怎么同时 替换他们呢? 展开
如果是一个目录下 多个CSV ,怎么同时 替换他们呢? 展开
2个回答
展开全部
这是BAT的,试过,可行
@echo off
setlocal enabledelayedexpansion
set file=
set /p file= 请输入要操作的文件名称(包括扩展名):
set "file=%file:"=%"
for %%i in ("%file%") do set file=%%~fi
set replaced=
set /p replaced= 请输入即将被替换的内容:
set all=
set /p all= 请输入替换字符串:
for /f "delims=" %%i in ('type "%file%"') do (
set str=%%i
set "str=!str:%replaced%=%all%!"
echo !str!>>"%file%"_tmp.txt
)
copy "%file%" "%file%"_bak.txt >nul 2>nul
move "%file%"_tmp.txt "%file%"
start "" "%file%"
@echo off
setlocal enabledelayedexpansion
set file=
set /p file= 请输入要操作的文件名称(包括扩展名):
set "file=%file:"=%"
for %%i in ("%file%") do set file=%%~fi
set replaced=
set /p replaced= 请输入即将被替换的内容:
set all=
set /p all= 请输入替换字符串:
for /f "delims=" %%i in ('type "%file%"') do (
set str=%%i
set "str=!str:%replaced%=%all%!"
echo !str!>>"%file%"_tmp.txt
)
copy "%file%" "%file%"_bak.txt >nul 2>nul
move "%file%"_tmp.txt "%file%"
start "" "%file%"
展开全部
Dim objfso1,objfso2
Dim strSourcePath
Dim strDesPath
Dim strReadl
Dim strOutput
Dim streamIn,streamOp
Dim strSearchFor
Dim strReplaced
strSourcePath = "C:\Note.csv" '源文件
strDesPath = "e:\fixed.csv" '替换后的文件
strSearchFor = "f://open" '你要寻找的
strReplaced = "c://open" '你要替换的
Set objfso1 = CreateObject("scripting.FileSystemObject")
Set objfso2 = CreateObject("scripting.FileSystemObject")
'WScript.Echo strFileName
Set streamIn = objfso1.OpenTextFile(strSourcePath,1)
Set streamOp = objfso2.OpenTextFile(strDesPath,2,True)
Do until streamIn.AtEndOfStream
strReadl = streamIn.ReadLine
If InStr(strReadl,strSearchFor) = 0 Then
streamOp.WriteLine strReadl
ElseIf InStr(strReadl,strSearchFor) > 0 Then
strOutput = Replace(strReadl,strSearchFor,strReplaced)
streamOp.WriteLine strOutput
End If
Loop
streamIn.Close
streamOp.Close
Set objfso1 = Nothing
Set objfso2 = Nothing
WScript.Echo "replace successfully!"
有问题随时沟通。
Dim strSourcePath
Dim strDesPath
Dim strReadl
Dim strOutput
Dim streamIn,streamOp
Dim strSearchFor
Dim strReplaced
strSourcePath = "C:\Note.csv" '源文件
strDesPath = "e:\fixed.csv" '替换后的文件
strSearchFor = "f://open" '你要寻找的
strReplaced = "c://open" '你要替换的
Set objfso1 = CreateObject("scripting.FileSystemObject")
Set objfso2 = CreateObject("scripting.FileSystemObject")
'WScript.Echo strFileName
Set streamIn = objfso1.OpenTextFile(strSourcePath,1)
Set streamOp = objfso2.OpenTextFile(strDesPath,2,True)
Do until streamIn.AtEndOfStream
strReadl = streamIn.ReadLine
If InStr(strReadl,strSearchFor) = 0 Then
streamOp.WriteLine strReadl
ElseIf InStr(strReadl,strSearchFor) > 0 Then
strOutput = Replace(strReadl,strSearchFor,strReplaced)
streamOp.WriteLine strOutput
End If
Loop
streamIn.Close
streamOp.Close
Set objfso1 = Nothing
Set objfso2 = Nothing
WScript.Echo "replace successfully!"
有问题随时沟通。
追问
高手。好像.BAT不可以吧,带符号的?可以的,也帮我回答下吧。谢谢了
追答
高手。好像.BAT不可以吧,带符号的?可以的,也帮我回答下吧。谢谢了
-------对于你这个问题比较困惑:是用bat作同样的操作不可以,还是说用鄙人的脚本执行不了bat文件?对于执行bat文件,还没测试,或许有问题吧。您能明确下问题吗?
另外对你的问题补充,随便写了点,比较乱,凑合看吧。
Dim objfso1,objfso2
Dim strDesPath
Dim strReadl
Dim strOutput
Dim path '你要替换的文件所在的文件夹路径
Dim allFiles '定义path路径下所有的文件的全地址,包括扩展名
Dim streamIn,streamOp
Dim strSearchFor
Dim strReplaced
path = "\\Documents and Settings\\tester\\Desktop\\" '自己定义这个路径吧
allFiles = getAllFiles(path,"csv")
For Each file In allFiles
strDesPath = getSaveName(file,"1.csv") '定义保存的文件名,1.csv为区分于源文件,文件名后加1
strSearchFor = "create" '你要寻找的
strReplaced = "c://open" '你要替换的
Set objfso1 = CreateObject("scripting.FileSystemObject")
Set objfso2 = CreateObject("scripting.FileSystemObject")
'WScript.Echo strFileName
Set streamIn = objfso1.OpenTextFile(file,1)
Set streamOp = objfso2.OpenTextFile(strDesPath,2,True)
Do until streamIn.AtEndOfStream
strReadl = streamIn.ReadLine
' WScript.Echo strReadl
If InStr(strReadl,strSearchFor) = 0 Then
streamOp.WriteLine strReadl
ElseIf InStr(strReadl,strSearchFor) > 0 Then
strOutput = Replace(strReadl,strSearchFor,strReplaced)
streamOp.WriteLine strOutput
WScript.Echo strOutput
End If
Loop
streamIn.Close
streamOp.Close
Set objfso1 = Nothing
Set objfso2 = Nothing
Next
WScript.Echo "replace successfully!"
后面还有。。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询