Batch file that creates folder with wildcard in path

十年热恋 提交于 2019-12-12 06:19:02

问题


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?


回答1:


  • 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"
    



回答2:


Next script could help.

@ECHO OFF
SETLOCAL enableextensions

set "_fldrtop=%USERPROFILE%\AppData\Local\Packages"

set "_fldrsub=LocalState\acn"
if not "%~1"=="" set "_fldrsub=%~1"     :: my testing data, remove this line

set "_fldrlow=Reports"
if not "%~2"=="" set "_fldrlow=%~2"     :: my testing data, remove this line

for /F "delims=" %%G in ('dir /B /AD "%_fldrtop%"') do (
  if exist "%_fldrtop%\%%G\%_fldrsub%\" (
    if exist "%_fldrtop%\%%G\%_fldrsub%\%_fldrlow%\" (
      echo echo "%_fldrtop%\%%G\%_fldrsub%\%_fldrlow%\"
    ) else (
      echo md "%_fldrtop%\%%G\%_fldrsub%\%_fldrlow%\"
    )
    rem echo xcopy /s /i /y c:\temp\Reporting "%_fldrtop%\%%G\%_fldrsub%\%_fldrlow%\"
  )
)

Output:

==>D:\bat\SO\31672436.bat

==>D:\bat\SO\31672436.bat "LocalState\Cache"
md "C:\Users\UName\AppData\Local\Packages\winstore_cw5\LocalState\Cache\Reports\"

==>D:\bat\SO\31672436.bat "LocalState\Cache" 2
echo "C:\Users\UName\AppData\Local\Packages\winstore_cw5\LocalState\Cache\2\"


来源:https://stackoverflow.com/questions/31672436/batch-file-that-creates-folder-with-wildcard-in-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!