Search code examples
batch-filexcopy

Pass through folder with any specific pattern


I have a batch script that would only copy pdf file, based on the user inputted code. As soon as they hit enter it will look for a folder that has same filename with the inputted code.. then if exist it will copy the pdf inside..

Now I'm having a problem because in one sub-folder, it's file name doesn't have standard filename.

Like for ex : sometimes its filename is UV DATA others is UV-DATA , UV

example folder structure storage for the pdf file :

server
'----22050-022
     '----Folder1
               22050-022-DATA.pdf

example folder destination :

server2
'----Folder
     '----22050-022
          '----UV DATA
               '----DOCUMENTS

The UV DATA folder is the only one is changing.. How can I set only only UV and second word is any specific pattern?

Sample XCopy if it is a static code and make default file name is UV DATA:

set /p code=Input Control Number:

xcopy "%MySetPath%\Folder\%code%\%code%-DATA.pdf" "%MySetDestination%\Folder1\%code%\UV DATA\DOCUMENTS\" /D /E /C /I /Y /H

I only tried something like this but doesn't work:

set /p code=Input Control Number:

xcopy "%MySetPath%\Folder\%code%\%code%-DATA.pdf" "%MySetDestination%\Folder1\%code%\UV*.*\DOCUMENTS\" /D /E /C /I /Y /H

Solution

  • @echo off
    setlocal
    
    rem Change current directory to HOMEDRIVE.
    cd /d "%HOMEDRIVE%" || (
        >&2 echo Failed to change directory to "%HOMEDRIVE%"
        exit /b 1
    )
    
    rem Get the control number i.e. 22050-022.
    set /p "code=Input Control Number: " || exit /b 0
    
    rem Set initial source path and destination path here.
    @rem Note: destination path without trailing "\UV*\DOCUMENTS".
    set "source=%HOMEPATH%\%code%\Folder1\%code%-DATA.pdf"
    set "destination=%HOMEPATH%\Folder\%code%"
    
    rem Check initial source path.
    if not exist "%source%" (
        >&2 echo Initial source path "%source%" not exist.
        exit /b 1
    )
    
    rem Check initial destination path.
    if not exist "%destination%" (
        >&2 echo Initial destination path "%destination%" not exist.
        exit /b 1
    )
    
    rem Get "%%destination%%\UV*\DOCUMENTS\" path.
    (
        set "destination="
    
        for /d %%A in ("%destination%\UV*") do if not defined destination (
            @rem Force an echo on line break.
            if exist "%%~A\DOCUMENTS\" set "destination=%%~A\DOCUMENTS\"
        )
    
        if not defined destination (
            >&2 echo Failed to get destination from "%destination%".
            exit /b 1
        )
    )
    
    rem Copy file from source to destination if file date is newer.
    xcopy /d /y "%source%" "%destination%"
    exit /b 0
    

    This will try to find the destination path with UV* in the path as specified in the OP and comments as to my understanding.

    Change directory to %HOMEDRIVE% else a path starting with %HOMEPATH% can be invalid if the the current directory is on another drive. If you use a drive which mirrors the %HOMEPATH% then you can disable the change of directory to remain on that drive.

    The destination path is resolved to the DOCUMENTS folder, else the destination variable will be detected as undefined.

    Checks are done to ensure initial source and destination paths exist.

    Since only 1 file is being copied, using xcopy arguments that apply for 1 file. Check to see if I am correct.

    To understand what the code is doing, set the line @echo off to @echo on to test.