How to split the filename from a full path in batch?

前端 未结 5 552
离开以前
离开以前 2020-11-29 23:30

How do I split the filename out of a full path in batch scripting?

5条回答
  •  不知归路
    2020-11-30 00:26

    Continuing from Pete's example above, to do it directly in the cmd window, use a single %, eg:

    cd c:\test\folder A
    for %X in (*)do echo %~nxX

    (Note that special files like desktop.ini will not show up.)

    It's also possible to redirect the output to a file using >>:

    cd c:\test\folder A
    for %X in (*)do echo %~nxX>>c:\test\output.txt

    For a real example, assuming you want to robocopy all files from folder-A to folder-B (non-recursively):

    cd c:\test\folder A
    for %X in (*)do robocopy . "c:\test\folder B" "%~nxX" /dcopy:dat /copyall /v>>c:\test\output.txt

    and for all folders (recursively):

    cd c:\test\folder A
    for /d %X in (*)do robocopy "%X" "C:\test\folder B\%X" /e /copyall /dcopy:dat /v>>c:\test\output2.txt

提交回复
热议问题