2013-06-28 2 views
1

우선 제가 스크립팅을 처음 접한다고 가정 해 보겠습니다.windows bat 스크립트 찾기 폴더와 하위 폴더의 모든 파일을 바꿉니다

주어진 문자열을 찾아 다른 주어진 문자열로 바꾸는 스크립트를 개발하려고합니다. 폴더/하위 폴더의 모든 파일에 대해이 작업을 수행해야합니다.

이것은 박쥐 파일이어야합니다. 여기에 내가 지금까지 가지고있는 것입니다 :

@echo off 
set RESULT_FILE="result.txt" 
set /p "var1=Enter the String to Find: " 
set /p "var2=Enter the String to Replace with: " 

pushd %~p0 
for /f "delims=" %%a in ('dir /B /S *.js') do (
    for /f "tokens=3 delims=:" %%c in ('find /i /c "%var1%" "%%a"') do (
    if %%c neq 0 (
     echo %var1% 
    ) 
) 
) 

popd 

이것은 발견 된 변수를 반환합니다.

미리 도움을 청하십시오.

+0

어디에서 찾았습니까? – devnull

+0

파일의 이름을 바꾸시겠습니까? 아니면 파일 내의 문자열을 바꾸시겠습니까? – cwin

+0

네이티브 스크립팅 (PowerShell, VBScript, JScript) 일 수 있습니까? 아니면 독점적으로 cmd.exe 여야합니까? –

답변

3

Windows 용 sed을 사용할 수 있습니까?

for /f "delims=" %%a in ('dir /B /S /A-D *.js') do sed -ibak "s/%var1%/%var2%/g" "%%~fa" 
 
Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]... 

    -n, --quiet, --silent 
       suppress automatic printing of pattern space 
    -e script, --expression=script 
       add the script to the commands to be executed 
    -f script-file, --file=script-file 
       add the contents of script-file to the commands to be executed 
    -i[suffix], --in-place[=suffix] 
       edit files in place (makes backup if extension supplied) 
    -l N, --line-length=N 
       specify the desired line-wrap length for the `l' command 
    -r, --regexp-extended 
       use extended regular expressions in the script. 
    -s, --separate 
       consider files as separate rather than as a single continuous 
       long stream. 
    --text  switch to text mode 
    -u, --unbuffered 
       load minimal amounts of data from the input files and flush 
       the output buffers more often 
     --help  display this help and exit 
    -V, --version output version information and exit 

If no -e, --expression, -f, or --file option is given, then the first 
non-option argument is taken as the sed script to interpret. All 
remaining arguments are names of input files; if no input files are 
specified, then the standard input is read. 
0

, 당신은 당신이 창 박쥐 스크립트의 한 줄에 원하는 것을 할 수 있습니다. -rProcess sub-folders recursively을 의미

fart.exe -r "C:\path\to\folder\*.*" string_to_be_replaced replace_string 

.

관련 문제