batch-filexcopytabletop-simulator

Copy list of files from directory+subfolders to another folder


First of all, I am a total beginner. I was trying an ultimate script bat file solution for a game. To not annoy you with details, let me tell you what I tried to do.

Example:

  1. I have 17 files. 10 of them are .jpg and 7 of them are .obj files.
  2. The images are in path \mods\images and the objects are in path \mods\models.
  3. I want to list all 17 missing files in a list.txt
  4. The bat file will read that list, search for the files and paste them into the TTSmissing folder

and here are my problems:

  1. The bat script only looks exactly into the source path, but not into subfolders (that's why I wrote \mods\images\, to test if it works) so what I basically want is: \Tabletop Simulator\Mods\ as source path and the script shall look into all subfolders, too.
  2. The list.txt only works, when the filenames also have their extensions. is it possible to change the script so i don't need the extension? so it will only look for names and copy the files? (example: the names in the list have to be like: hello123.jpg. It's not working when its only hello123.)
  3. How do I need to change the bat script if i don't want a list.txt but just put the list of files right into the bat file?
@echo off
mkdir %USERPROFILE%\Desktop\TTSmissing
set src_folder=%USERPROFILE%\Documents\My Games\Tabletop Simulator\Mods\Images
set dst_folder=%USERPROFILE%\Desktop\TTSmissing
set file_list=%USERPROFILE%\Desktop\list.txt

for /f "tokens=*" %%i in (%file_list%) DO (
    xcopy /S/E "%src_folder%\%%i" "%dst_folder%"
)
pause

Solution

  • @echo off
    setlocal
    
    set "src_folder=%USERPROFILE%\Documents\My Games\Tabletop Simulator\Mods"
    set "dst_folder=%USERPROFILE%\Desktop\TTSmissing"
    set "file_list=%USERPROFILE%\Desktop\list.txt"
    set "ext_list=.gif .jpeg .jpg .mp4 .obj .pdf .png .webm"
    
    if not exist "%dst_folder%" md "%dst_folder%"
    
    for /d /r "%src_folder%\" %%A in (*) do (
        pushd "%%~A" && (
            for /f "usebackq delims=" %%B in ("%file_list%") do (
                for %%C in (%ext_list%) do (
                    if exist "%%~B%%~C" (
                        echo copy /y "%%~B%%~C" "%dst_folder%\"
                    )
                )
            )
            popd
        )
    )
    

    You just want to copy files so copy is easier to use than xcopy. The code will echo the copy command to test whether it is working how you want it. If satisfied, remove the echo in front of copy and run the code again to do the actual copy process.

    A for /d /r loop will recursively iterate the subdirectories in %src_folder%. pushd will change the current directory to each subdirectory so as can work relative to the source files.

    The for /f loop will iterate each line from %file_list%. The simple for loop will iterate each of %ext_list%. If current "name.extension" exists, it will be copied to %dst_folder%.

    If you set variables names in a script, it is usually a good idea to use setlocal to keep the variables defined local to the script.

    To view help for a command, use command /?. This will work for many of commands used in the code.

    View command /? help for copy, for, if, setlocal ...