5

I have a script to move folders from one local account to my admin. But I want to use a wildcard in my script, so for example if the folder name is FOLDER, FOLDER1 is also moved to the admin.

I am trying this command:

Move /Y C:\Users\Test\Desktop\folder* "C:\Users\admin\Documents\Moved

But I get the error:

The syntax of the file name, folder name, or volume name is incorrect.

2 Answers2

7

The move command does not support wildcards as you are trying to use here. However, you can use the dir command with the /s /b /ad parameters in a for /f loop and make it recursively traverse the source folder for directories only and then iterate those folders with the move command to move the folders to the destination folder.

for /f "tokens=*" %a in ('dir /s /b /ad "C:\source\folder\*"') do move /y "%~a" "C:\Destination\folder\"

Further Resources

1

Moving folders this way with wildcards can be achieved with very similar syntax to moving files with cmd's move command in PowerShell like so:

Move-Item -Path C:\source_folder\A*1 -Destination C:\destination_folder

This will move all folders in source_folder matching the A*1 pattern with the classic * wildcard character. (Axxx1, A1, A111, AAA1, Areallylongstring1 etc.)

I have also tested the single character wildcard ?, which works as well (AA1, AB1, AC1, A11, etc.).

Only keep in mind to use quotation marks " around your folder path if it contains spaces.

John Doe
  • 111