windows 批处理文件,文件夹路径中的通配符,不知道通配符可以是什么

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25794369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 18:42:50  来源:igfitidea点击:

Batch file, wildcard in folderpath, no idea what the wildcard can be

windowsbatch-filecmdwildcard

提问by Glowie

I have a batch file that attempts to replaces a file in

我有一个批处理文件试图替换一个文件

C:\Users\All Users\Symantec\Symantec Endpoint Protection.*\Data\Config

But the * varies from computer to computer, so it's not as if I can have an input list

但是 * 因计算机而异,所以我不能有一个输入列表

This batch file is executed in another batch file, which is called from command-line.

该批处理文件在另一个批处理文件中执行,该文件从命令行调用。

Here is the batch file with the * path, C:\script.bat

这是带有 * 路径的批处理文件,C:\script.bat

@echo off
if exist "C:\Users\All Users\Symantec\Symantec Endpoint Protection.*\Data\Config\SyLink.xm_" del /Q "C:\Users\All Users\Symantec\Symantec Endpoint Protection.*\Data\Config\SyLink.xm_"
ren "C:\Users\All Users\Symantec\Symantec Endpoint Protection.*\Data\Config\SyLink.xml" sylink.xm_bak
del /Q "C:\Users\All Users\Symantec\Symantec Endpoint Protection.*\Data\Config\SyLink.xml"

echo.
echo Copying new SyLink.xml
echo.
copy C:\SyLink.xml "C:\Users\All Users\Symantec\Symantec Endpoint Protection.*\Data\Config"


echo.
echo Deleting temp files
del /Q "c:\SyLink.xml"

And this is another batch file, C:\copy.batthat calls the first one, i.e. C:\script.bat

这是另一个批处理文件,C:\copy.bat调用第一个,即 C:\script.bat

xcopy C:\sylink.xml \10.10.10.10\c$
xcopy C:\sylink.xml \10.10.10.11\c$
D:\pstools\psexec.exe @C:\clients.txt -c C:\Script.bat

C:\clients.txt

C:\clients.txt

10.10.10.10
10.10.10.11

Batch file is executed through the command-line

批处理文件通过命令行执行

C:\> C:\copy.bat

Question is, how do I make this batch file work so it recognizes * as a wildcard?

问题是,如何使这个批处理文件工作,以便将 * 识别为通配符?

Thanks

谢谢

采纳答案by MC ND

for /d %%a in (
  "C:\Users\All Users\Symantec\Symantec Endpoint Protection.*"
) do set "theFolder=%%~fa\Data\Config"

echo %theFolder%

You can only include a wildcard in the last element of the path.

您只能在路径的最后一个元素中包含通配符。

To do what you need, you have to enumerate the 12.*folders and select one. In this case the last folder matching the wildcard is selected (as the code in the doclause is executed, the variable is overwritten)

要执行您需要的操作,您必须枚举12.*文件夹并选择一个。在这种情况下,选择与通配符匹配的最后一个文件夹(当do执行子句中的代码时,变量被覆盖)

回答by Honguito98

Try this:

尝试这个:

@echo off

For /D %%D in ("C:\Users\All Users\Symantec\Symantec Endpoint Protection.*") Do (
    Set "Dir=%%~fD\Data\Config"
)

If exist "%Dir%\SyLink.xm_" Del /q %Dir%\SyLink.xm_"
Ren "%Dir%\SyLink.xml" sylink.xm_bak
Del /q "%Dir%\SyLink.xml"

echo.
echo Copying new SyLink.xml
echo.
copy C:\SyLink.xml "%Dir%"


echo.
echo Deleting temp files
Del /q "c:\SyLink.xml"

/Dswitch find for folders.

/D切换查找文件夹。

%%~fDgets full path from folder.

%%~fD从文件夹获取完整路径。