3

First of all, there are similar questions on Stack OverFlow like:

However, my use-case is a bit specific (or let's say: I couldn't manage to solve my problem using the lessons learned from the previous forum entries - mind that I am a pure beginner with Batch files).

What I want to do is to grab a file from a certain path, which includes a few subfolders (which change their names) - and copy it to another path which has similar folder structure.

I am currently stuck at the point, that I don't know how to set multiple wildcards in the source path, as it consists of a few things changing. Example:

File in Source:

C:\20170621_Update2017SR1\Polarion_update\_backup-20170627-1602.05\polarion\plugins\com.polarion.alm.tracker_3.17.0\configuration\MyPolarion\page.xml

Target Directory:

C:\Polarion\polarion\plugins\com.polarion.alm.tracker_3.18.2\configuration\My Polarion

Basically only the parts with numbers can change, so I was trying the following:

for /D %%a in ("C:\Polarion\polarion\plugins\com.polarion.alm.tracker*") do set "com.polarion.alm.tracker=%%a"
for /D %%b in ("C:\*_Update*\Polarion_update\_backup-*\polarion\plugins\com.polarion.alm.tracker*") do set "folder=%%b"

echo %com.polarion.alm.tracker% 
echo %folder%

set source="%folder%\configuration\MyPolarion\page.xml"
set destination="%com.polarion.alm.tracker%\configuration\My Polarion"
xcopy /s /Y %source% %destination%

I am pretty sure line 2 of my Code contains mistakes - because I don't know if I can set multiple wildcards like this.

The console gives me for line 2:

Echo is on

I don't understand what it means and what should I do.

double-beep
  • 5,031
  • 17
  • 33
  • 41
SteffPoint
  • 479
  • 2
  • 9
  • 24
  • 3
    `Echo is on` means that your batch file is trying to perform `echo` without anything after it. So that means that `%com.polarion.alm.tracker%` is empty. – Pieter Mantel Dec 13 '18 at 12:59
  • Thank you very much Pieter! – SteffPoint Dec 13 '18 at 13:00
  • 3
    You can't use wildcards in the folder path. – Squashman Dec 13 '18 at 13:34
  • Hmmm, well line 1 and 4 kinda works for me. – SteffPoint Dec 13 '18 at 13:38
  • 4
    Wildcards can only be used in the very last element of a path (I guess that's what @Squashman was actually referring to); so `D:\some_dir\*_name` works, but `D:\some_*\dir_name` doesn't. So every directory level with wildcards need to be resolved individually: `for /D %J in ("D:\some_*") do for /D %I in ("%~J\*_name") do echo Resolved two directory levels: "%~I"` (double the `%`-signs in a batch file) – aschipfl Dec 13 '18 at 14:08

1 Answers1

6

As I already mentioned in a comment, wildcards can only be used in the very last element of a path (independent on whether this is a file or directory). That is why your command line containing C:\*_Update*\Polarion_update\... fails. However, you can resolve every directory level with wildcards individually, like this:

set "folder="
for /D %%b in ("C:\*_Update*") do (
    for /D %%c in ("%%~b\Polarion_update\_backup-*") do (
        for /D %%d in ("%%~c\polarion\plugins\com.polarion.alm.tracker*") do (
            set "folder=%%~d"
        )
    )
)
echo "%folder%"

If there are more than one matching directories on any levels, replace set "folder=%%~d" by echo "%%~d" to view them all.

aschipfl
  • 33,626
  • 12
  • 54
  • 99