Batch copy folders to variable destination based on second underscore delimited part in folder name
<p>First of all, I don't have really much experience writing batch scripts. With help from this and other sites I wrote some scripts copying files from several PCs which are not connected to the network to a USB drive. Now I want to copy these backup-folder to a NAS drive. The backup folders are all named like:</p>

<pre><code>backup_PC1_data1_dd-mm-yyyy
backup_PC1_data2_dd-mm-yyyy
backup_PC2_data1_dd-mm-yyyy
backup_PC2_data2_dd-mm-yyyy
...
</code></pre>

<p>The NAS drive <code>G:\</code> contains the folders:</p>

<pre><code> ABC_PC1
 ABC_PC2
 ...
</code></pre>

<p>Now I want to copy the folders from the USB drive to the NAS drive according to the folder names.</p>

<p>For example: <code>Backup_PC1_data1_tt-mm-yyyy</code> and <code>Backup_PC1_data2_tt-mm-yyyy</code> should be copied to the folder <code>ABC_PC1</code> on the NAS drive, <code>Backup_PC2_data1_tt-mm-yyyy</code> and <code>Backup_PC2_data2_tt-mm-yyyy</code> should be copied to the folder <code>ABC_PC2</code> on the NAS drive.</p>

<p>Since the USB to NAS copy process should work on different PCs and some PCs have more than one HDD I want to start the <code>copy2NAS</code> script from the USB drive. I started with getting the drive letter and set the source drive with:</p>

<pre><code>set drive=%~d0
set source=%drive%
</code></pre>

<p>My plan was to get all folders in source named <code>Backup_*</code> with:</p>

<pre><code>for /d %%i in ("%source%\Backup_*")
</code></pre>

<p>and then something like splitting the name of the source folder and using the part between <code>Backup_</code> and <code>_data…</code> to define the destination folder (always <code>G:\ABC_XXX</code>) where <code>XXX</code> is the part between <code>Backup_</code> and <code>_data*</code> of the source folder name.</p>

<p>Writing a code like:</p>

<ul>
<li>For all folders on the USB drive check if name is <code>Backup_*</code>.</li>
<li>If yes split name and extract the part between <code>Backup_</code> and <code>_data*</code>.</li>
<li>Use the extracted part of the source folder name to specify the destination folder.</li>
<li>Copy source folder (and the files in it) to destination.</li>
</ul>

<p>I have coded so far:</p>

<pre><code>set drive=%~d0
set source=%drive%
for /d %%i in (%source%\Backup_*) do call :ProcessDir "%%i"

:ProcessDir
set "foldername=%~n1"
echo %foldername%
for /F "tokens=2 delims=_" %%a in ("%foldername%") do set nthstring=%%a
set "Source=%source%\%foldername%"
set "destination=C:\mol\backups\backup_nas\GMH_%nthstring%\%foldername%"
robocopy %Source% %destination% /s
</code></pre>

<p>But it looks like this is beyond my batch scripting skills. I either ended up with no copy process, or just the files to a newly created destination, or ...</p>

<p>I almost forgot to mention that the <code>Backup_*</code> folders are not the only folders on the USB drive.</p>

<p>Has anybody an idea how to realize this task?</p>