@echo off
setlocal
echo 批处理说明如下:
echo --------------------------------------------
echo 添加的字符串是对所有行而言
echo 但可以通过在行首加";"来防止添加字符串(将忽略该行)
::eol=#,;,$,^,@,!,*,/,\,%,=......
echo 没有忽略行首空白
echo 忽略了空行
echo 修改后的文件名为:modified_filename
echo --------------------------------------------
echo.
echo 当前路径为:
echo %cd%
set /p filename= 请输入文件名(可包含路径)
for /f "tokens=* delims=\" %%a in ("%filename%") do @set filename=%%~nxa
echo 文件名为:%filename%
set /p string= 请输入要添加的字符串( 不能含空格和符号 "&" ) :
echo 刚输入字符串为:%string%
echo 按任意键开始添加 ...
pause>nul
if exist modified_%filename% (
del "modified_%filename%"
)
:: 注意:命令行中与批处理文件中的不同,命令行下使用%a而批处理中使用%%a
:: On the console,you can use these commands:
:: for /f "tokens=* delims="%a in ( filename ) do @echo %string%%a >> modified_filename
if EXIST "%filename%" (
for /f "eol=; usebackq tokens=* delims=" %%a in ("%filename%") do @if "%%a"=="" (
echo %string%>>"modified_%filename%"
) else (
echo %string%%%a>>"modified_%filename%"
)
echo SUCCESS!!!
pause>nul
) else (
echo 找不到文件 %filename% !
pause
)
endlocal
无法保留原来文本中的空行(无任何内容的行);
2014年8月2日发现无法处理有路径的文件,于是修改了一下可以了。