批量文件跳过FOR LOOP

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了批量文件跳过FOR LOOP相关的知识,希望对你有一定的参考价值。

更新我删除了令牌= 4并开始输出数据。它没有跳过FOR LOOP。我用令牌跳过太远了。我仍然有点困惑,为什么它作为一个批次,而不是从这批次,但现在至少我知道问题是什么。感谢所有为我调查此事的人。

我正在编写一个脚本来将数据从一台计算机复制到另一台计算机。问题是它正在跳过我从另一个FOR LOOP调用的FOR LOOP。如果您正在测试脚本,它需要两台PC和一台映射的T:驱动器到第二台计算机上的某个位置。我可以编写脚本,以便查找外部驱动器,如果这对某人更有帮助的话。

FOR /F "tokens=4 skip=1" %%a in ('REG QUERY "%_regshell%" /v "%_regdesktop%"') DO (
     SET _dt=%%a
     echo robocopy "!_dt!" "!_NetworkDrive!!_fndesktop!" !_params!
     echo attrib -h -r "!_NetworkDrive!!_fndesktop!"
    )

如果我自己批量编写上面的FOR LOOP并且只是回显%% a那么它的工作没有问题。在这里,我可以看到它确实在调用:_backup但它直接跳过FOR循环,我不知道为什么。我已经写了很多次这样的脚本,但从来没有完全忽略FOR循环。任何人都可以看看并协助吗?谢谢。

@echo off
:: Set Variables
SET _driveID=T:
SET _params=/Z /E /COPY:DT /R:1 /W:0 /XD LocalService NetworkService temp "temporary internet files" winsxs Content.IE5 cache /XF ntuser.* *.tmp /XJ /FP /NC /NS /NP /NJH
SET _regshell=HKEY_CURRENT_USERSoftwareMicrosoftWindowsCurrentVersionExplorerUser Shell Folders
SET _regdesktop=Desktop

:: Set Current Directory
pushd %SystemDrive%

:: Start Menu - Create Choices and Options.  Send to various places to perform the actions.
:_start
cls
ECHO   Please type either option 2 or 3 and then press ENTER on the keyboard?
Echo     2. TRANSFER FILES FROM DESKTOP TO LAPTOP
Echo     3. EXIT THE PROGRAM
echo.
set /p choice=Enter Number: 

if '%choice%'=='2' goto _desktopToLaptop
if '%choice%'=='3' goto :EOF
echo "%choice%" is not a valid option.  Please try again
echo.
goto _start

:: Detect Drive Letters
:_desktopToLaptop
setlocal EnableDelayedExpansion
FOR /F "usebackq skip=1" %%a IN (`WMIC logicaldisk where DeviceID^="%_driveID%" get caption`) DO (
    SET _NetworkDrive=%%a
    if exist %%a ( 
      CALL :_backup
      goto :EOF
    ) else (
      echo.
      echo The laptop does not appear to be attached to the computer.
      echo.
      pause
      goto :EOF
    )
)

:_backup
:: Detect the folder locations and begin to backup each location to the laptop.
FOR /F "tokens=4 skip=1" %%a in ('REG QUERY "%_regshell%" /v "%_regdesktop%"') DO (
 SET _dt=%%a
 echo robocopy "!_dt!" "!_NetworkDrive!!_fndesktop!" !_params!
 echo attrib -h -r "!_NetworkDrive!!_fndesktop!"
)
echo we are past the for loop
pause
:: Return to directory program was run from
popd
答案

如果其他人遇到此问题或类似问题,请检查您的令牌和跳过。我作为一个批处理工作得很好,但是当我作为一个调用包括在内时,我必须将选项从tokens = 4 skip = 1更改为tokens = 3 * skip = 2以获得正确的输出。

FOR LOOPS中的正确令牌应该是:

@echo off
:: Set Variables
SET _driveID=T:
SET _params=/Z /E /COPY:DT /R:1 /W:0 /XD LocalService NetworkService temp "temporary internet files" winsxs Content.IE5 cache /XF ntuser.* *.tmp /XJ /FP /NC /NS /NP /NJH
SET _regshell=HKEY_CURRENT_USERSoftwareMicrosoftWindowsCurrentVersionExplorerUser Shell Folders
SET _regdesktop=Desktop

:: Set Current Directory
pushd %SystemDrive%

:: Start Menu - Create Choices and Options.  Send to various places to perform the actions.
:_start
cls
ECHO   Please type either option 2 or 3 and then press ENTER on the keyboard?
Echo     2. TRANSFER FILES FROM DESKTOP TO LAPTOP
Echo     3. EXIT THE PROGRAM
echo.
set /p choice=Enter Number: 

if '%choice%'=='2' goto _desktopToLaptop
if '%choice%'=='3' goto :EOF
echo "%choice%" is not a valid option.  Please try again
echo.
goto _start

:: Detect Drive Letters
:_desktopToLaptop
setlocal EnableDelayedExpansion
FOR /F "usebackq skip=1" %%a IN (`WMIC logicaldisk where DeviceID^="%_driveID%" get caption`) DO (
    SET _NetworkDrive=%%a
    if exist %%a ( 
      CALL :_backup
      goto :EOF
    ) else (
      echo.
      echo The laptop does not appear to be attached to the computer.
      echo.
      pause
      goto :EOF
    )
)

:_backup
:: Detect the folder locations and begin to backup each location to the laptop.
FOR /F "tokens=3* skip=2" %%a in ('REG QUERY "%_regshell%" /v "%_regdesktop%"') DO (
 SET _dt=%%a
 echo robocopy "!_dt!" "!_NetworkDrive!!_fndesktop!" !_params!
 echo attrib -h -r "!_NetworkDrive!!_fndesktop!"
)
echo we are past the for loop
pause
:: Return to directory program was run from
popd
另一答案

鉴于脚本中的主要问题似乎是在已定义的注册表项和值中为数据设置变量,您可以使用:

Set "_regshell=HKCUSoftwareMicrosoftWindowsCurrentVersionExplorerUser Shell Folders"
Set "_regdesktop=Desktop"

Set "_dt="
For /F "EOL=H Tokens=2*" %%A In ('Reg Query "%_regshell%" /V "%_regdesktop%"'
) Do Set "_dt=%%~B"
If Not Defined _dt GoTo :EOF
Echo "%_dt%"

以上是关于批量文件跳过FOR LOOP的主要内容,如果未能解决你的问题,请参考以下文章

为什么在迭代sqlite数据库时for-loop变量会跳过每隔一行?

for loop 从字符串链接下载文件

VS Code Intellisense 一开始不建议片段

020003_循环——批量处理数据

如何用批处理,根据日期并且跳过双休日,来批量命名Excel文件。如20131014这样的格式。每个

我想将'for-loop'的结果保存为python中的文本文件