스크립트와 유틸리티를 사용하여 cmd에서“file.txt의 모든 포함”및“다른 텍스트 파일의 단어를 대체하십시오”복사


2

두 개의 파일이 있습니다.

  • file1.txt
  • file2.txt

file1.txt

wget --ftp-user=ezyro_19988709 --http-password=....... ftp://ftp.com/ADMIN.txt

file2.txt

password

이제 암호를 file2에서 file1로 넣고 점선 영역을 바꾸고 싶습니다. 이제 두 파일 출력을 병합 한 후 다음과 같아야합니다.

wget --ftp-user=ezyro_19988709 --http-password=password ftp://ftp.com/ADMIN.txt" 

나는 이것에 대해 어떻게 갈 것인가?


PowerShell을 Get-Content, Out-Filereplace.
세스

답변:


1

이것을 쏴야합니다.

.bat확장명을 가진 파일을 만든 다음 bat 파일에 다음 코드를 넣고 파일 (명령 줄이 포함 된 file1.txt 및 암호가 포함 된 file2.txt)을 작성하고이 두 파일을 동일하게 넣으십시오. .bat 파일로 디렉토리.

그런 다음 파일을 실행하십시오. 작동하면 오류가 있는지 알려주십시오.

set replacestr=""
for /F "tokens=*" %%a in (file2.txt) do call :Foo %%a
goto End

:Foo
set replacestr=%1

call :innerloop  
:innerloop 

echo %replacestr%
call :FindReplace "......." %replacestr% file1.txt

exit /b 

:FindReplace <findstr> <replstr> <file>
set tmp="%temp%\tmp.txt"
If not exist %temp%\_.vbs call :MakeReplace
for /f "tokens=*" %%a in ('dir "%3" /s /b /a-d /on') do (
  for /f "usebackq" %%b in (`Findstr /mic:"%~1" "%%a"`) do (
    echo(&Echo Replacing "%~1" with "%~2" in file %%~nxa
    <%%a cscript //nologo %temp%\_.vbs "%~1" "%~2">%tmp%
    if exist %tmp% move /Y %tmp% "%%~dpnxa">nul
  )
)
del %temp%\_.vbs
exit /b

:MakeReplace
>%temp%\_.vbs echo with Wscript
>>%temp%\_.vbs echo set args=.arguments
>>%temp%\_.vbs echo .StdOut.Write _
>>%temp%\_.vbs echo Replace(.StdIn.ReadAll,args(0),args(1),1,-1,1)
>>%temp%\_.vbs echo end with

1
당신은 단지 좋은 형제입니다! 고마워 효과가있다!!!
user3275899

1
you you happy;)
Yacine

안녕하세요,이 코드에 문제가 있습니다. 코드를 code.bat에 넣은 다음 % temp % 폴더의 vbs 스크립트를 사용하여 code.bat를 시작하려고합니다. 파일 start.vbs에 포함 : Set oShell = CreateObject ( "Wscript.Shell") Dim strArgs strArgs = "cmd / c code.bat "oShell.Run strArgs, 0, false 정상적으로 실행되지만 file1.txt에서"....... "를 대체 할 수 없습니다.
user3275899

% temp % 폴더에 파일 이름 "_.vbs"에 코드가 포함되어 있습니다. Wscript set args = .arguments .StdOut.Write _ Replace (.StdIn.ReadAll, args (0), args (1), 1, -1,1)로 끝나는
user3275899

1

Windows에 Powershell이 설치되어 있으면 Powershell 을 열고 다음 명령을 실행할 수 있습니다.

(Get-Content fullPathToFile2.txt).replace('string', (Get-Content fullPathToFile1.txt) ) | Set-Content fullPathToFile2.txt

예:

(Get-Content I:\b.txt).replace('5', (Get-Content I:\a.txt) ) | Set-Content I:\b.txt

귀하의 답변에 감사하지만 선생님, 나는 모든 친구를위한 보편적 인 솔루션이 필요하므로 Powershell을 사용하고 싶지 않습니다 .fart.exe와 같은 명령 줄에 vbscript 또는 char_string 교체 유틸리티를 사용하고 싶지 않습니다. fart.exe는 이러한 종류의 작업이 부족하여 나를 도울 수 없습니다.
user3275899

1
필자는 "[...] 스크립트와 유틸리티를 사용하여"게시물 제목을 읽었으므로 Powershell에 문제가 없을 것이라고 생각했습니다. 그러나 나는 당신이 어쨌든 문제를 해결하는 것을 보았습니다
m2cit
당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.