Search code examples
windowsbatch-filecmdbatch-rename

Files having same prefix to be moved to another directory


I have a files which doesn't have extension ending A_INI, A_FIF. And I need to write a code to search this files by user entering only A, if Exist I need to copy A_INI, A_FIF to another folder.

Example folder contains A_INI, A_FIF, A_LOG by prompting user enters file name as A.

I have to check files starting with A_* and if exist i have to move it to another folder.

How can this be achieved using batch script?


Solution

  • Not tested:

    @echo off
    
    setlocal
    set "source_dir=C:\source"
    set "destination_dir=C:\dest"
    
    set /p "mask=Enter a pattern: "
    
    pushd "%source_dir%"
    for %%# in ("%mask%_*") do (
       copy /y "%%~#" "%destination_dir%"
    )
    endlocal