Copying all files from several folders to 1 folder with a batch - batch-file

I'm trying to copy several files from several folders into 1 folder with this code:
for /R \\wiki\help\images %%f in (*.jpg, *.png, *.gif) do (
copy %%f "\\10.101.3.21\wikitest\wiki\wikiimages"
)
However, it copies all the files as wikiimages, and I can't get around it. I just want to use the copy command, not an external program (so no xcopy or the like). How can I do this?

I'm pretty sure
for /R \wiki\help\images %%f in (*.jpg, *.png, *.gif) do copy %%f "\10.101.3.21\wikitest\wiki\wikiimages\"
is the way to fix this, it usually need the \ to know its a folder and not a filename.

Related

how to copy only modified files using LogFile.txt?

when it comes to creating a batch-file I would sadly call myself a newbie and therefore it's kind of difficult for me to achieve what I want on my own:
so here is how my codes look:
#ECHO OFF
for /r "C:\source" %%f in (*) do copy /y "%%f" C:\ReadyToExport
del C:\ReadyToExport\*.pdf*
for /f "delims=" %%i in ('xcopy "C:\ReadyToExport" C:\import /EXCLUDE:LogFile.log /S /E /D') do (
echo %%i >> C:\LogFile.log)
PAUSE
and here is what the code exactly does
1- for /r "C:\source" %%f in (*) do copy /y "%%f" C:\ReadyToExport
this line copies all files from source to another target folder the good thing is that this command copies all the files inside other subfolders from source and paste them into the folder C:\ReadyToExport without any subfolders.
2- del C:\ReadyToExport\*.pdf*
this command filters all the .pdf files because they are unnecessary
3-
for /f "delims=" %%i in ('xcopy "C:\ReadyToExport" C:\import /EXCLUDE:LogFile.log /S /E /D') do (
echo %%i >> C:\LogFile.log)
this command basically copies all files from "C:\ReadyToExport" into C:\import and writes the name of the recorded file in LogFile.log then the copied files will be excluded when the script runs again because I don't want to copy the files again if I already copied them before
and here is what I want to achieve:
I want to copy only modified files from the folder "C:\ReadyToExport" to the target C:\import but keep in mind that
the files in the target folder "C:\import" will be deleted but since the names of the files that have already been copied are registered in LogFile.log the files will be excluded and not copied again.
in another word: files in target file do not exist anymore but their names are written in LogFile
so is there any way to copy only modified ones? even though they don't exist in the target folder anymore? but their names are in LogFile.txt? can the script somehow write the last modified date in LogFile.txt near the file name? and then compare the date from the source? so it copies only files that have been changed and ignore all files that didn't?
p.s: using Xcopy, robocopy, etc is not a problem
any answer will be appreciated.
thnx
your method does, in fact, works so thank you so much not the way the I want with exclude:logfile.log
but this definitely worked:
robocopy C:\ReadyToExport\ C:\import /M /E
since it copies only the files that hast the attribute archivable checked in the settings.

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:
I have 17 files. 10 of them are .jpg and 7 of them are .obj files.
The images are in path \mods\images and the objects are in path \mods\models.
I want to list all 17 missing files in a list.txt
The bat file will read that list, search for the files and paste them into the TTSmissing folder
and here are my problems:
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.
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.)
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
#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 ...

Batch rename files to include part of the folder name

I'm trying to write a batch file to rename multiple files inside multiple folders and can't quite figure it out.
The folder names all follow the same structure: "foo.bar_baz" and all the files inside the folder are 2 character codes (AA, AB, BC) and all share the same extension. foo and baz are constant throughout all the folders and bar changes every time. I want to rename all the files as bar_.
I don't have a lot of experience with batch files so I'm probably missing something obvious but I can"t figure it out.
#echo off
for /d /r "d:\some\dir\" %%d in (*) do (
for /f "delims=._ tokens=2" %%b in ("%%~nxd") do (
for %%f in ("%%~d\??.*") do ren "%%~f" "%%b_%%~nxf"
)
)
pause

Batch script to archive subfolder contents using 7zip with FOR /R

I have a folder structure like this:
C:\\\Logs\logs1\tracelogXXXX.log
C:\\\Logs\logs2\tracelogXXXX.log
C:\\\Logs\logs3\tracelogXXXX.log
Each folder has a bunch of tracelogXXXX's, and I've got the pseudocode for a script that loops through each folder, archives each log into its own .zip, and then delete the tracelog left outside the archive (because 7zip doesnt have move functionality).
But I have no batch experience really, and I can't even get the zipping to work properly.
I can't access the documentation for 7zip from where I am currently, so I've tried this:
CD C:\Logs
FOR /R %%i IN ("*.log") DO "C:\...\7za.exe" a -tzip "%%i.zip"
And also this:
CD C:\Logs
FOR /R %%i IN ("*.log") DO "C:\...\7za.exe" a -tzip "%%i.zip" "%%i\"
The first one goes and zips all of \Logs for each instance of a .log file, making many zips each bigger than the last. And the second makes zips for each instanceof a .log file, with nothing in them.
How do I just zip each log file, in its own zip, named after itself, while operating from the parent directory? Deleting the outer files afterwards doesn't seem hard to accomplish once I figure out whats wrong with this syntax, but this is the important part!
You can do this from the command line with no batch file needed:
FOR /F "usebackq tokens=* delims=" %A IN (`DIR "C:\Logs\*.log" /B /S`) DO "C:\Path\To\7za.exe" a "%~dpnA.zip" "%~fA" & DEL "%~fA"
To use in a batch file, just replace each % with %%.

Comparing Folders and Subfolders files using batch command

I have two folders. I want to compare the files inside these folders.There are some subfolders in root folders. These sub folders are also containing some files. I want to compare these files in subfolder also. For that I am trying to write a batch file. Any kind of solution will help me a lot
I would not use a batch file at all to accomplish this task. BeyondCompare is here to do exactly that, and does it seemingly well.
Now on the other hand you really wanna do it via batch file, I'd suggest you install a tool called diff tools, and you'll be able to do something like:
diff.exe <file1> <file2> <htmlfile>
On the command line.
hope it helps
UPDATE
As a follow up to your comment, I write this, which also seems to work for me, and doesn't use any external tool. This is a simple example, but you can make it better.
if exist compare.log del compare.log
if exist missing.log del missing.log
for /f "delims=" %%a in ('dir/b/a-d c:\test\1') do (
if exist "C:\test\2\%%a" (
fc "c:\test\%%a" "C:\test1\%%a" >> compare.log
) else (
echo %%a is missing >> missing.log
)
)
Pause
try dir /b /s
to search for files within subdirectory, instead of dir /b

Resources