简体   繁体   English

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

[英]Move files into a subfolder of a destination directory

I started using batch commands last week and I've reached a real obstacle with a script I made.我上周开始使用批处理命令,但我制作的脚本遇到了真正的障碍。

What I want to do我想做的事

Move a PDF file from C:\\Users\\JK\\Documents\\reports PDFs into pre-made subfolders in the destination W:\\Departments\\QA\\cases .将 PDF 文件从C:\\Users\\JK\\Documents\\reports PDFs到目标W:\\Departments\\QA\\cases中的预制子文件夹中。

For example the script would move 2223 report.pdf to W:\\Departments\\QA\\cases\\2201 - 2300\\2223例如,脚本会将2223 report.pdf移动到W:\\Departments\\QA\\cases\\2201 - 2300\\2223

What I tried我试过的

I made a script based off the answer in 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

Where I am stuck我被卡住的地方

How could I add subfolders or account for them in the destination directory?我如何在目标目录中添加子文件夹或为其添加帐户?

FYI, I also tried adding a wildcard symbol at the end of the destination directory by changing %DestDir%\\%%B to %DestDir%\\*\\%%B* .仅供参考,我还尝试通过将%DestDir%\\%%B更改为%DestDir%\\*\\%%B*在目标目录的末尾添加通配符。

I would probably accomplish the task with the following script (see all the explanatory rem remarks):我可能会使用以下脚本完成任务(请参阅所有解释性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

You may need to adapt a few constants to your situation in the section commented with Define constants here: at the top.您可能需要在顶部的“ Define constants here:注释的部分中根据您的情况调整一些常量。

After having tested the script for the correct output, remove the upper-case ECHO commands in front of the move commands!在测试脚本的正确输出后,删除move命令前面的大写ECHO命令! In order to avoid multiple 1 file(s) moved.为了避免移动多个1 file(s) moved. messages, replace these ECHO commands by > nul .消息,将这些ECHO命令替换为> nul

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将文件从源目录移动到具有文件名第一部分名称的目标目录? - How to move files from a source directory to a destination directory with name of first part of file name? 将特定子文件夹中的文件移动到另一个子文件夹 - Move files in specific subfolders to another subfolder 将通配符文件夹结构文件移动到目标文件夹 - Move Wildcard Folder structure files to a destination folder Windows 7 Batch - 创建子文件夹,然后查找文件名中包含特定文本的文件并将这些文件移动到新创建的子文件夹中 - Windows 7 Batch - Create subfolder, then find files with certain text in file name and move those files in the newly created subfolder 批处理文件以在文件夹中创建新的子文件夹,将文件移动到目录中所有文件夹的新创建的子文件夹 - batch file to create new subfolder in folder, move file to newly created subfolder, for all folders in directory 用相同的名称创建新的子文件夹并将文件移动到新文件夹 - Create new subfolder with same name and move files to the new folder 创建批处理文件以删除,重命名和移动多个文件的多个子文件夹 - Create batch file to delete,rename and move multiple files multiple subfolder 如何将特定文件夹的每个子文件夹中的所有 *.pdf 文件移动到每月创建的子文件夹? - How to move all *.pdf files in each subfolder of a specific folder to a monthly created subfolder? Inno Setup 在安装时在目标目录中创建的临时 tmp 文件 - Temporary tmp files created by Inno Setup in destination directory while installing 移至ftp的子文件夹 - move to subfolder of ftp
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM