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 如何处理FOR循环中的文件/文件夹名称列表,该循环中还可以包含感叹号?_Windows_Batch File_Cmd - Fatal编程技术网

Windows 如何处理FOR循环中的文件/文件夹名称列表,该循环中还可以包含感叹号?

Windows 如何处理FOR循环中的文件/文件夹名称列表,该循环中还可以包含感叹号?,windows,batch-file,cmd,Windows,Batch File,Cmd,我编写批处理脚本来查找文件夹中文件的内容。内容位于文本文件中,并具有感叹号等特殊字符 如何获取包含感叹号的FILENAME和FOLDERNAME @ECHO off SETLOCAL EnableDelayedExpansion set /p SRC="Enter source folder link: " set /p DST="Enter destination folder link: " FOR /F "delims=" %%a IN ('DIR /b /s /a-d "%SRC%"

我编写批处理脚本来查找文件夹中文件的内容。内容位于文本文件中,并具有感叹号等特殊字符

如何获取包含感叹号的
FILENAME
FOLDERNAME

@ECHO off
SETLOCAL EnableDelayedExpansion

set /p SRC="Enter source folder link: "
set /p DST="Enter destination folder link: "

FOR /F "delims=" %%a IN ('DIR /b /s /a-d "%SRC%"') do (
    Set "CODE=%%~na"
    Set "EXT=%%~xa"
    findstr /c:"!CODE!" "%SRC%\Content.txt">nul
    IF "!errorlevel!" EQU "0" (
        for /F "tokens=2,3" %%c in ('findstr /c:"!CODE!" "%SRC%\Content.txt"') do (
            ECHO !CODE!
            Set "NEWNAME=%%c"
            Set "FOLDERNAME=%%d"
            Set "NEWNAME=!NEWNAME:_= !"
            Set "FOLDERNAME=!FOLDERNAME:_= !"
            IF not exist "%DST%\!FOLDERNAME!" md "%DST%\!FOLDERNAME!"
            mklink "%DST%\!FOLDERNAME!\!NEWNAME!!EXT!" "%%a"
        )
    )
)
Endlocal
Exit

PS:源文件夹有许多文件。

一种解决方案是使用子例程来避免使用延迟的环境变量扩展:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

:GetSource
set "SRC="
set /P SRC="Enter source folder link: "
if not defined SRC goto GetSource
set "SRC=%SRC:"=%"
if not defined SRC goto GetSource

:GetDestination
set "DST="
set /P DST="Enter destination folder link: "
if not defined DST goto GetDestination
set "DST=%DST:"=%"
if not defined DST goto GetDestination

for /F "eol=| delims=" %%I in ('dir /A-D /B /S "%SRC%" 2^>nul') do (
    if exist "%SRC%\Content.txt" for /F "tokens=2,3" %%A in ('%SystemRoot%\System32\findstr.exe /C:"%%~nI" "%SRC%\Content.txt" 2^>nul') do (
        set "NEWNAME=%%~A"
        set "FOLDERNAME=%%~B"
        call :MakeLink "%%I"
    )
)

endlocal
exit /B

:MakeLink
echo %~n1
set "NEWNAME=%NEWNAME:_= %"
set "FOLDERNAME=%FOLDERNAME:_= %"
if not exist "%DST%\%FOLDERNAME%" md "%DST%\%FOLDERNAME%"
mklink "%DST%\%FOLDERNAME%\%NEWNAME%%~x1" %1
goto :EOF

打开命令提示符窗口并运行
call/?
以获取帮助,解释如何使用具有启用的命令扩展名的命令call在同一批处理文件中像子例程一样运行块。另请参见

我没有研究您的代码,但我认为在设置变量名后启用延迟扩展更合适:

@Echo关闭
SetLocal DisableDelayedExpansion
设置/P“SRC=输入源文件夹链接:”
设置/P“DST=输入目标文件夹链接:”
对于(*)中的/D/R%%A,对于/F“代币=2-3”%%B(
'FindStr/C:“%%~nxA”“%SRC%\Content.txt”2^>Nul”)Do(Echo%%~nA
设置“新建=%%B”
设置“FLD=%%C”
SetLocal EnableDelayedExpansion
如果不存在“%DST%\!FLD:\!\”MD“%DST%\!FLD:\!”2>Nul&&(
MkLink“%DST%\!FLD:=!\!NEW:=!%%~xA”“%%A”)
(本地)
退出/B
我强烈建议您在使用用户输入执行任务之前,即在
For
循环之前,对用户输入执行一些适当的验证