How to merge two folders including subfolders in batch - windows

Context
I'm trying to make a batch script that can be thrown into a pre-existing folder and upgrade it. Imagine a folder that looks like this:
domfolder
LICENSE
merge_new_stuff.bat
words.txt (has 6 words)
awesomefolder
garfield.png
upgrade_assets
upgradeinfo.txt
new_files
words.txt (has 7 words)
awesomefolder
odie.png
awesomest
MONKEY.txt
subfolder
subsubfolder
numbers.txt
The sole purpose of merge_new_stuff.bat is to take everything inside the new_files folder, and bring it into domfolder, replacing any pre-existing files like words.txt, leaving anything else alone, and getting rid of the upgrade_assets folder. So if everything goes right, the folder structure should like this after running:
domfolder
LICENSE
merge_new_stuff.bat
words.txt (has 7 words)
awesomefolder
garfield.png
odie.png
awesomest
MONKEY.txt
subfolder
subsubfolder
numbers.txt
What I've Tried
First instinct is to just use a simple move command: move /y upgrade_assets\new_files\*
That upgrades words.txt, but it totally ignores the folders. Only part of what I want.
I tried a for loop to see if it'd be different somehow, same results:
for %%A in (upgrade_assets\new_files\*) do move /y %%A
Next, I tried looping through dir results alongside the basic move:
for /f "tokens=*" %%A in ('dir /b /s /a:d "upgrade_assets\new_files"') do move /y "%%A\*.*" "%~dp0%%~nxA"
This almost worked, properly moving odie.png and subfolder\subsubfolder\numbers.txt. But once it tries to move MONKEY.txt, it... decides to make a file called "awesomest" that has the contents of MONKEY.txt? So clearly, it doesn't handle going down pre-existing folders, which is definitely not helpful.
Looking around, I only found one thread that seems close to what I want. Most of the replies mention xcopy/robocopy, but I want to avoid the copy/delete method as much as possible considering the potential size of updates. And when I changed the accepted answer to fit what I'm doing, it still had issues going more than one folder deep.
for /d /r upgrade_assets\new_files %%A in (*) do (
if exist "%~dp0%%~nA" (
dir "%%A" | find "0 File(s)" > NUL
if errorlevel 1 move /y "%%A\*.*" "%~dp0%%~nA"
) else (
move /y "%%A" %~dp0
)
)
I'm pretty sure the issue is centered around %~dp0%%~nA, I need a way to have it read more than just the folder the file being copied is in, which is all %%~nA shows.
Preferably, the solution should be completely independent of what actual files are in the folder, so it can be used with any update. If I need external tools, I would like them to be free/libre software if possible. I'm running Windows 10, but I know this script will be run by people using Windows 7.

Quite frankly this will do exactly what you want.
robocopy .\upgrade_assets\new_files\ . /E /MOVE
It will create the directories and files as well as delete from source once done.

Related

Program to move unlocked files to an new folder

i need something that will move files from one folder to another network location, but leave any locked files that are currently being written to!
any ideas?
apols i am no developer, but i have been trying to use
ROBOCOPY C:\test c:\test\q /move /R:0 /W:0
but even if i open a file, it still moves it to the new directory
mal
You can use for %%f in (*) do move %%f destination\%%f (in a batch file) and just allow each individual invocation of move to fail, watching the errors scroll by.
There are also options for making for %%f work recursively, though that's a bit involved.

Move specific files out of subfolders and delete said subfolders

I recently exported the HDD on my panasonic camera to my notebook and noticed that the video files weren't ordered by name, but by their parent directory and said parent directory was clouded with a bunch of miscellaneous files.
To put things into perspective, the directory tree looks something like this:
\Panasonic
\PRG00A
\PRG00B
...
\PRG069
Panasonic is located inside a bunch of folders, hence why I would like to put my batch file alongside \Panasonic. And have it work relatively to its location.
So basically I want to create a batch file move.bat, which shall traverse the subdirectories of \Panasonic and move out any video files (with extension .MOD to simplify things) and afterwards delete the parent directory (e.g. \PRG00B).
The result would be that the \Panasonic directory only includes video files instead of sub-directories with a bunch of rubbish.
What I've got so far (keep in mind that this is my first batch script, and I haven't even tested it fully). The choice to continue doesn't work, by the way. Not sure why, though.
#echo off
cls
set dirName=%~dp0Panasonic
goto question
:start
goto move
goto end
:move
for /D %%G in ("%cd%") do (
for %%I in ("%%G") do (
if %%I equ "*.MOD" (
move /Y %%I %dirName%
)
)
rmdir /s /q %%G
)
:end
echo Done.
pause
endlocal
exit
:question
set /P c="Are you sure you want to proceed with moving video files from %dirName%? [Y/N]"
if /I %c% equ 'y' (
echo Moving files...
goto start
) else (
goto end
)
Once again, this is my first time creating a batch file, so any help is much appreciated!
you are almost there, but just need to fix a couple of things, very easy to fix, in fact, you just need to simplify a lot your code.
Just in three simple steps
Step 1. To loop over all the directories you already had it right, your friend is for /d
for /d %%a in (*) do echo %%a
Step 2. To move all the .mod files in each of the directories found, to its parent directory or one directory up in the hierarchy, that happens to be the current directory, you just need to
move %%a\*.mod .
don't use /y option, so it will not overwrite existing files already moved to the parent directory (You will have the opportunity the check the results later. Keep reading)
Step 3. And finally, remove the directory,
rd %%a
but don't use /s, so it will only work it the directory is empty, that is, if you have successfully moved out all of the files it contained. This way you can then browse thru them to see what is left without losing any data.
So, your moveupallmod.bat becomes simply
#echo off
for /d %%a in (*) do (
move "%%a\*.mod" .
rd "%%a"
)
and that's all!

How would I copy all files and folders except for hidden ones?

On my drive S:\ I have a folder X which has multiple files and subfilders, each of which in turn contains its own files and subfolders and so on. Each folder(at any level) contains a hidden folder named the same way, say HID, with several files in it.
I have the same structure in another drive D:\ - same folder X with the same structure, but with slighly different contents in the files.
Basically I need to copy-and-replace the contents of X from S:\ into D:\, but not touch the hidden folders hamed HID (basically, they are unique in two independent ways - by the fact that they are named HID and by the fact that they are hidden).
I'm lazy to do this manually and don't feel like writing a C++ application to do this either. Is there any easy way to do this using a small bat file or a direct shell command with smart arguments?
You may be interested in xcopy command. As it says, "By default, xcopy does not copy hidden or system files.". It also has an exclude option, which seems to be used for ignoring specially named files.
Try this: lose the echo and pause if it's echoing the right commands.
#echo off
cd /d "s:\folder x"
for /f "delims=" %%a in ('dir /a-d /b /s ^|find /v "\HID\" ') do (
echo copy /y "%%a" "d:%%~pnxa"
pause
)

Windows batch copy files from subfolders to one folder

I had tried to make batch script that copies all *.tif files located in D:\images(random named subfolders here) to d:\all.
xcopy D:\Downloads\*.TIF D:\temp\ /s
works, but it copies with all folder tree. I tried to use other keys, but its dont works.
Thanks for help!
FOR is your friend. Read HELP FOR on the /R option and the %~nx variable substitution; and then try this very simple code.
pushd d:\downloads
for /r %%a in (*.tif) do (
echo COPY "%%a" "d:\temp\%%~nxa"
)
popd
watch carefully the results and then remove the ECHO command.
You will have to refine the code to cope with errors, duplicate names, edge cases, names with reserved characters, race conditions, cosmic events...
Searched files using windows file explorer for e.g. *.gif , I got files in search window, used Edit=>Select All , copy and then pasted to desired folder. This copied all the gif files in all sub directories to single folder.
For large number of files, it sometimes hangs/not responding, but otherwise works ok.
pushd D:\Source
for /r %%a in (*.?*) do (
MOVE "%%a" "D:\Destination folder\%%~nxa"
)
popd
You can also use the XXCOPY freeware. Works like XCOPY, but when you use a /SG parameter, it flattens the sub-directories. See how to use it here.

Windows Batch File Looping Through Directories to Process Files?

I need to write/use a batch file that processes some imagery for me.
I have one folder full of nested folders, inside each of these nested folders is one more folder that contains a number of TIF images, the number of images vary in each folder. I also have a batch file, lets call it ProcessImages.bat for Windows that you can "drop" these TIF files on (or obviously specify them in a command line list when invoking the bat); upon which it creates a new folder with all my images process based on an EXE that I have.
The good thing is that because the bat file uses the path from the folders you "drop" onto it, I can select all the TIFs of one folder and drop it to do the processing... but as I continue to manually do this for the 300 or so folders of TIFs I have I find it bogs my system down so unbelievably and if I could only process these one at a time (without manually doing it) it would be wonderful.
All that said... could someone point me in the right direction (for a Windows bat file AMATEUR) in a way I can write a Windows bat script that I can call from inside a directory and have it traverse through ALL the directories contained inside that directory... and run my processing batch file on each set of images one at a time?
You may write a recursive algorithm in Batch that gives you exact control of what you do in every nested subdirectory:
#echo off
call :treeProcess
goto :eof
:treeProcess
rem Do whatever you want here over the files of this subdir, for example:
for %%f in (*.tif) do echo %%f
for /D %%d in (*) do (
cd %%d
call :treeProcess
cd ..
)
exit /b
Aacini's solution works but you can do it in one line:
for /R %%f in (*.tif) do echo "%%f"
Jack's solution work best for me but I need to do it for network UNC path (cifs/smb share) so a slight modification is needed:
for /R "\\mysrv\imgshr\somedir" %%f in (*.tif) do echo "%%f"
The original tip for this method is here
Posting here as it seems to be the most popular question about this case.
Here is an old gem I have finally managed to find back on the internet: sweep.exe. It executes the provided command in current directory and all subdirectories, simple as that.
Let's assume you have some program that process all files in a directory (but the use cases are really much broader than this):
:: For example, a file C:\Commands\processimages.cmd which contains:
FOR %%f IN (*.png) DO whatever
So, you want to run this program in current directory and all subdirectories:
:: Put sweep.exe in your PATH, you'll love it!
C:\ImagesDir> sweep C:\Commands\processimages.cmd
:: And if processimages.cmd is in your PATH too, the command becomes:
C:\ImagesDir> sweep processimages
Pros: You don't have to alter your original program to make it process subdirectories. You have the choice to process the subdirectories only if you want so. And this command is so straightforward and pleasant to use.
Con: Might fail with some commands (containing spaces, quotes, I don't know). See this thread for example.
I know this is not recursion (iteration through enumerated subdirectories?), but it may work better for some applications:
for /F "delims=" %%i in ('dir /ad /on /b /s') do (
pushd %%i
dir | find /i "Directory of"
popd
)
Replace the 3rd line with whatever command you may need.
dir /ad - list only directories
The cool thing is pushd does not need quotes if spaces in path.
rem Replace "baseline" with your directory name
for /R "baseline" %%a in (*) do (
echo %%a
)

Resources