带有空格的批处理参数,用于循环递归移动

时间:2015-09-25 11:56:19

标签: windows batch-file cmd

我正在尝试将一个批处理放在一起,以递归方式将所有zip文件从当前目录移回一个目录,我从父目录中指定搜索目录。

例如,当前目录为download,递归搜索的子命令是命令行参数。

然后,我想将*.zipdownload移回一个目录。我不想搜索除我指定的目录之外的任何其他目录。

>movezipdir junk 2009

这会输出junk 2009。那时垃圾是出乎意料的。

这是我所拥有的,但for循环不喜欢带空格的变量......

@echo off
IF %1.==. GOTO No1
set DirName=%*
echo %DirName%
For /d %DirName% in ( *.* ) do (
    For /d %%e in (""%DirName%"\*") do (
rem     move /Y "%%e\*.zip" "%CD%\.."
echo    %DirName%
rem     rd /S /Q "%%e"
    )
rem rd /S /Q %DirName%
        )   
GOTO End1
:No1
  ECHO No Directory Specified
GOTO End1
:End1

如果有人能指出我正确的方向,那就太好了。谢谢。肯

1 个答案:

答案 0 :(得分:0)

@echo off

rem :: check if the args is not nul
if [%1] == [] (
    goto error
    ) else (
    for /f "usebackq tokens=1*" %%i in ('echo %*') do (
        set "dirname=%%~i"
    )
)

rem check if the specified args correspond to a directory
if exist "%dirname%\nul" echo %dirname% it's a directory || goto error

set /a c=1
setlocal EnableDelayedExpansion
for /f "tokens=* delims=" %%a in ('dir /b /s /a-d "%dirname%\*.txt"') do (

    rem usage: move [{/y | /-y}] [Source] [Target]
    rem https://technet.microsoft.com/en-us/library/cc772528.aspx
    rem http://ss64.com/nt/move.html
    rem %cd% here are redundant because when target are omitted,
    rem  the target are always the current directory
    echo move /-y "%%a" "%cd%"

    rem break loop after 5 iterations.
    rem You can remove it if the test are Okay
    if [!c!] equ [5] goto break_loop
    set /a c+=1

)
:break_loop
rem usage: RD [/S] [/Q] [drive:]path
echo rd /s /q %dirname%
exit /b 0

:error
echo     Error: No directory specified or invalid arguments
echo     Usage: %~0 ^<full path of the directorys^>
echo   Example: %~0 c:\user\janne\MyDocuments
exit /b 0