Batch to copy folders with same ending to another existing folder - windows

I want to copy multiple folders with the same ending from a main folder to another folder. My .bat file doesn't work unfortunately. This batch file will be put in the same directory as the main folder.
Folder structure:
Folderdirectory/main folder/folderxxx10.1, folderyyy10.2, folderzzz10.3, etc.
Location of the batch file:
Folderdirectory/copy.bat
Where to copy the folders:
Folderdirectory/1 AAA
1 AAA is the destination folder (The space is on purpose). Other destination folders have the same structure: 2 BBB , 3 CCC, etc.
For example: All folders in Folderdirectory/main folder/ with the ending "10.1" should be copied to an existing folder Folderdirectory/1 AAA.
This should be looped in order to copy folders with the ending 10.2 (copy to Folderdirectory/2 BBB ), 10.3 (copy to Folderdirectory/3 CCC ) , ..., 10.30
The code I wrote:
for /L %%i in (1,1,30) DO (
xcopy "%~dp0%\main folder\*10.%%i" "%~dp0\%%i *" /E
)
PAUSE
Error I get: (example for i=30)
File not found - 30 *
0 File(s) copied
Thank you in advance! I am new to coding and would be very grateful for any help.
Kind regards,
TamTam
UPDATE: I updated the code:
for /L %%i in (1,1,30) do for /D %%j in ("%~dp0\main folder*10.%%i") do
xcopy /E /I "%%~j" "%~dp0\%%i*")
PAUSE
The batch file now copies all folderxxx10.1, folderyyy10.2, etc. to Folderdirectory/ and not to Folderdirectory/1 AAA, Folderdirectory/2 BBB for instance. So, there must be something wrong with the destination "%~dp0\%%i*".
Furthermore, it copies the files in the folder, not the folder itself.
Thanks so far for your help!

Related

Windows Batch: Rename a folder to the name of a file inside

I have ~3000 folders, each with multiple files in it. Each folder contains a txt file, with a few other file types. The folder names are hashed, so right now it's a jumble of random digits. I want to rename each folder to the same name as the txt file that is inside that folder. For example:
123456/myfile.txt
Should become:
myfile/myfile.txt
The folders do not contain any subfolders, if that matters.
Any help is greatly appreciated!
for /d %%a in (*) do (
for %%b in ("%%a\*.txt") do (
ECHO ren "%%a" "%%~nb"
)
)
use a for /d to iterate over your folders and another plain for to get the filename (assuming, there is exactly one .txt file in each folder). %%~nb gets the name only without extension).
NOTE: after troubleshooting, remove the ECHO to enable the rename command.

Merge all files from the current folder and all subfolders in one file using .bat file

I'm looking for a batch file that merges the content of all files in the current directory and files from all subdirectories.
Also, that would be perfect if files were separated by several new lines in the big file and probably contain the filename with filepath above the each file content.
For example, there are two files in the current folder D:\ , where our batch file is located:
1.
d:\file1.txt contains:
some
text here
hahaha
2.
d:\folderabc\file2.mp3 contains:
doremi text
I run merge.bat file on d:\ and it creates a merge file result.txt (or whatever extension) with such content:
=========d:\file1.txt=========
some
text here
hahaha
=========d:\folderabc\file2.mp3=========
doremi
I appreciate if someone share his solution for this problem.
Thank you so much.
Test this with your folder. It assumes the files are text files.
edit: This has a fix - and you can remove the "d:\base\folder" to run it from the current directory.
#echo off
for /r "d:\base\folder" %%a in (*) do (
(
echo =========%%a=========
type "%%a"
echo(
echo(
echo(
)>>"%temp%\temp.file"
)
move "%temp%\temp.file" . >nul
echo done
pause

Windows 7 Batch - Create subfolder, then find files with certain text in file name and move those files in the newly created subfolder

Tried my best searching for a solution but close to my need was this example which did not work. Bash: Moving multiple files into subfolders
I am not a programmer so unable to create the batch file myself for Windows 7. Any help will be appreciated.
Needed code for a batch file that does the following:-
Searches the folder for all files that have "_F1" in the file name
Creates a subfolder named as "F1" where this file is located
Move all the files searched in step 1 to the folder "F1" created in step 2
Ideally, the batch file should execute from parent folder and should complete the 3 steps in all subfolders at least till 3 levels down the parent folder.
Thanks in Advance for any help.
I tried and came up with this. Works, but is very raw. Needs to be run manually from inside of each folder (100's of them)
MKDIR F1
MKDIR F2
DO 500
move *_F1*.* F1
move *_F2*.* F2
ENDDO
Try like this :
#echo off
for /f "delims=" %%a in ('dir /s/b/a-d *.* ^| find /i "_F1"') do (
if not exist "%%~dpaF1" md "%%~dpaF1"
move "%%~fa" "%%~dpaF1")

How to Move or Copy Folders using directory Wildcards within Batch File?

I need to set up a batch file that will copy files from a preset directory, several layers down, organized into a folder in the current directory using batch files.
The file structure looks something like this,
AAA
--0001
----textures
------file1.tga
------file2.tga
------file3.tga
--0002
----materials ...
--0003
----textures ...
--0004
----scripts ... etc
--0005 ...
--0006 ...
BBB
--0001 ... etc
---textures ... etc
CCC ...
DDD ... etc
I want to move each textures, materials, models (etc.) folders into a single folder beneath the parent item (eg. AAA) so that the folder structure then looks like this, with all sub-files in tact,
AAA
--textures
--materials
--models
--scripts
I've been trying to use a code similar to this, with no success as of yet,
for /d %%a in ('dir /b .\AAA*\textures') do copy "%%a" ".\AAA"
Whereas the wildcard would go between 'AAA' and 'textures' folders. The same would apply for every other 2nd-generation sub-folder.
I just tried another version of the command which moved the files, but not the folder. I'm launching the batch file from within the "AAA" folder.
for /d %%a in (0001) do copy "%%a" "."
I think the issue is not knowing how to properly input directory wildcards into the "for" command, which could be a very simple fix.
Any help would be greatly appreciated beyond measure.
Put this code in a .bat file, and run it inside your AAA directory
for /f "tokens=*" %%a in ('dir /b') do robocopy %%a . /s /move
for /f "tokens=*" means that all the returned values by 'dir /b' will be processed
dir \b will list the directory in bare format
robocopy is the main command, that will do the copy job.
source %%a is the variable containing the iterated subfolder name {0001\textures, 0002\materials, ...}
destination . means copy to the working directory, which is AAA in our case
option /s to search subdirectories
option /move to move instead of copy

To copy entire subfiles to other (only one) folder without path in windows platform

I did lots of search however i couldnt achieve what i want to do :
I have a folder which contains lots of folders and files under it. I just want to copy entire JPEG files in this folder to another folder without copying previous path tree.
I used this batch command like that :
for /r "C:\source" %%f in ("*.JPG") do #copy %%f "C:\dest" /Y
It works some files which dont contain any space in the name of files. However, it doesnt work files which contains space in file name like that :
it can find and copy "aabb.JPEG"
it can not copy "aa bb.JPEG"
any solutions?
The following works great for me
#ECHO OFF
FOR /R "c:\source" %%F IN (*.jpg) DO (
copy "%%F" "c:\dest\" /Y
)

Resources