Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 从批处理文件中的给定文件夹中获取一个扩展名为*.nupkg的文件名_Windows_Batch File - Fatal编程技术网

Windows 从批处理文件中的给定文件夹中获取一个扩展名为*.nupkg的文件名

Windows 从批处理文件中的给定文件夹中获取一个扩展名为*.nupkg的文件名,windows,batch-file,Windows,Batch File,我是windows批处理编程的新手。下面是我的代码 @echo off setlocal set sourcedir=C:\Projects\Libraries set pdbdir=C:\Projects\App\bin\Debug\net461 set dlldir=C:\Users\radiraju\.nuget\packages @echo Cleaning up the solution. Please wait... FOR /d %%F IN (%sourcedir%\*) D

我是windows批处理编程的新手。下面是我的代码

@echo off
setlocal
set sourcedir=C:\Projects\Libraries
set pdbdir=C:\Projects\App\bin\Debug\net461
set dlldir=C:\Users\radiraju\.nuget\packages

@echo Cleaning up the solution. Please wait...

FOR /d %%F IN (%sourcedir%\*) DO (del /q /s %%F\bin\debug\ 1>nul)

@echo Cleaning up completed. Rebuild Started.

FOR /f %%F IN ('dir /ad /b %sourcedir%\*') DO (

    if not "%%F"==".vs" (
        cd %sourcedir%\%%F
        dotnet pack
        xcopy %sourcedir%\%%F\bin\debug\net461\%%F.pdb %pdbdir% /Y
        del /q %sourcedir%\%%F\bin\debug\*.symbols.nupkg 1>nul
        FOR /F "tokens=*" %%I IN ('dir /b /a-d %sourcedir%\%%F\bin\debug') DO (
        set _ver=%%I)
        xcopy %sourcedir%\%%F\bin\debug\net461\%%F.dll %dlldir%\%%F\%_ver%\lib\net461 /Y
    )
)
在上一条语句中,我得到
%\u var%
为空。不知道为什么

    FOR /F "tokens=*" %%I IN ('dir /b /a-d %sourcedir%\%%F\bin\debug') DO (
    set _ver=%%I)
\u ver
(而不是
\u var
)设置为在目录中找到的最后一个文件名注意:NTFS卷的默认顺序是名称顺序。未定义FAT卷中的订单

    xcopy %sourcedir%\%%F\bin\debug\net461\%%F.dll %dlldir%\%%F\%_ver%\lib\net461 /Y
您似乎想使用上一条逻辑语句中设置的
\ver
值。请阅读有关延迟扩展的信息。第一次解析块时,块中任何
%var%
的值都将替换为该变量的值,而不是该变量的运行时值

要使用变量的运行时值,需要调用
delayedexpansion
,然后使用
!瓦尔
代替
%var%
,或者您可以在单独的子例程中调用命令,或者您可以使用动态标志:

    del /q %sourcedir%\%%F\bin\debug\*.symbols.nupkg 1>nul
    set "flag=Y"
    FOR /F "tokens=*" %%I IN ('dir /b /a-d /O-N %sourcedir%\%%F\bin\debug') DO if defined flag (
     set "flag="
     xcopy %sourcedir%\%%F\bin\debug\net461\%%F.dll %dlldir%\%%F\%%I\lib\net461 /Y
    )
此处注意:
标志
设置为某个值(任何值,只要已设置),则%%I的
将使用
dir
列表以相反的名称顺序调用
(/O-N)
,以便首先显示所需的元素(/O-D将产生相反的日期顺序,如果这更合适)。当%%I
返回第一个元素时,清除
标志
,并使用版本调用
xcopy
?在
%%I
中。由于清除了
标志
,因此
如果已定义
将阻止
xcopy
对进一步的目录条目进行复制。(
如果已定义,则对变量的运行时值进行操作)


还请注意,您可以使用
/i
开关修改
if
,使其在需要时不区分大小写。

在上一个语句集之前_var=%\u var:.nupkg=%&set _var=%\u var:%%F.=%应添加以查找版本号,并且该版本将用于上一个语句中,而不是用于建议。在我的例子中,文件夹%sourcedir%\%%F\bin\debug\将只有一个扩展名为“.nupkg”的文件(例如LibraryName.1.1.0.nupkg)Setlocal EnableDelayedExpansion for/F“tokens=*”%%I in('dir/b/a-d%sourcedir%\%%F\bin\debug\*.nupkg')DO(set\ver=%I)set\ver=_版本:.nupkg=!设置_ver=_版本:LibraryName.=!xcopy%sourcedir%%\%F\bin\debug\net461\%F.dll%dlldir%%\%F\_弗尔\lib\net461/Y