将文件移到目标目录的子文件夹中

问题描述 投票:0回答:1

我上周开始使用批处理命令,但是我编写的脚本确实给我带来了很大的障碍。

我想做什么

将PDF文件从C:\Users\JK\Documents\reports PDFs移动到目标W:\Departments\QA\cases中的预制子文件夹中。

例如,脚本会将2223 report.pdf移至W:\Departments\QA\cases\2201 - 2300\2223

我尝试了什么

我根据this thread中的答案制作了一个脚本

cls
@pushd %~dp0

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "SourceDir=C:\Users\JK\Documents\reports PDFs"
set "DestDir=W:\Departments\QA\cases\"

for /F "eol=| delims=" %%A in ('dir /B /A-D-H "%SourceDir%\*.pdf" 2^>nul') do (
    for /F "eol=| tokens=1" %%B in ("%%~nA") do (
        for /D %%C in ("%DestDir%\%%B*") do move /Y "%SourceDir%\%%A" "%%C\"
    )
)

endlocal
popd

pause

我被困在哪里

如何在目标目录中添加子文件夹或为其添加帐户?

FYI,我还尝试通过将%DestDir%\%%B更改为%DestDir%\*\%%B*在目标目录的末尾添加通配符。

windows batch-file directory subdirectory
1个回答
0
投票

我可能会使用以下脚本来完成任务(请参阅所有解释性的rem备注:]

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Define constants here:
set "_SOURCE=C:\Users\JK\Documents\reports PDFs" & rem // (source directory; `.` or `` is current, `%~dp0.` is script parent)
set "_TARGET=W:\Departments\QA\cases" & rem // (target directory; `.` or `` is current, `%~dp0.` is script parent)
set "_SUBDIR=#"      & rem // (set to non-blank value in order to create individual sub-directories)
set "_FMASK=????*.pdf"    & rem // (pattern to match source files)
set "_DMASK=???? - ????*" & rem // (pattern to match target directories)
set "_FFIL1=^[0123456789][0123456789][0123456789][0123456789] .*\.pdf$" & rem // (filter 1 for source files)
set "_FFIL2=^[0123456789][0123456789][0123456789][0123456789]\.pdf$"    & rem // (filter 2 for source files)
set "_DFIL1=^[0123456789][0123456789][0123456789][0123456789] - [0123456789][0123456789][0123456789][0123456789] .*$"
set "_DFIL2=^[0123456789][0123456789][0123456789][0123456789] - [0123456789][0123456789][0123456789][0123456789]$"

rem // Change into source directory:
pushd "%_SOURCE%" && (
    rem // Iterate through all files matching the specified pattern and filters:
    for /F "eol=| delims=" %%F in ('
        dir /B /A:-D-H-S "%_FMASK%" ^| findstr /I /R /C:"%_FFIL1%" /C:"%_FFIL2%"
    ') do (
        rem // Store full path of currently iterated file:
        set "FILE=%%~fF"
        rem // Extract the leading numeric part of the file name:
        for /F "eol=| delims= " %%N in ("%%~nF") do (
            rem // Store the numeric part:
            set "NUM=%%N"
            rem /* Remove any leading zeros from the numeric part of the file name, because
            rem    such cause the number to be unintentionally interpreted as octal: */
            set /A "INT=0" & for /F "eol=| tokens=* delims=0" %%Z in ("%%N") do 2> nul set /A "INT=%%Z"
        )
        rem // Change into target directory:
        pushd "%_TARGET%" && (
            rem // Iterate through all directories matching the specified pattern and filters:
            for /F "eol=| delims=" %%D in ('
                cmd /V /C 2^> nul dir /B /A:D-H-S "!_DMASK:|=%%I!" ^| findstr /I /R /C:"%_DFIL1%" /C:"%_DFIL2%"
            ') do (
                rem // Store name of currently iterated directory:
                set "TDIR=%%D"
                rem // Extract first (from) and second (to) numeric parts of directory names:
                for /F "eol=| tokens=1-2 delims=- " %%S in ("%%D") do (
                    rem // Remove any leading zeros from first (from) numeric part:
                    set /A "FRM=0" & for /F "eol=| tokens=* delims=0" %%Z in ("%%S") do set /A "FRM=%%Z"
                    rem // Remove any leading zeros from second (to) numeric part:
                    set /A "TOO=0" & for /F "eol=| tokens=* delims=0" %%Z in ("%%T") do set /A "TOO=%%Z"
                )
                rem // Toggle delayed variable expansion to avoid trouble with `!`:
                setlocal EnableDelayedExpansion
                rem /* Do integer comparisons to check whether the numeric part of the file name
                rem    lies within the range given by the directory name (from/to): */
                if !INT! geq !FRM! if !INT! leq !TOO! (
                    rem // Numeric part of file name lies within range, hence try to move file:
                    if defined _SUBDIR (
                        2> nul md "!TDIR!\!NUM!"
                        ECHO move "!FILE!" "!TDIR!\!NUM!\"
                    ) else (
                        ECHO move "!FILE!" "!TDIR!\"
                    )
                )
                endlocal
            )
            rem // Return from target directory:
            popd
        )
    )
    rem // Return from source directory:
    popd
)

endlocal
exit /B

您可能需要根据顶部的Define constants here:注释部分,根据情况调整一些常量。

在测试了脚本的正确输出之后,删除大写的ECHO命令]move命令之前!为了避免出现多个1 file(s) moved.消息,请将这些ECHO命令替换为> nul

© www.soinside.com 2019 - 2024. All rights reserved.