Batch file: Change names on files with certain extensions in multiple directories based on the directory name and copy into one directory

batch filefile-transferrename

I am all new to batch files and DOS, but I hope you can help me anyway.

I need to copy files of a specific file type that are now in separate subdirectories, one such file per directory(but more files with different file types). They all have the same name, so when copying to a new folder the last file to be copied gets replaced by the following file when using the command described in this question: Copy files with certain extensions from multiple directories into one directory.

Would it be possible to first change the file names to the names of their respective directory and then use the command in the example? Or even better, change the name of the new copy of the file before putting it in the new directory, or after copying it there but before copying the following file?

Best Answer

Ok, here's one. You might want to consider using something more flexible than a batch file; a simple C++ program would have been much easier. Batch does not play nice with the string manipulation necessary to extract the directory name.

@ECHO OFF

SETLOCAL EnableDelayedExpansion

SET _destination=%~1
SHIFT

SET _source=%~1
SHIFT

SET _cmdstring=dir /b /s

:LoopGetExt
    SET _cmdstring=%_cmdstring% "%_source%\*%~1"
    SHIFT
    IF NOT "%~1"=="" GOTO LoopGetExt
::End LoopGetExt

FOR /F "tokens=*" %%i IN ('%_cmdstring%') DO (
        SET _fullpath=%%i
        SET _splitpath=!_fullpath:\=^

!
        SET _filename=%%~ni%%~xi
        FOR /F "tokens=*" %%j IN ("!_splitpath!") DO (
            IF NOT "%%j"=="!_filename!" SET _dirname=%%j
        )

        ECHO "%%i" =^> "%_destination%\!_dirname!_!_filename!"
        COPY "%%i" "%_destination%\!_dirname!_!_filename!"
)

ENDLOCAL

At the moment nothing has been hardcoded: to use it, do this.

copyfiles.bat <destination> <source> <ext> [ext]

e.g.

copyfiles.bat "C:\Dest" "C:\Source" .txt .xml .csv .log

EDIT (as per request in first comment)

::copyfiles.bat <destination> <source> <ext> [ext]
::e.g.
::copyfiles.bat "C:\Dest" "C:\Source" .txt .xml .csv .log

@ECHO OFF

SETLOCAL EnableDelayedExpansion

SET _destination=%~1
SHIFT

SET _source=%~1
SHIFT

SET _cmdstring=dir /b /s

:LoopGetExt
    SET _cmdstring=%_cmdstring% "%_source%\*%~1"
    SHIFT
    IF NOT "%~1"=="" GOTO LoopGetExt
::End LoopGetExt

FOR /F "tokens=*" %%i IN ('%_cmdstring%') DO (
        SET _fullpath=%%i
        SET _splitpath=!_fullpath:\=^

!
        SET _filename=%%~ni
        FOR /F "tokens=1 delims=. " %%j IN ("!_splitpath!") DO (
            IF NOT "%%j"=="!_filename!" SET _dirname=%%j
        )

        ECHO "%%i" =^> "%_destination%\!_dirname!%%~xi"
        COPY "%%i" "%_destination%\!_dirname!%%~xi"
)

ENDLOCAL
Related Question