-1

How would I alter this code to make it non-recursive?

FOR /R %pathold% %%G in (*.tif *.tiff *.jpg *.jpeg *.pdf) do (
    move "%%G" %pathnew%>NUL
    echo %%G
)

I tried using ForFiles, but it doesn't support multiple Wildcards.

ForFiles /P %pathold% /M *.tif *.tiff *.jpg *.jpeg *.pdf /C "move @path %pathnew%"

ERROR: Invalid argument / option - '* .tiff'.

SherylHohman
  • 16,580
  • 17
  • 88
  • 94
Lyux
  • 453
  • 1
  • 10
  • 22
  • 1
    Open a [command prompt](https://www.howtogeek.com/235101/), run `for /?` and read the output help explaining option `/R` for a recursive search in current directory or the specified directory after `/R` which is here `%pathold%`. You can use either above __FOR__ command line `pushd "%pathold%"` to push current directory on stack and set `%pathold%` as current directory and use below __FOR__ command block `popd` to pop the directory path from stack and restore the previous current directory. I recommend further to use `move "%%G" "%pathnew%"`, except `pathnew` holds path with double quotes. – Mofi Jan 03 '20 at 12:29
  • Run in command prompt window `pushd /?` and `popd /?` for help on those two commands. You could use also `FOR %%G in ("%pathold%\*.tif" "%pathold%\*.tiff" "%pathold%\*.jpg" "%pathold%*.jpeg" "%pathold%\*.pdf") do (` if the environment variable `pathold` contains the folder path without a backslash at end and not already enclosed in double quotes. A folder or file name without or with path should be never assigned to an environment variable with surrounding double quotes to be able to syntactically correct concatenate the file/folder name with path/file name. – Mofi Jan 03 '20 at 12:34
  • I suggest further to run `move /?` and read the output help. This command supports wildcards. For that reason it would be much more efficient to run `for %%I in (tif tiff jpg jpeg pdf) do if exist "%pathold\*.%%I" move /Y "%pathold\*.%%I" "%pathnew%" >nul`. Or you use just command __ROBOCOPY__ with help output on running `robocopy /?` which can move also all files matching one or more wildcard patters like `robocopy "%pathold%" "%pathnew%" *.tif *.tiff *.jpg *.jpeg *.pdf /MOV /R:3 /W:5 /NP`. It is important here that `pathold` and `pathnew` hold the path strings without backslash at end. – Mofi Jan 03 '20 at 12:41
  • Note: Command __FOR__ processes a file like `Image.tiff` also with using wildcard pattern `*.tif` because of the short 8.3 file name of `Image.tiff` is for example `IMAGE~1.TIF`. The Windows file systems are still configured by default to store short 8.3 file/folder names for files/folders with long names and the file system functions to get first and next directory entry matching a wildcard pattern applies the pattern also on the short names and not only on the long names. PS: I missed the backslash in `"%pathold%\*.jpeg"` in second comment. – Mofi Jan 03 '20 at 12:49
  • BTW: I recommend to delete your question because of being off-topic. It is in real a non-programming question caused by not reading the documentation of the used [Windows Commands](https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/windows-commands) by running them with `/?` in a cmd window, or reading the referenced Microsoft documentation pages or reading even better documentation on [SS64.com - A-Z index of the Windows CMD command line](https://ss64.com/nt/). – Mofi Jan 03 '20 at 12:56
  • 1
    Lyux, if you feel that those comments are not helpful, you've chosen the wrong site. I do not believe that anyone here would have taken so much time and imparted so much information to assist you to fix your **off topic** question. – Compo Jan 03 '20 at 14:55

1 Answers1

0

So in order to use only the root Directory remove the /R %pathold% from the Code above so it looks like this:

FOR %%G in (*.tif *.tiff *.jpg *.jpeg *.pdf) do (
move "%%G" %pathnew%>NUL
echo Moving %%G
)

Important to note: You have to be in the Directory you wan't to move from. You can do this with cd %oldpath% or pushd %oldpath% for example.

Lyux
  • 453
  • 1
  • 10
  • 22
  • 1
    With regards your ***Important to note:***, you can use `PushD "%pathold%"` as advised by @Mofi in their opening, _but seemingly unhelpful_, [comment](https://stackoverflow.com/questions/59577630/non-recursive-file-move-with-multiple-wildcards?noredirect=1#comment105323504_59577630). Single line example: `@PushD "%pathold%" 2>NUL&&((For %%# In (*.tif,*.jpg,*.jpeg,*.pdf)Do @Move /Y "%%#" "%pathnew%">NUL)&PopD)` – Compo Jan 03 '20 at 17:06
  • 1
    You shouldn't be thanking just me! I would advise an apology to @Mofi, _who provides more fully explained code and tutorial like advice than any other member on this particular section of this site_. In this case they provided at least three fully coded answers and additional helpful information for you to improve your own. You ignored all of it calling it unhelpful and blatantly telling them that none of those answered your question. – Compo Jan 03 '20 at 19:00