gpt4 book ai didi

batch-file - 批处理 - EnableDelayedExpansion、感叹号和文件/文件夹名称

转载 作者:行者123 更新时间:2023-12-03 09:32:45 25 4
gpt4 key购买 nike

我正在为文件和文件夹名称上的感叹号 ( ! ) 苦苦挣扎。
我创建的批处理读取 .txt 文件的内容并将列出的文件从文件夹排序到子文件夹中。
例如:source.txt里面有:file_1.zipfile_2.zip名为 system 的文件夹有 file_1.zip , file_2.zip , file_3.zip , ETC。
并且批处理读取 source.txt并将两个文件复制到 system/source .

我试图避免启用 DelayedExpansion,但在添加文件夹浏览器后,我想不出比使用它更好的方法。我需要启用它以显示 :listSources :listFolders
我现在的问题不是处理太多 (!) 在文件夹名称上(虽然允许它会很棒),但允许复制具有 的文件(!) 在它的名字上。

我想过逃跑^^! setlocal 和 endlocal 之间的字符或切换,但我仍在学习如何正确使用它,因此不知道如何处理它。也许有更好的方法? (我不是程序员!)

@echo off
setlocal EnableDelayedExpansion

rem Copy sorted files into destination folder? [Y/N]
set "copyMode=y"
if /i "%copyMode%"=="y" (set appendTitle=[Copy Mode]) else set appendTitle=[Log Mode]

rem Set prefix for output file (used only if copyMode=n)
set prefixMiss=missing_in_

rem Set defaults to launch directory
set rootSource=%~dp0
set rootFolder=%~dp0

:listSources
title Source file selection %appendTitle%
Show folder names in current directory
echo Available sources for sorting:
for %%a in (*.txt) do (
set /a count+=1
set mapArray[!count!]=%%a
echo !count!: %%a
)

:checkSources
set type=source
if not exist !mapArray[1]! cls & echo No source file (.txt) found on current directory^^! & goto SUB_folderBrowser
if !count! gtr 1 set /a browser=!count!+1 & echo.!browser!: Open Folder Browser? & goto whichSource
if !count! equ 1 echo. & set /p "oneSource=Use !mapArray[1]! as source? [Y/N]: "
if /i "%oneSource%"=="y" (set "sourceFile=1" & echo. & goto listFolders) else goto SUB_folderBrowser

:whichSource
echo.
rem Select source file (.txt)
echo Which one is the source file you want to use? Choose !browser! to change directory.
set /p "sourceFile=#: "
if %sourceFile% equ !browser! goto SUB_folderBrowser
if exist !mapArray[%sourceFile%]! echo. & goto listFolders
echo Incorrect input^^! & goto whichSource

:listFolders
title System folder selection %appendTitle%
rem Show folder names in current directory
cd %~dp0
set count=0
echo Available folders for sorting:
for /d %%a in (*) do (
set /a count+=1
set mapArray2[!count!]=%%a
echo !count!: %%a
)

:checkFolders
set type=root
if not exist !mapArray2[1]! cls & echo No folders found on current directory^^! & goto SUB_folderBrowser
if !count! gtr 1 set /a browser=!count!+1 & echo.!browser!: Open Folder Browser? & goto whichSystem
if !count! equ 1 echo. & set /p "oneFolder=Use !mapArray2[1]! as target? [Y/N]: "
if /i "%oneFolder%"=="y" (set "whichSystem=1" & goto whichFolder) else goto SUB_folderBrowser

:whichSystem
echo.
rem Select system folder
echo Which system do you want to sort? Choose !browser! to change directory.
set /p "whichSystem=#: "
if %whichSystem% equ !browser! goto SUB_folderBrowser
if exist !mapArray2[%whichSystem%]! echo. & goto createFolder
echo Incorrect input^^! & goto whichSystem

:createFolder
rem Create destination folder
if /i not "%copyMode%"=="y" goto sortFiles
set destFolder=!mapArray[%sourceFile%]:~0,-4!
if not exist ".\!mapArray2[%whichSystem%]!\%destFolder%" md ".\!mapArray2[%whichSystem%]!\%destFolder%"

:sortFiles
rem Look inside the source file and copy the files to the destination folder
title Sorting files in progress... %appendTitle%
for /f "delims=" %%a in ('type "%rootSource%\!mapArray[%sourceFile%]!"') do (
if exist ".\!mapArray2[%whichSystem%]!\%%a" (
if /i "%copyMode%"=="y" copy ".\!mapArray2[%whichSystem%]!\%%a" ".\!mapArray2[%whichSystem%]!\%destFolder%\%%~nxa" >nul
echo %%a
) else (
echo.
echo %%a missing & echo.
if /i not "%copyMode%"=="y" echo %%a >> "%~dp0%prefixMiss%!mapArray2[%whichSystem%]!.txt"
)
)

title Sorting files finished^^! %appendTitle%

popd & pause >nul & exit

:SUB_folderBrowser
if !count! lss 1 set /p "openBrowser=Open Folder Browser? [Y/N]: "
if !count! lss 1 (
if not "%openBrowser%"=="y" exit
)
set count=0 & echo Opening Folder Browser... & echo.
if "%type%"=="root" echo Select the root system folder, not the system itself^^!

rem PowerShell-Subroutine to open a Folder Browser
set "psCommand="(new-object -COM 'Shell.Application')^
.BrowseForFolder(0,'Please choose a %type% folder.',0,0).self.path""
for /f "usebackq delims=" %%i in (`powershell %psCommand%`) do set "newRoot=%%i"

if "%type%"=="source" set rootSource=%newRoot%
if "%type%"=="root" set rootFolder=%newRoot%

rem Change working directory
if "%type%"=="source" cls & pushd %rootSource% & goto listSources
if "%type%"=="root" cls & pushd %rootFolder% & echo Selected source file: !mapArray[%sourceFile%]! & echo. & goto listFolders

任何帮助将不胜感激!

最佳答案

只有当 FOR 变量包含 ! 时,延迟扩展才会在 FOR 循环中引起问题。 .

如果从循环内调用 :subroutine,通常可以避免在 FOR 循环内延迟扩展。每次迭代都会重新解析子例程,因此您可以正常使用 %var%变量扩展。但是 CALL 会大大减慢速度,所以我喜欢避免它。

有一个简单的解决方案可以避免延迟扩展并避免调用 - 使用 DIR/B 和 FINDSTR/N 将行号注入(inject)目录输出,FOR/F 可以轻松解析。

例如,这是 ListSources 循环的解决方案:

for /f "delims=: tokens=1*" %%A in ('dir /b /a-d *.txt^|findstr /n "^"') do (
set "mapArray[%%A]=%%B"
set "count=%%A"
echo %%A: %%B
)

您可以轻松地将相同的技术应用于您的第二个循环。

您仍然需要在其他非循环代码部分中延迟扩展以扩展数组值。所以从禁用延迟扩展开始,然后在 :whichSource 中启用它,在第二个循环之前再次禁用它,然后在最后一个循环之后再启用一次。请注意,您不能使用 ENDLOCAL,否则您将丢失先前定义的数组值。因此,当您在禁用和启用延迟扩展之间切换时,您将创建一小堆 SETLOCAL。

关于batch-file - 批处理 - EnableDelayedExpansion、感叹号和文件/文件夹名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41386156/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com