Batch file for copy many files to many folders?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Batch file for copy many files to many folders?

#31 Post by abc0502 » 09 Dec 2012 07:37

As Foxidrive Indecated in his first post, any similar numbers in different files will cause to take both files and move them even if they don't match that's why i pointed at the last coditions
> Folders doesn't contain similar numbers like "C5000 English" and "5000 Italian" or even the PDF files as it just search for the numbers.


and Also as Foxidrive said in his second post, using same algorithm will be impossible, The main problem is the Unicode Names, that's what making the problem.
Also there is no pattern when adding a letter before the numbers like C,
I might try to find a way for that but for sure you will need some one has experience in VBscripting or Powershell as foxidrive said


Edited:
This link can convert between ANSI and Unicode but it seams that encoding file to "UCS-2 Little Endian" is what make the file handle the Unicode names when stored in them "when save a notepad file as Unicode" and this method encode to "UCS-2 LE w/o BOM" "you can see the encoding by notepad++"

This Link has a methods to convert to the "UCS-2 Little Endian" the encoding that can handle the Unicode names in the files but it just doesn't work with me.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Batch file for copy many files to many folders?

#32 Post by abc0502 » 09 Dec 2012 08:14

This should rename the folder and the first level of sub-folders Only:

Code: Select all

@Echo OFF

Setlocal EnableDelayedExpansion
For /F "delims=" %%A in ('DIR /B /A:D "*.*"') Do (
   set "parent_folder=%%A"
      PUSHD !parent_folder!
         For /F "delims=" %%B in ('DIR /B /A:D "*.*"') Do (
            set "sub_folder=%%B"
            Ren "!sub_folder!" "!parent_folder! !sub_folder!" )
      POPD
   )
   
pause

I tested on this
main 1
|___1
|___2
|___3

عربي
|___1
|___2
|___3

and it worked for both

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Batch file for copy many files to many folders?

#33 Post by abc0502 » 09 Dec 2012 10:02

I modified the batch, and it should handle the files nams.
It also assumes that the numbers in the last one "00-00-XXX.pdf" or last two token "00-00-XXX-Y.pdf" is always the one that is pointing to the folder names.
Also it will ignore the Character before the number as i don't know on what basis it is there.

Code: Select all

@echo off

:: Delete Old list Files
del Files.list 2>nul
del Folders.list 2>nul

:: Get Current Dir
For %%A in (*) Do Set "tmpDIR=%%~dpA"
Set "CurrDir=%tmpDIR:~0,-1%"

:: Get a List of All Files
Setlocal EnableDelayedExpansion
For /F "delims=" %%A in ('DIR /B /A:-D "*.pdf"') Do Echo %%A>>Files.list

:: Get a List of All Folders [ and replace "." with "-" ]
For /F "delims=" %%A in ('DIR /B /A:D "*.*"') Do (
   set "N=%%A"
   echo !N:.=-!>>Folders.list
   )

:: Extract Each Folder number "1st token" and search Files.list for a match PDF name that contain same number
For /F "tokens=1 delims= " %%A in ('Type "Folders.list"') Do (
   set "str=%%A"
   For /F "delims=" %%a in ('FINDstr /I /C:"!str!" "Files.list"') Do (
      For /F "tokens=1-4 delims=-" %%b in ("%%a") Do (
         set "tok1=%%b"
         set "tok2=%%c"
         set "tok3=%%d"
         set "tok4=%%e"
            :: Case 1 : folder name is like "XXX name"
            IF "!str!.pdf" EQU "!tok1!" CALL :Process "!str:-=.!" "%CurrDir%\%%a"
            IF "!str!.pdf" EQU "!tok2!" CALL :Process "!str:-=.!" "%CurrDir%\%%a"
            IF "!str!.pdf" EQU "!tok3!" CALL :Process "!str:-=.!" "%CurrDir%\%%a"
            IF "!str!.pdf" EQU "!tok4!" CALL :Process "!str:-=.!" "%CurrDir%\%%a"
            :: Case 2 : folder name is like "XXX.y name"
            IF "!str!.pdf" EQU "!tok1!-!tok2!" CALL :Process "!str:-=.!" "%CurrDir%\%%a"
            IF "!str!.pdf" EQU "!tok2!-!tok3!" CALL :Process "!str:-=.!" "%CurrDir%\%%a"
            IF "!str!.pdf" EQU "!tok3!-!tok4!" CALL :Process "!str:-=.!" "%CurrDir%\%%a"
            :: Case 3 : folder name is like "ZXXX name"
            IF "!str!.pdf" EQU "!tok1:~1!" CALL :Process "!str:-=.!" "%CurrDir%\%%a"
            IF "!str!.pdf" EQU "!tok2:~1!" CALL :Process "!str:-=.!" "%CurrDir%\%%a"
            IF "!str!.pdf" EQU "!tok3:~1!" CALL :Process "!str:-=.!" "%CurrDir%\%%a"
            IF "!str!.pdf" EQU "!tok4:~1!" CALL :Process "!str:-=.!" "%CurrDir%\%%a"
            :: Case 4 : folder name is like ZXXX.y name"
            IF "!str!.pdf" EQU "!tok1:~1!-!tok2!" CALL :Process "!str:-=.!" "%CurrDir%\%%a"
            IF "!str!.pdf" EQU "!tok2:~1!-!tok3!" CALL :Process "!str:-=.!" "%CurrDir%\%%a"
            IF "!str!.pdf" EQU "!tok3:~1!-!tok4!" CALL :Process "!str:-=.!" "%CurrDir%\%%a"            
         )
      )
   )

Del /F /Q "*.list" "*.vbs" >nul
pause
exit /b

:process
call :vb "%~1" "%~2"
"Move.vbs"
Goto :EOF

:vb [ <!str!> <full_file_path> ]
:: This is a work around as Batch can't pass the Unicode name to the vbs file so we let the vbs handle it
:: <!str!> is the number we use to search for the pdf in File.lstx but we change the - to .
:: <full_file_path> is the full path to the pdf file we found matched the folder number
(
Echo Option Explicit
Echo Dim strFolderToSearch, objFSO, objRootFolder
Echo Dim colSubfolders, File, num, Destination
Echo Dim FolderName, objFolder, SFolderName
Echo num = "%~1"
Echo File= "%~2"
Echo strFolderToSearch = left^(WScript.ScriptFullName,^(Len^(WScript.ScriptFullName^)^)-^(len^(WScript.ScriptName^)^)^)
Echo Set objFSO = CreateObject^("Scripting.FileSystemObject"^)
Echo Set objRootFolder = objFSO.GetFolder^(strFolderToSearch^)
Echo Set colSubfolders = objRootFolder.SubFolders
Echo For Each objFolder in colSubfolders
Echo    FolderName = objFolder.Name
Echo    SFolderName = Split^(FolderName^)
Echo       If SFolderName^(0^) = num Then
Echo          Destination = objRootFolder ^& "\" ^& FolderName ^& "\"
Echo          objFso.MoveFile File , Destination
Echo          'WScript.Echo Destination
Echo       End If
Echo    Next
)>Move.vbs
Goto :EOF
Last edited by abc0502 on 09 Dec 2012 22:11, edited 1 time in total.

taherkhani
Posts: 24
Joined: 07 Dec 2012 04:33

Re: Batch file for copy many files to many folders?

#34 Post by taherkhani » 09 Dec 2012 13:07

@ abc0502
Your new code, don't work for these folders:

470.7 تاريخ هنر جهان
471.6 الياف نساجي
488 الگو
488.1 دوخت
491.3 الگو و دوخت لباس كودك
491.5 رنگ شناسي تخصصي لباس
494.8 طراحي اندام و لباس

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Batch file for copy many files to many folders?

#35 Post by abc0502 » 09 Dec 2012 22:01

OK, I Modified the code, and added two more cases where the number might start with a letter as in "C6014" and "C400" ..etc.
I forgot to make it ignore that 1st letter

taherkhani
Posts: 24
Joined: 07 Dec 2012 04:33

Re: Batch file for copy many files to many folders?

#36 Post by taherkhani » 09 Dec 2012 23:42

Very very thankful @abc0502, you absolutely save my time by your code.

@abc0502, can you introduce a book, lecture, or anything that i follow that and rise my ability in write scripting program like you?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch file for copy many files to many folders?

#37 Post by foxidrive » 10 Dec 2012 00:11

abc0502 wrote:OK, I Modified the code, and added two more cases where the number might start with a letter as in "C6014" and "C400" ..etc.


I didn't follow too closely - I'm very tired - but should this

IF "!str!.pdf" EQU
be this?
IF "!str!" EQU

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Batch file for copy many files to many folders?

#38 Post by abc0502 » 10 Dec 2012 00:22

@Foxidrive, I was making that too, but i forgot that the extension will be included in the token, so if the number is 6000 which is the !str! it will be 6000.pdf in the token, so instead of excluding the .pdf from the token i just add them in the !str!

My start with batch was with a small exe program that emulate the commands use, I tried to search for that program again but no luck and i don't even remember the name.
So if you are going to search for a tutorial i think the most useful tutorial will be the interactive one. here is an interactive courses but for other languages, you should search for something like that website.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch file for copy many files to many folders?

#39 Post by foxidrive » 10 Dec 2012 00:37

abc0502 wrote:@Foxidrive, I was making that too, but i forgot that the extension will be included in the token, so if the number is 6000 it will be 6000.pdf in the token, so instead of excluding the .pdf i just add them in the !str!


Ok. I thought I saw a case like this: 111-222-C5000-999.PDF
where the numeral needed was the 5000
I could be mistaken - and the OP says your solution works well.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Batch file for copy many files to many folders?

#40 Post by abc0502 » 10 Dec 2012 00:43

In fact if it is the case, it will be impossible to make an automated batch for that task.
and may be he didn't test the code yet. but as for my tests it worked for me tell now as long as the pdf names is not longer than 4 tokens

Post Reply