street
street

Reputation: 21

Iterate through folder passed as argument

So a bit of background: I have one of those old mp3 players that if you just drag and drop a whole folder, it then plays the songs randomly. The solution I found is to move the files one by one. But, when you have lots of folders this can be time consuming.

I am trying to make a script with Batch for Windows that moves file after file from one folder to another passed as parameters (ideally, it waits until the previous move is finished, so no moves are running in parallel. For now just added a timeout), but I am not sure if I am not using the right syntax or what I'm doing wrong (I've done a similar one in Shell, and it worked fine, but just curious).

This is the code right now:

@echo off
echo mvp3...

IF %%1.==. GOTO No1
IF %%2.==. GOTO No2

set arg1=%1
set arg2=%2
echo before_for
for /r %%i in (%%1) do (
    echo after_for
    move %%i %%2
    timeout 1
GOTO End1

:No1
  ECHO No param 1
GOTO End1
:No2
  ECHO No param 2
GOTO End1

:End1

)

But if I try to run something like ./mvp3 C:\Users\folder1 C:\Users\folder2, this is the output:

mvp3...
before_for
The syntax of the command is incorrect.

I've done a bit of duckduckgoing but couldn't find the answer, can someone illuminate me?

Thanks.

Upvotes: 1

Views: 82

Answers (1)

user7818749
user7818749

Reputation:

The loop will allow each file to be copied completely before the next file copy will start, so no need to put in the timeout.

@echo off
echo mvp3...

if "%~1"=="" echo Param 1 empty & goto :eof
if "%~2"=="" echo Param 2 empty & goto :eof
for /r %%a in (%~1\*) do echo move /Y "%%a" "%~dpn2"

Here we simply expand %1 and %2 to remove double quotes if exist, then test them for being empty. then pushd to the source and recursively copy each .mp3 file one after the other into the destination.

to understand variable expansion better, open cmd and run for /? read the help in the substitution of FOR variable references section.

Upvotes: 1

Related Questions