Search code examples
batch-filewildcardxcopy

Batch file that creates folder with wildcard in path


I want to write a batch file that creates a folder (if it does not exist) and copies a certain file into that folder. So far so good.

The problem is that one folder in the path varies slightly from time to time, so a wildcard becomes necessary.

The following code works just fine but obviously misses to create the folder (Reports). So if the folder is not there, it simply does nothing.

for /r "c:\Users\%USERNAME%\AppData\Local\Packages" &&G in ("LocalState\acn\Reports") do @if exist %%G xcopy /s /i /y c:\temp\Reporting "%%G"

The full path is: c:\Users\FSchneider\AppData\Local\Packages\“WILDCARD"\LocalState\acn\Reports\

Any idea?


Solution

    • Add /d switch in for to indicate you're looking for a directory, not a file
    • Add * and omit quotes in the wildcard to indicate it's actually a wildcard
    • No need for if exist now

      for /d /r "%LocalAppData%\Packages" %%G in (LocalState\acn.*) do xcopy /s /i /y c:\temp\Reporting "%%G\Reports"