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