Reputation: 45
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:
\mods\images
and the objects are in path \mods\models
.list.txt
TTSmissing
folderand here are my problems:
\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.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
.)@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
Upvotes: 1
Views: 679
Reputation: 5372
@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
...
Upvotes: 1