Page 1 of 3
Batch file for copy many files to many folders?
Posted: 07 Dec 2012 05:24
by taherkhani
Hi everyone,
i have many folders like this:
C:\books\5002.1 persian
C:\books\6001 russian
C:\books\6014.1 english
.
.
.
and have files like this:
C:\books\000-C5002-1.pdf
C:\books\001-030-C5002-1.pdf
C:\books\031-067-C5002-1.pdf
C:\books\068-093-C5002-1.pdf
C:\books\094-135-C5002-1.pdf
C:\books\136-167-C5002-1.pdf
C:\books\168-205-C5002-1.pdf
C:\books\206-243-C5002-1.pdf
C:\books\244-283-C5002-1.pdf
C:\books\284-312-C5002-1.pdf
C:\books\000-C6001.pdf
C:\books\001-022-C6001.pdf
C:\books\023-046-C6001.pdf
C:\books\047-081-C6001.pdf
C:\books\082-105-C6001.pdf
C:\books\106-170-C6001.pdf
C:\books\000-6014-1.pdf
C:\books\032-066-6014-1.pdf
C:\books\067-095-6014-1.pdf
C:\books\096-134-6014-1.pdf
C:\books\135-147-6014-1.pdf
C:\books\148-181-6014-1.pdf
C:\books\001-031-6014-1.pdf
.
.
.
I want a bach file for copy files to those folders.
I test this code, but not work:
move *5002-1.pdf 5002.1*
move *6001.pdf 6001*
move *6014-1.pdf 6014.1*
thanks for your help.
Re: Batch file for copy many files to many folders?
Posted: 07 Dec 2012 05:44
by abc0502
can you be more specific,
do you want the files that has the same number in it's names to be moved to the folders that has the same number ?
Re: Batch file for copy many files to many folders?
Posted: 07 Dec 2012 06:03
by abc0502
Does the the files that has numbers 5002 and 6001 always has C befor it and the 6014 has no letters before it ?
if there is any pattern in there names please point to it.
Re: Batch file for copy many files to many folders?
Posted: 07 Dec 2012 06:11
by taherkhani
can you be more specific,
do you want the files that has the same number in it's names to be moved to the folders that has the same number ?
yes , exactly, numbers be at the beginning of the folders and the end of files.
Re: Batch file for copy many files to many folders?
Posted: 07 Dec 2012 06:14
by taherkhani
Does the the files that has numbers 5002 and 6001 always has C befor it and the 6014 has no letters before it ?
if there is any pattern in there names please point to it.
Yes, some of the files have letter C before number, and some of them have not.
Re: Batch file for copy many files to many folders?
Posted: 07 Dec 2012 06:26
by abc0502
Test this first on some of this files:
Put this batch with the files and folders, the 3 must be in the same location "book" folder
Code: Select all
@Echo OFF
:: Create folders if not exist
IF NOT EXIST "5002.1 persian" MD "5002.1 persian" >nul
IF NOT EXIST "6001 russian" MD "6001 russian" >nul
IF NOT EXIST "6014.1 english" MD "6014.1 english" >nul
Setlocal EnableDelayedExpansion
For /F "tokens=1-4 delims=-" %%A in ('DIR /B /A:-D "*.pdf"') Do (
:: each token associated with a number
set "tok1=%%~nA"
set "tok2=%%~nB"
set "tok3=%%~nC"
set "tok4=%%~nD"
:: IF file starts with 000 , it will have 3 tokens only except that has number C6001
IF "!tok1!" == "000" (
IF "!tok2!" == "C5002" Move "%%A-%%B-%%C" "5002.1 persian"
IF "!tok2!" == "C6001" Move "%%A-%%B" "6001 russian"
IF "!tok2!" == "6014" Move "%%A-%%B-%%C" "6014.1 english"
) Else (
:: IF not start with 000, it will have 4 tokens except that has number C6001
IF "!tok3!" == "C5002" Move "%%A-%%B-%%C-%%D" "5002.1 persian"
IF "!tok3!" == "C6001" Move "%%A-%%B-%%C" "6001 russian"
IF "!tok3!" == "6014" Move "%%A-%%B-%%C-%%D" "6014.1 english" )
)
pause
Re: Batch file for copy many files to many folders?
Posted: 07 Dec 2012 07:20
by foxidrive
Make sure the folders exist first abc0502, otherwise the files will be renamed and overwritten.
Re: Batch file for copy many files to many folders?
Posted: 07 Dec 2012 07:22
by abc0502
I missed that, thanks
I will fix it up
Re: Batch file for copy many files to many folders?
Posted: 07 Dec 2012 07:35
by taherkhani
thanks very much, but some folders have unicode character and i have error with your code, i mean i have folder like "4002.1 عربی"
Re: Batch file for copy many files to many folders?
Posted: 07 Dec 2012 07:42
by foxidrive
Batch is very poor at handling unicode.
You might find that using a dual pane file manager like Total Commander is a better tool to tag and move, but it's still a manual process.
Re: Batch file for copy many files to many folders?
Posted: 07 Dec 2012 07:43
by abc0502
I just made the batch based on the folders and files you provided, you didn't say it must work for other files and folders as well.
you will have to follow the same steps in the batch for your other files and folders and add them manually,
as I guess it will be impossible cause there is no static pattern in the file names so i can make it work.
May be some one else here could help.
Edited,
ok,wait

i might have an idea
Re: Batch file for copy many files to many folders?
Posted: 07 Dec 2012 07:53
by abc0502
is this numbers in blue always 4 digits
C:\books\5002.1 persian
C:\books\6001 russian
C:\books\6014.1 english
and could be a folder contain for example 6001 and another one contain 6001.2
and does the folders always exist, i mean that i don't have to create the folders , right ?
Re: Batch file for copy many files to many folders?
Posted: 07 Dec 2012 08:01
by taherkhani
Yes, i have folders that have numbers like this:
236.1 فارسی
236.2 عربی
236.3 زبان
236.4 انگلیسی
236.5 ادبیات
236.6 هنر
236.7 معارف
236.8 دینی
.
.
.
and like this.
Yes, all folders exist and don't have to create them.
Re: Batch file for copy many files to many folders?
Posted: 07 Dec 2012 08:09
by foxidrive
@abc0502 : you will find that batch cannot handle Unicode very well.
Liviu has mentioned this several times in the past so I just pass when unicode is mentioned.
Search his posts for the other limitations too...
Evidently dir /b can't be used for characters outside of the current codepage.
Re: Batch file for copy many files to many folders?
Posted: 07 Dec 2012 08:32
by abc0502
@Foxidrive , thanks
I managed to make the batch work in general without the need for the manual setting but the unicode is just a problem, when it come to unicode in folder names, it just create new folders with identical names and then copy the files to that new folders not the old existing ones
This all what i managed to make, it will just create new folders with identical names and copy files to it
Code: Select all
@Echo OFF
:: Get a List of All Files
For /F "delims=" %%A in ('DIR /B /A:-D "*.pdf"') Do Echo %%A>>Files.lstx
:: Get a List of All Folders [ and replace "." with "-"
Setlocal EnableDelayedExpansion
For /F "delims=" %%A in ('DIR /B /A:D "*.*"') Do (
set "F=%%A"
echo !F:.=-!>>Folders.lstx
)
For /F "tokens=1* delims= " %%A in ('Type "Folders.lstx"') Do (
:: Folder name here has "-" instead of "."
set "Folder=%%A %%B"
set "str=%%A"
For /F "delims=" %%a in ('FINDstr /I /C:"!str!" "Files.lstx"') Do (
:: here we replace the "-" with the "." when checking or moving to it
IF not exist "!Folder:-=.!" MD "!Folder:-=.!"
Move "%%a" ".\!Folder:-=.!\"
)
)
Del /F /Q "*.lstx" 2>nul
pause