1

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
ikegami
  • 367,544
  • 15
  • 269
  • 518
pogos
  • 45
  • 8

1 Answers1

1
@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 ...

michael_heath
  • 5,262
  • 2
  • 12
  • 22
  • `(*.*)` unfortunately didnt work. this immediately causes the RAM to rise and it doesnt find a single file (compared to earlier, it at least found the jpg files). this is kinda confusing. it makes no sense for me why it works perfectly with a folder with only a few files, but not with folders with alot of files. and why it immediately can find some files types, but others not when src is a folder with alot of files. also strange thing is the order. without *jpg, it finds pngs. without *.png it finds object files, although there are still the .mp4 exntensions etc existing. – pogos Apr 28 '20 at 16:50
  • i tried your modification. thanks for your efforts by the way, i appreciate it! unfortunately your modified script is doing this: it only finds the files with the very first extension. in your modification, obj is the first one, so it finds all objects. when i put, for example, jpg to the first, and obj to second, it finds all jpg files. but it wont find all other ones after the first exntension, and it will again fall into the rising-RAM bug thingy. – pogos Apr 28 '20 at 17:00
  • 1
    I have changed the code so that `findstr` is not being used as a `for /f` now reads the list file. It is all iterating, so hopefully performance will be improved. – michael_heath Apr 29 '20 at 02:19
  • This did the trick! i dont know how i can thank you. you took so much time for a bloody beginner's problem. not sure if anyone else would have put so much time and effort to help out. now it finds the files in the list.txt in a few seconds. (btw i added the `if not exist "%dst_folder%" md "%dst_folder%"`command to your improved script) thanks you very much! i wish you good health in this current hard time! – pogos Apr 29 '20 at 11:49