简体   繁体   中英

How do I move variable named files from one directory to another?

Here is what I have so far that doesn't work:

set from=V:\directory\CONCA*.*
set to=V:\directory\CONCA
for /f %%a IN ('dir "%from%" /b') do move %from%\%%a %to%

This returns the error message:

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

I do not always have a file in the directory that it is from, I do not want to see any error messages when it does not find a file.

I do want to see what is it moving when it finds something to move.

set "from=V:\directory"
set "to=V:\directory\CONCA"
    rem ensure that target folder exists
md "%to%" 2>NUL
for /f "delims=" %%a IN ('dir "%from%\CONCA*.*" /b /A:-D 2^>NUL') do (
    echo "%from%\%%a"
    rem move "%from%\%%a" "%to%\"
)

Explanation:

  • my comment to your question shows up the quoted error message culprit;
  • 2>NUL redirect error messages to NUL ; we need to escape > redirection operator when used in a command processed by for /F loop as 2^>NUL ;
  • "delims=" to treat possible spaces in file names properly;
  • /A:-D exclude folders to prevent move "V:\\directory\\CONCA" "V:\\directory\\CONCA\\" (attempt to move itself to itself );
  • echo "%from%\\%%a" to satisfy I do want to see what is it moving when it finds something to move claim;
  • rem move "%from%\\%%a" "%to%\\" operational move command is commented up using rem merely for debugging phase: remove rem as soon as debugged (but no sooner).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM