在批处理中请求多个文件名并用逗号分隔

在批处理中请求多个文件名并用逗号分隔

我编写了一个小程序,要求用户输入文件名和路径。它首先创建路径,然后将文件移动到该路径。

@echo off

set /p filename="pls enter filename(s): "
set /p filespath="pls enter path: "

mkdir %filespath%
move %filename% %filespath%

但是我怎样才能让它要求多个文件名,blabla.png, test1.png, image.png ... 并用逗号分隔每个文件名,然后移动文件?

答案1

您可以使用该goto命令多次要求输入,并将所有值附加到新变量(filelist)。例如

@echo off

set filelist=

:start

set filename=
set /p filename="pls enter filename, or Enter when finished: "

if "%filename%"=="" goto :nextstep

set filelist=%filelist% %filename%

goto :start


:nextstep

set filespath=
set /p filespath="pls enter path: "
if "%filespath%"=="" goto :EOF

mkdir %filespath%

for %%f in (%filelist%) do (
    move %%f %filespath%
)

答案2

没有必要像这样分隔输入 - 您可以使用计数并索引变量名称来处理所有输入,如下所示:

@echo off
Setlocal EnableDelayedExpansion

    For /L %%A IN (1,1,8000) Do (
        Set /P "Filename[%%A]=Drag and Drop or Enter Filename. Type Done to Process }<"
        Set /A Filecount=%%A - 1
        IF /I "!Filename[%%A]!"=="done" (GOTO :Process)
    )

:process
REM Your commands here for filepath and move actions. The below is how to access the indexed variables.
For /L %%B IN (1,1,%Filecount%) DO (ECHO( !Filename[%%B]!)
pause
Exit

答案3

要保存伪数组中文件的名称,还要在移动文件夹之前检查文件夹是否已创建以及文件是否存在...


@echo off && setlocal enabledelayedexpansion

cd /d "%~dp0"

:_loop_name_:
set "filename=" && call set /p filename="[ Enter filename's ] if done, just [enter]!! "

echo/!filename!|%__APPDIR__%findstr.exe . >nul && (
set /a "_cnt+=1+0" && call set "_filename_!_cnt!=!filename!" && goto :_loop_name_: )

:_loop_path_:
set /p filespath="pls enter path: " || goto :_loop_path_:
echo/!filespath!|%__APPDIR__%findstr.exe . >nul || goto :_loop_path_:

if not exist "!filespath!\." mkdir 2>nul "!filespath!" || (echo/ Error MKDir: !filespath! & goto :EOF)
for /l %%L in (1 1 !_cnt!)do if exist "!_filename_%%~L!" (call move /y "!_filename_%%~L!" "!filespath!" >nul 
) else echo/File not found/exist: "!_filename_%%~L!"

%__APPDIR__%timeout.exe -1
rem./ :: do more task....
endlocal && goto :EOF

相关内容