1

I made a bat that asks the user for a filename and a path. It creates the path first and then moves the file to the path.

@echo off

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

mkdir %filespath%
move %filename% %filespath%

BUT how can I make it ask for multiple filenames like blabla.png, test1.png, image.png ... and make it seperate each filename by comma and then move the files?

  • 1
    Iterate over your filenames list using FOR /F "tokens=1* delims=," %x IN ("%1") in a subroutine. – Akina Feb 11 at 7:44
2

You can use the goto command to ask for input multiple times, and append all values to a new variable (filelist). E.g.

@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%
)
1

There is no need to delimiter input like so - you can use a count and index the Variable name to process all input like so:

@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
0

To save the name of the files in the pseudo array, also check if the folder was created and if the file exists before moving them ...


@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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.