none
Batch File Search and Replace a String RRS feed

  • Question

  • I've written a batch file to handle the copying of files.  I'm trying to reduce the number of parameters that I pass to the batch file.

    I want the batch file to perform a string replace on the directory path that is passed as a parameter to the batch file.

    Here is how I run the batch file:

    COPY_FILE.BAT C:\DATA\FTP_DATA\ACME\IN      (I'm passing directory path "C:\DATA\FTP_DATA\ACME\IN" as parameter %1)

    So my batch file will copy a file to the directory path represented by parameter %1.

    But then I want the file copied to a second directory.  I want the batch file to replace string "FTP_DATA" in the directory path with string "FTP_DATA\ARCHIVE"

    I know I could pass this path as a second parameter, but I want to limit the number of parameters so figured I could have the batch file perform a search and replace on string "FTP_DATA".

    After searching the internet, I thought I found the answer by using the following command in the batch file:

    set ARCHIVE_PATH=%1:FTP_DATA=FTP_DATA\ARCHIVE

    But it doesn't perform a search and replace.  It just appends to the directory path with the following result:

    C:\DATA\FTP_DATA\ACME\IN:FTP_DATA=FTP_DATA\ARCHIVE

    What I want is the this:

    C:\DATA\FTP_DATA\ARCHIVE\ACME\IN

    Any suggestions?

    Thursday, March 27, 2014 2:58 PM

Answers

  • Your syntax is not correct. Example:


    @echo off
    setlocal enableextensions
    set ARCHIVE_PATH=C:\DATA\FTP_DATA\ACME\IN
    echo %ARCHIVE_PATH%
    set ARCHIVE_PATH=%ARCHIVE_PATH:FTP_DATA=FTP_DATA\ARCHIVE%
    echo %ARCHIVE_PATH%
    endlocal
    


    -- Bill Stewart [Bill_Stewart]

    Thursday, March 27, 2014 3:06 PM
    Moderator

All replies

  • Your syntax is not correct. Example:


    @echo off
    setlocal enableextensions
    set ARCHIVE_PATH=C:\DATA\FTP_DATA\ACME\IN
    echo %ARCHIVE_PATH%
    set ARCHIVE_PATH=%ARCHIVE_PATH:FTP_DATA=FTP_DATA\ARCHIVE%
    echo %ARCHIVE_PATH%
    endlocal
    


    -- Bill Stewart [Bill_Stewart]

    Thursday, March 27, 2014 3:06 PM
    Moderator
  • My god your quick!

    It works!!!

    Thanks so much!

    Thursday, March 27, 2014 3:16 PM