vicki
vicki

Reputation: 157

batch file to segregate files based on a specific string, occurring anywhere, in a filename

I want to move files in the folder c:\test (all are .wav files), containing string 'ooc' occurring anywhere in the filename. I have written a batch file for the same:

@echo off
cd C:\test
FOR /R %completepath% %%G IN (*ooc*.wav) DO
(
move /-y "C:\test\*.wav" "C:\ooc\"
)

However, this is not working. Any suggestions please.

Upvotes: 1

Views: 126

Answers (2)

Thomas
Thomas

Reputation: 605

I guess what you want is this:

@echo off
cd C:\test
FOR /R %completepath% %%G IN (*ooc*.wav) DO (
    move /-y "%%G" "C:\ooc\"
)

Upvotes: 0

dbenham
dbenham

Reputation: 130819

Moving one file at a time by iterating all files:

for /r %completepath% %%F in (*ooc*.wav) do move /-y "%%F" "c:\ooc\"

Moving whole folders at a time by iterating folders:

for /r %completepath% %%F in (.) do if exist *ooc*.wav move /-y "%%~fF\*ooc*.wav" "c:\ooc\"

You could remove the IF and redirect stderr to NUL to hide any "cannot find the file specified" error message, but then all potential error messages will be obscured.

for /r %completepath% %%F in (.) do move /-y "%%~fF\*ooc*.wav" "c:\ooc\" 2>nul

Upvotes: 1

Related Questions