cle*_*ent 2 windows batch-file command-line-interface
我正在开发一个应用程序。在整个项目中必须更改一些路径。路径是固定的,可以编辑文件(位于“ .cshtml”中)。
因此,我想可以使用一个批处理文件将所有的“ http://localhost.com ” 更改为“ http://domain.com ”(我知道相对路径和绝对路径,但是在这里我必须这样做: -))
因此,如果您具有可以在文件中进行更改的代码,那就太妙了!
为了完成我的问题,这是文件和目录的路径
MyApp
MyApp/Views
MyApp/Views/Index/page1.cshtml
MyApp/Views/Index/page2.cshtml
MyApp/Views/Another/page7.cshtml
...
Run Code Online (Sandbox Code Playgroud)
谢谢帮助我:-)
这样的事情也可能会起作用:
#!/bin/bash
s=http://localhost.com
r=http://example.com
cd /path/to/MyApp
grep -rl "$s" * | while read f; do
sed -i "s|$s|$r|g" "$f"
done
Run Code Online (Sandbox Code Playgroud)
编辑:否,因为您只是从bash切换到batch-file。批处理解决方案可能如下所示:
@echo off
setlocal EnableDelayedExpansion
for /r "C:\path\to\MyApp" %%f in (*.chtml) do (
(for /f "tokens=*" %%l in (%%f) do (
set "line=%%l"
echo !line:
)) >"%%~ff.new"
del /q "%%~ff"
ren "%%~ff.new" "%%~nxf"
)
Run Code Online (Sandbox Code Playgroud)
批量执行此操作确实非常丑陋(也容易出错),并且最好sed用于Windows或(更好)在PowerShell中执行:
$s = "http://localhost.com"
$r = "http://example.com"
Get-ChildItem "C:\path\to\MyApp" -Recurse -Filter *.chtml | ForEach-Object {
(Get-Content $_.FullName) |
ForEach-Object { $_ -replace [regex]::Escape($s), $r } |
Set-Content $_.FullName
}
Run Code Online (Sandbox Code Playgroud)
请注意,-Filter仅在PowerShell v3中有效。对于早期版本,您必须这样做:
Get-ChildItem "C:\path\to\MyApp" -Recurse | Where-Object {
-not $_.PSIsContainer -and $_.Extension -eq ".chtml"
} | ForEach-Object {
(Get-Content $_.FullName) |
ForEach-Object { $_ -replace [regex]::Escape($s), $r } |
Set-Content $_.FullName
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3679 次 |
| 最近记录: |