Announcement

Collapse
No announcement yet.

Batch file question

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Batch file question

    I am writing a batch file and I want it to find and replace a particular file.
    I don't nessarily know where the file is.

    So what I want to do is take the output from:
    dir /s /b myfile.dat

    and use it in:
    for %%i in (theoutput) do copy mynewfile.dat %%i

    But I cant figure out how to get the list into the for loop.
    Ive tried
    for %%i in (dir /s /b myfile.dat) do copy mynewfile.dat %%i
    dir /s /b myfile.dat || for %%i in (%%1) do copy mynewfile.dat %%i

    etc.

    It seem like it should be simple.
    But it's got me frustrated

    Thanks,
    Chuck
    Chuck
    秋音的爸爸

  • #2
    Well I had to answer my own question

    I did up a generic case just for heck.

    Here is a simple file find and replace (update) batch file

    It will work on 2000 and XP (and most likely NT too)

    Code:
    @echo off
    rem findandreplace.bat
    
    if %1=="" goto help
    if %3=="" goto help
    if not exist "%1null" goto nobase
    if not exist "%3" goto nosource
    if %1%2==%3 goto err
    
    set ##basepathdr##=%1
    set ##sourcefile##=%2
    set ##destinfile##=%3
    
    cmd /X /R
    for /f "delims=" %%g in ('dir /s /b /a:d %##basepathdr##%*') do @if exist "%%g\%##destinfile##%" echo copy /b /y /z /v "%##sourcefile##%" "%%g\%##destinfile##%"
    
    goto end
    :help
    echo syntax: findandreplace basepath filetofind locationofsourcefile
    echo.
    echo eg findandreplace c:\ oldfile.text \\share\newfile.text 
    echo where all occurences of old file will be replaced by sourcefile
    echo.
    echo eg findandreplace c:\windows\ somedir\oldfile.text \\share\newfile.txt
    echo in this case the search would begin in c:\windows
    echo and oldfile would only be replaced if it was under a directory called "somedir"
    goto end
    
    :nobase
    echo Base path not found ("%1")
    goto end
    
    :nosourse
    echo Source file not found ("%3")
    goto end
    
    :err
    echo error: Can not overcopy source
    :end
    echo.
    enjoy.
    Chuck
    秋音的爸爸

    Comment

    Working...
    X