我正在尝试使用命令回显批处理中的文本echo text here>file.txt
,但是否可以将文本回显为 Ln20 Col30 之类的内容
我尝试过这些但没有任何好结果:
set wfile=aze.txt
set wcontent=txt here
for /F "skip=20 delims=30" %%p in %wfile% do echo %wcontent%
我知道要在文件中查找文本,我们可以使用该find
命令,但我不知道该如何写
有人知道可以解决此问题的命令吗?我会很感激任何反馈
我发现我可能能够找到我想要重写的那行代码,然后重写整个代码,目前我发现了这段代码
@echo off &setlocal DisableDelayedExpansion
set "file=file.txt"
set "lastline=txt here"
for /f %%i in ('type "%file%"^|find /c /v ""') do set "last=%%i"
<"%file%" >"%file%.tmp~" (
for /f "delims=" %%i in ('type "%file%"^|findstr /n "^"') do (
set "line=%%i"
setlocal EnableDelayedExpansion
for /f "delims=:" %%j in ("!line!") do if %%j equ %
echo(!lastline!
) else (
echo(!line:*:=!
)
)
endlocal
)
)
move /y "%file%.tmp~" "%file%"
唯一的问题是这将写最后一行,我们可以选择该行吗?
嘿 T3RROR,一种可能将 dir 的输出存储到一个变量中的方法是将 dir 命令输出到一个临时文件中,然后读取该文件并将读到的内容放入一个变量中,然后删除该文件,因为我们不再需要它了
@echo off
rem here, input dir /b file.txt /s into inputfiletxtword.txt
set "txtlist=|type %temp%\inputfiletxtword.txt"
echo %txtlist%
pause
答案1
* 更新 *
您尝试设置路径的方式不起作用。
* 已更新,所有输入均设置 /P*
用于替换任何文本文件类型的目标行的程序:
@ECHO OFF
TITLE %~dp0
Setlocal EnableDelayedExpansion
REM - Allow definition of inputfile via drop and drag. Cannot handle Spaces.
::: Set /p "inputfile=Drag and Drop your File ]>"
:select
cls
Set /p inputfile=[Type or paste full filepath, or drop file If no Spaces:]>
IF not defined inputfile goto select
IF NOT exist "%inputfile%" GOTO select
REM - Capture info to rename temp file with later
for %%a in ("%inputfile%") do (
Set name=%%~na
Set Location=%%~dpa
Set EXT=%%~xa
)
Set "TempReplaces=!Location!!name!!EXT!"
REM - Count number of lines in file
Set avail=0
REM - Check available lines:
for /f "tokens=* delims=" %%a in (%inputfile%) do (
set /a avail+=1
)
REM - Select Line Number
:linenumber
cls
ECHO File: %inputfile%
Set /p subline=[File Contains: %avail% Lines. Line Number to Substitute:]
REM - Ensure the linenumber is numerical
Set /a test=%subline%+1
IF %test% LSS 2 GOTO linenumber
REM - Enter Replacement String
Set /p substitution=[Enter Replacement Text:]
REM - Function counts lines then Echoes the defined text when target line is encountered.
for /f "tokens=* delims=" %%l in (%inputfile%) do (
set /a line+=1
if !line! EQU !subline! (
echo %substitution% >>%Location%TempSub%EXT%
) else (
echo %%l >>%Location%TempSub%EXT%
)
)
REM - Replace File With Substitution
DEL /Q "%TempReplaces%"
TIMEOUT 1 > nul
FOR %%n IN ("%Location%TempSub%EXT%") DO (
REN %%n "%name%!EXT!"
)
ECHO Done
TIMEOUT 2 > nul
EXIT