Replace 使用批处理替换文件中的部分文本

Replace 使用批处理替换文件中的部分文本,replace,batch-file,Replace,Batch File,“MyFile.txt”文件中有一行,我需要替换该行的一部分 例如: 文件中的行是这样的 53544THOIN91111160000000 我想替换“MyFile.txt”中现有行中的“11111 6”,这里的内容是“11111 6”是一个变量,并且会不断更改。它基本上是一个格式为YYMMDD的日期,我想从另一个文件中读取修改后的日期,并将这些数字替换为“MyFile.txt” 这是我试过的代码 set b=MyFile.txt for /f "tokens= 1" %%c in (%b%)

“MyFile.txt”文件中有一行,我需要替换该行的一部分

例如:

文件中的行是这样的

53544THOIN91111160000000
我想替换“MyFile.txt”中现有行中的“11111 6”,这里的内容是“11111 6”是一个变量,并且会不断更改。它基本上是一个格式为YYMMDD的日期,我想从另一个文件中读取修改后的日期,并将这些数字替换为“MyFile.txt”

这是我试过的代码

set b=MyFile.txt

for /f "tokens= 1" %%c in (%b%) do (set line=%%c)

Set OLDDate=%line:~11,6%

SET filename="AnotherFile.txt"
FOR %%f IN (%filename%) DO SET filedatetime=%%~tf

SET Month=%filedatetime:~0,2%
SET Date=%filedatetime:~3,2%
SET Year=%filedatetime:~8,2%

SET NEWDate=%Year%%Month%%date%

ECHO OLD DATE = %OLDDate%
ECHO NEW DATE = %NEWDate%

我需要将'MyFile.txt'中~11,6

位置的
%OLDDate%
替换为
%NEWDate%
,powershell为什么不能这样做

    # Example of PowerShell -replace parameter
    clear-Host
    $file = Get-ChildItem "D:\powershell\snippets\g*.txt"
    foreach ($str in $file) 
    {
    $content = Get-Content -path $str
    $content | foreach {$_ -replace "the the", "the"} | Set-Content $str
    }
    write-Host "After replace `n"
    $file
在foreach循环中,可以用另一个字符串替换任何字符串

日期逻辑可按如下方式使用:

$anotherfile = gi anotherfile.txt
检索日期信息

$year = $anotherfile.LastWriteTime.Year

据说它是一个变量,我能用另一个变量替换一个变量吗?还没有尝试过powershell,这将在用户的系统上运行,我不希望有任何依赖项…Windows 7本机安装了powershell。Windows XP需要管理安装更新才能使用Powershell。它就像一个高级命令行。这个工具将运行在Windows 2000 Server、Windows 2003 Server、XP、Vista和7。。。