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.

3
  • The " on the target is unterminated. It is also unnecessary, since there are no embedded spaces. I prefer to add prefer a terminating \ when the target is a directory.
    – AFH
    Mar 27, 2018 at 10:59
  • 2
  • Not a duplicate of the first, as it is a question about moving files not folders (which MOVE treats differently). Not a duplicate of the second as REN only renames and cannot move to a different path (hence the user didn't ask to use REN).
    – jep
    Oct 17, 2018 at 19:21

2 Answers 2

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

4
  • i tried it and it seemed to help thx Mar 28, 2018 at 7:17
  • Wouldn't it be simpler to just use for /d %a in ("c:\source\folder*") do move /y "%a" "C:\Users\admin\Documents\Moved\"? The /d tells for to match against directory names instead of files.
    – jep
    Oct 17, 2018 at 19:19
  • @jep You may be right, I know for /d is more efficient than for /f for the iterations I believe, but I'd have to test to know for certain that it'd work for what the OP requested. This is one of those things that may have multiple solutions that would work where one could be better than the other, but I wrote it based on whatever my thought process or memory, etc. recalled then. Oct 17, 2018 at 19:58
  • This is weird - I'm finding that for /D %foobar IN ("D:\etc\*") DO ... doesn't work unless I use a single-letter name like %s, but then the command I wanted to use (move) wouldn't work unless I used a different variable-name that was also upper-case (%U). Gah! There's so many obscure, under-documented quirks and gotchas in Windows' command-line that make it impossible for a non-expert to quickly do simple tasks like moving a bunch of directories around.
    – Dai
    Apr 20, 2020 at 13:04
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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .