I am trying to write a bat script in order to better organize my clients' files.
I have created two folders.
The first one is named "folders" and contains the folders for each client with their files. Every client's folder name has the the structure name_surname_uniqueclientid.
The second one is named "Raw" and contains raw client files and folders but each folder or file contains inside its name the unique client id.
My goal is to extract every id from each "folders" folder and check if there are any files or folders inside the "Raw" folder that contain this id. If there are it should xcopy them inside the corresponding "folders" folder with the same id.
My problem is i cannot check for folders that contain the id and move them to the corresponding "folders" folder.
I have managed to extract the id from the folder's name, check for files inside "Raw" folder containing this id and xcopy them to the corresponding "folders" folder. As far as i can tell the script doesn't enter the last if. It should find the folders containing the id and if there are xcopy them to the corresponding "folders" folder.
@echo off
setlocal enabledelayedexpansion
for /d %%a in ("folders\*") do (
for /f "tokens=3 delims=_" %%t in ("%%~nxa") do (
if exist "Raw\*%%t*" (
xcopy "Raw\*%%t*" "%%a"
)
This part bellow doesn't work:
if exist Raw\*%%t*\ (
xcopy "Raw\*%%t*\*" "%%a" /s /i
echo ok
)
)
)
pause
goto :eof
Initial Folder Tree
C:\DATA
├───folders
│ ├───FirstName_Lastname_10123
│ │ |
│ │ │ kjhda10123.rtf
│ │ │ dadsada10123lhlhfds.txt
│ │ │
│ │ └───kjhfdsfs10123f
│ ├───FirstName_Lastname_10124
│ │ │ jgkjgjfs10124kjlda.rtf
│ │ │ klhlidkas10124klhdas.txt
│ │ │
│ │ └───lkhjlkhdsakda10124
│ └───FirstName_Lastname_10125
│ │ kjhkdsa10125.rtf
│ │ 10125dakjh.txt
│ │
│ └───10125
| | kjhkjda.txt
| | hkda.pdf
└───Raw
| dsakhkhda10123.txt
| kgjddjasg10125.pdf
| kkkkdajh10123khda.docx
| 10124dsadas
|
└───vcb10125
After
C:\DATA
├───folders
│ ├───FirstName_Lastname_10123
│ │ | kkkkdajh10123khda.docx
│ │ │ kjhda10123.rtf
│ │ │ dadsada10123lhlhfds.txt
| | | dsakhkhda10123.txt
│ │ │
│ │ └───kjhfdsfs10123f
│ ├───FirstName_Lastname_10124
│ │ │ jgkjgjfs10124kjlda.rtf
│ │ │ klhlidkas10124klhdas.txt
│ │ │ 10124dsadas
│ │ └───lkhjlkhdsakda10124
│ └───FirstName_Lastname_10125
│ │ kjhkdsa10125.rtf
│ │ 10125dakjh.txt
│ │ kgjddjasg10125.pdf
| | vcb10125
│ └───10125
| | kjhkjda.txt
| | hkda.pdf
└───Raw
|
|
|
|
|
└───
If a folder already exists merge
Question? (I know this will get downvoted but I want to help the OP and try and clearly understand the issue)
So, trying to make this clear, you have two main directories called
folders
& Raw
correct?
Inside the folders
contains each clients folder named in your
name_surname_uniqueclientid
format correct?
Inside Raw
you have "raw client files and folders" BUT each with the name format of name_surname_uniqueclientid
correct?
Guessing the actuall name of the folder is after the _uniqueclientid
in example _10123_taxing.rtf
...?
Folder Tree?
C:\DATA
├───folders
│ ├───FirstName_Lastname_10123
│ ├───FirstName_Lastname_10124
│ └───FirstName_Lastname_10125
└───Raw
│ FirstName_Lastname_10123_taxing.rtf
│ FirstName_Lastname_10123_text.txt
│ FirstName_Lastname_10124_data.rtf
│ FirstName_Lastname_10124_text.txt
│ FirstName_Lastname_10125.rtf
│ FirstName_Lastname_10125_text.txt
│
├───FirstName_Lastname_10123_taxing
├───FirstName_Lastname_10124_numbers
└───FirstName_Lastname_10125_names
Goal?
So if any files inside the Raw
folder contain uniqueclientid
in the name you want to copy them to the name_surname_uniqueclientid
contained inside the folders
..?
New Tree ?
C:\DATA
├───folders
│ ├───FirstName_Lastname_10123
│ │ │ FirstName_Lastname_10123_taxing.rtf
│ │ │ FirstName_Lastname_10123_text.txt
│ │ │
│ │ └───FirstName_Lastname_10123_taxing
│ ├───FirstName_Lastname_10124
│ │ │ FirstName_Lastname_10124_data.rtf
│ │ │ FirstName_Lastname_10124_text.txt
│ │ │
│ │ └───FirstName_Lastname_10124_numbers
│ └───FirstName_Lastname_10125
│ │ FirstName_Lastname_10125.rtf
│ │ FirstName_Lastname_10125_text.txt
│ │
│ └───FirstName_Lastname_10125_names
└───Raw
EDIT: (Working Solution)
If this indeed was your question, then this is a working solution to your problem. Keep note that all functions, code blocks, and processes have a Rem |
that explains it step by step in the code.
So using the bases of your code, we can grab the uniqueclientid
and from there we will be setting this as a string called FolderIDNumber
. This string will be used inside a find
to only display valid folders inside the dir
loops.
With the main folders
name_surname_uniqueclientid folder being used in a loop, we are free to process the files in Raw
to move over. This is easy as we now have all the data for dir
. The reason we have two dir
loops is so one only grabs Folders and the other only grabs Files. This way we can properly COPY
them to the designated name_surname_uniqueclientid folder.
@echo off
@setlocal enabledelayedexpansion
Rem | Configure Directories
Set "FolderLocation=C:\DATA\folders"
Set "RawLocation=C:\DATA\Raw"
Rem | Get All Folders Locations In X Directory
for /d %%A in ("!FolderLocation!\*") do (
Rem | Set Raw Location
Set "FoldersLoc=%%A"
Rem | Get Each ID# From %%A
for /f "tokens=3 delims=_" %%B in ("%%A") do (
Rem | Set Folders ID#
Set "FolderIDNumber=%%B"
)
Rem | Get Each Folder W/H !FolderIDNumber!
for /f "tokens=*" %%C in ('Dir "!RawLocation!\" /B /A:D^| find "!FolderIDNumber!"') do (
Rem | Set Raw Location & Name
Set "RawFolderLoc=!RawLocation!\%%C"
Set "RawFoldersName=%%~nC"
Rem | Move Folders
robocopy "!RawFolderLoc!" "!FoldersLoc!\!RawFoldersName!" /E /NJH /NJS /NDL /NFL /NC /NS>NUL
)
Rem | Get Each File W/H !FolderIDNumber!
for /f "tokens=*" %%D in ('Dir "!RawLocation!\*.*" /B /A-D^| find "!FolderIDNumber!"') do (
Rem | Set Raw Location
Set "RawFileLoc=!RawLocation!\%%D"
Rem | Move Files
copy "!RawFileLoc!" "!FoldersLoc!">NUL
)
)
goto :EOF
For help on any of the commands do the following:
call /?
set /?
for /?
if /?
find /?