Home > Back-end >  Batch move file with using regex to new folder
Batch move file with using regex to new folder

Time:05-08

I'm not dev, I'm just looking for get code.

my folder:

default-0.png
[email protected]
default-1.png
[email protected]
default-2.png
[email protected]
default-3.png
[email protected]
default-4.png
[email protected]
default-5.png
[email protected]
default-6.png
[email protected]
default-7.png
[email protected]
default-8.png
[email protected]
default-9.png
[email protected]
...

I want move files with filename "@2..." only and any file extension to "new folder".

[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
...

I trying use regex: (^. @2. .$) see check regex101 for full match text.

I tried my code and doesn't work:

@echo off
setlocal enabledelayedexpansion
for /f %%f in ('dir /b | findstr /r "(^. @2. .$)") do (
    md "new folder"
    move "%%~f" "moved folder"
)
Echo done
pause

anyone help me, what am i wrong?

UPDATED: thanks for @develc (answered) and @Mofi (edited) This worked, perfect number of my files matched regex.

md "new folder"
move "*@2x*" "new folder"

ANOTHER METHOD: This worked but 0.1% can't move cause can't detect to filename has comment like example@2x - if comment.png. thanks for answered by @Hackoo

@echo off
MD "new folder">nul 2>&1
setlocal enabledelayedexpansion
for /f %%f in ('dir /b *@2*') do (
    move "%%~f" "new folder"
)
Echo done
pause

CodePudding user response:

There can be used something like this:

md "new folder"
move "*@2x*" "new folder"

In batch files, *a means everything that ends with a; b* means everything that starts with b; a*b means everything that starts with a and ends with b; a?b means everything that starts with a, has one character between a and b and ends with b. So * is a wildcard for everything and ? is a wildcard for every character.

CodePudding user response:

You can try something like that :

 @echo off
 MD "new folder">nul 2>&1
 setlocal enabledelayedexpansion
 for /f %%f in ('dir /b *@2*') do (
     move "%%~f" "new folder"
 )
 Echo done
 pause

CodePudding user response:

Normally your regex syntax seem to be correct. But when you check "findstr" documentation the plus ( ) character is not in accepted list :)

Findstr doc: https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/findstr

You can try this:

@echo off
setlocal enabledelayedexpansion
MD "new folder">nul 2>&1
for /f %%f in ('dir /b ^ | findstr /r "(^.*@2.*$)"') do (
    move "%%~f" "new folder"
)
Echo done
pause

CodePudding user response:

There can be used also:

%SystemRoot%\System32\robocopy.exe . "new folder" *@2x.* /MOV /NDL /NFL /NJH /NJS >nul

The entire directory tree to the specified destination folder – in this case the subdirectory new folder in current directory – is automatically created by ROBOCOPY on not existing before moving all files in current directory matched by the wildcard pattern *@2x.*.

Run in a command prompt window robocopy /? for help on this command.

See also:

  • Related