1.电脑系统是xp,没有mklink
2.在盘,c,d,e,f//xx//都有500个cpp txt等类型文件
每个//xx//下包含有a.cpp,b.cpp两个文件
3.只要更改a.cpp,b.cpp两个文件里面内容m.txt改为n.txt,其它文件不动
4. 用link目录链接 行不通 会把整个目录复制过去 文件太多不行
5.显示更改次数
6.python会更改原文件格式 行不通
7.有没有什么好方法一次到位,不用每次手动打开4个盘 用notepad++?
8.用过bat脚本命令 但是会更改源文件格式
@echo off
set count=0
for /f "delims=" %%i in ('dir /s /b /a-d c:\*.cpp') do (
findstr /c:"m.txt" "%%i" >nul
if not errorlevel 1 (
echo Replacing "m.txt" with "n.txt" in "%%i"
set /a count+=1
type "%%i" | findstr /v "m.txt" >temp.txt
type temp.txt > "%%i"
echo n.txt>>temp.txt
type temp.txt > "%%i"
del temp.txt
)
)
echo Total Files Changed: %count%
pause
该批处理文件将在c盘的所有.cpp文件中查找“m.txt”字符串,并将其替换为“n.txt”。并且它会显示总共更改了多少个文件。
1. import os
2. root_path = 'C:/'
3. target_files = ['a.cpp', 'b.cpp']
4.
old_string = 'm.txt'
new_string = 'n.txt'
5.
replacement_count = 0
6.
for subdir, dirs, files in os.walk(root_path):
for file in files:
if file in target_files:
file_path = os.path.join(subdir, file)
with open(file_path, 'r') as f:
file_contents = f.read()
new_contents = file_contents.replace(old_string, new_string)
with open(file_path, 'w') as f:
f.write(new_contents)
replacement_count += 1
7.
print(f'Number of replacements made: {replacement_count}')
可以使用批处理脚本实现该任务。下面是一个示例:
@echo off
set count=0
for /f "delims=" %%a in ('dir /b /s /a-d *m.txt') do (
set /a count+=1
call :sub "%%a"
)
echo %count% files changed
goto :eof
:sub
set file=%1
set file=%file:m.txt=n.txt%
if exist "%file%" goto :eof
copy "%1" "%file%"
保存以上代码为 .bat 文件,双击运行即可。该脚本将在盘 C、D、E、F 中的所有子目录下寻找名为 m.txt 的文件,并将其复制到对应的 n.txt 文件中。最后,该脚本将显示更改的文件数量。
请注意,使用此脚本可能会导致原始文件丢失,因此请确保在使用前进行备份。
如对您有帮助,望采纳,嘿嘿
首先,让我告诉您,在 Windows XP 系统中并不支持 mklink 命令。因此,您需要使用其他方法来解决您的问题。
在此,我建议您使用 C 语言编写一个程序来实现您的要求。您可以使用 C 语言的文件操作函数,如 fopen,fread 和 fwrite,打开,读取和更改文件的内容。
以下是一个示例代码,该代码在文件夹中的所有 *.cpp 文件中替换所有出现的 "m.txt" 串为 "n.txt":
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LENGTH 1000 // 定义文件中最大字符数
int main()
{
char file_name[100]; // 定义文件名字符数组
char search_string[] = "m.txt"; // 定义需要搜索的字符串
char replace_string[] = "n.txt"; // 定义需要替换的字符串
char buffer[MAX_LENGTH]; // 定义缓冲区
int change_count = 0; // 定义更改次数
// 枚举所有 cpp 文件
for (int i = 1; i <= 500; i++)
{
sprintf(file_name, "//xx//%d.cpp", i); // 生成文件名
// 打开文件
FILE *fp = fopen(file_name, "r");
if (fp == NULL)
{
printf("Unable to open file %s\n", file_name);
continue;
}
// 读取文件内容
fread(buffer, MAX_LENGTH, 1, fp);
// 替换字符串
char *pos = strstr(buffer, search_string);
while (pos != NULL)
{
strncpy(pos,
可以使用批处理来实现,在每个盘的根目录下创建一个.bat文件,内容如下:
@echo off
for /r %%f in (*.cpp) do (
type "%%f" | findstr /C:"m.txt" > nul
if not errorlevel 1 (
type "%%f" | findstr /V /C:"m.txt" > temp.txt
type temp.txt | findstr /V /C:"n.txt" > temp2.txt
type temp2.txt | findstr /V /C:"m.txt" > "%%f"
del temp.txt
del temp2.txt
)
)
echo 共更改了 %errorlevel% 次
pause
打开每个盘的根目录,双击运行该.bat文件,即可完成替换,并显示更改次数。
XP系统并不支持mklink命令,可以尝试使用其他系统或软件。
可以使用批处理脚本更改文件的内容。
可以使用批处理脚本读取每个文件的内容,如果包含“m.txt”则替换为“n.txt”,并保存文件。
可以使用批处理脚本搜索所有包含“m.txt”的文件,并替换为“n.txt”。
可以使用计数器统计替换的次数。
可以使用批处理脚本读取文件内容,并使用字符串替换函数替换内容。
使用批处理脚本是一个不错的解决方案,可以在命令行中执行批处理脚本实现一次性替换。
可以使用批处理脚本读取文件内容,并使用字符串替换函数替换内容,然后再保存文件。
以下是一个批处理脚本的示例代码:
@echo off
setlocal enabledelayedexpansion
set count=0
for /r %%a in (*.cpp *.txt) do (
set file=%%a
set content=
for /f "delims=" %%b in (%%a) do (
set content=!content! %%b
)
if "!content!" == "" (
echo Warning: empty file "%%a"
continue
)
set content=!content:m.txt=n.txt!
(
echo !content!
) > "%%a"
set /a count+=1
)
echo Replaced %count% times.
pause
可以使用脚本工具来实现,比如使用powershell或者是perl等等,可以实现一次到位的更改文件内容。还可以在powershell中用Find-String来查找是否含有”m.txt”字符,如果有则替换为”n.txt”字符。同时,可以使用powershell来计算更改次数,使用”Measure-Object”来计算行数变化,来显示更改次数。
尝试使用一个类似于如下代码的 Windows 命令批处理脚本:
@echo off
setlocal enabledelayedexpansion
set src_file=m.txt
set dst_file=n.txt
set changed_files=0
for /r c:\ %%f in (a.cpp b.cpp) do (
set file_changed=0
set file_content=
for /f "tokens=* usebackq" %%l in ("%%f") do (
set line=%%l
set line=!line:%src_file%=%dst_file%!
set file_content=!file_content!%%l
if not "%%l" == "!line!" set file_changed=1
)
if !file_changed! == 1 (
set /a changed_files+=1
echo !file_content! > "%%f"
)
)
echo %changed_files% files changed.
这个脚本会遍历 c:\ 目录及其子目录中的所有 a.cpp 和 b.cpp 文件,将其中所有的 "m.txt" 替换为 "n.txt"。如果有任何一个文件内容被更改了,则将新的内容写回到原文件,并计数更改了多少个文件。
注意,在使用这个脚本之前,请将 src_file 和 dst_file 变量设置为你需要更改的源文件名和目标文件名。
此脚本可以直接在 Windows 系统的命令提示符中运行。如果需要在 Notepad++ 中调用此脚本,你可以将此脚本保存为 change.bat 文件,然后在 Notepad++ 的运行菜单中选择 "Run...",输入 cmd /c "path\to\change.bat"(其中 path\to 是你保存脚本的路径)并按下回车键即可运行。
如果您想要批量修改多个文件中的相同内容,但不想手动逐个打开和修改每个文件,可以考虑使用Python编写脚本进行批量处理。
以下是一个简单的Python脚本示例,可以在Windows上使用:
import os
# 需要更改的目标文件夹
target_folder = r"C:\xx"
# 需要替换的内容
old_str = "m.txt"
new_str = "n.txt"
# 统计更改次数的变量
count = 0
# 遍历目标文件夹及子文件夹
for root, dirs, files in os.walk(target_folder):
for file in files:
# 只处理.cpp和.txt文件
if file.endswith(".cpp") or file.endswith(".txt"):
# 获取文件路径
file_path = os.path.join(root, file)
# 打开文件并读取内容
with open(file_path, "r") as f:
content = f.read()
# 如果需要更改,替换内容并保存文件
if old_str in content:
content = content.replace(old_str, new_str)
with open(file_path, "w") as f:
f.write(content)
# 每次替换成功,将计数器加1
count += 1
print(f"共进行了 {count} 次替换。")
将上述代码保存为.py文件,将target_folder变量修改为你的目标文件夹,old_str和new_str变量修改为你要替换的内容,然后运行该脚本即可。
此脚本将遍历目标文件夹及其子文件夹,只处理.cpp和.txt文件,并在其中查找要替换的内容,如果找到了就替换并保存文件。最后会输出替换次数的统计结果。
需要注意的是,此脚本将直接修改原始文件,如需备份文件或避免意外修改,请先备份目标文件夹。