Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple Windows batch to move folders

I'm simply trying to move all files and subdirectories inside d:\temp\test to d:\temp\archive and so I tried these commands:

move d:\temp\test\* d:\temp\archive\
move d:\temp\test\*.* d:\temp\archive\

but I got this error in return:

The filename, directory name, or volume label syntax is incorrect.

Then I dug around on the web and tried this inside a doc bat:

for %%F in ( d:\temp\test\*.* ) do move /Y %%F d:\temp\archive

and this time it shows no error, but everything is standing still and made no changes.

What am i missing here? I'm trying this on Windows 10.

like image 888
jimzcc Avatar asked Jan 31 '26 21:01

jimzcc


1 Answers

ok, if you want to just move all the files directories from inside \test\ then this will do the files first, then directories in a batch. The for /d will copy the directories and sub directories and files.

@echo off
move "d:\temp\test\*" "d:\temp\archive"
for /d %%a in ("D:\temp\test\*") do move "%%~fa" "d:\temp\archive\"

as a side note, from cmd when running below you get an error.

move d:\temp\test\* d:\temp\archive

That is because it will move all files, but not directories. If you get The filename, directory name, or volume label syntax is incorrect. there are no files and only folders which your move command cannot see.

NOTE from within a batch file, the /Y switch is disabled and folders will not be replaced if exist. So if you plan on overwriting often, perhaps rather use xcopy and update archive instead, then run a delete in d:\temp once the files have successfully copied.

Lastly, always enclose your paths in double ". In this case it will work fine without the double quotes, but if you have something like move d:\program files\temp\* d:\temp\archives it will create an error because of the space between program and files so it is always better to use move "d:\program files\temp\*" "d:\temp\archive

EDIT Understanding the %%~ assignments. In these examples we use %%I instead of %%a

%~I         : expands %I removing any surrounding quotes (")
%~fI        : expands %I to a fully qualified path name
%~dI        : expands %I to a drive letter only
%~pI        : expands %I to a path only
%~nI        : expands %I to a file name only
%~xI        : expands %I to a file extension only
%~sI        : expanded path contains short names only
%~aI        : expands %I to file attributes of file
%~tI        : expands %I to date/time of file
%~zI        : expands %I to size of file
%~$PATH:I   : searches the directories listed in the PATH
              environment variable and expands %I to the
              fully qualified name of the first one found.
              if the environment variable name is not
              defined or the file is not found by the
              search, then this modifier expands to the
              empty string

The modifiers can be combined to get compound results:

%~dpI       : expands %I to a drive letter and path only
%~nxI       : expands %I to a file name and extension only
%~fsI       : expands %I to a full path name with short names only
%~dp$PATH:I : searches the directories listed in the PATH
              environment variable for %I and expands to the
              drive letter and path of the first one found.
%~ftzaI     : expands %I to a DIR like output line`

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!