I want to make a file where it copies all images into a new folder in the current directory, except you can edit the file to get rid of directories like AppData or Program Files. Problem: it doesn't acknowledge when there's a space in the directory, and it cuts it off where the space character lies, which ends up breaking the code. Could someone help explain how to fix it?

@echo off
color 0a

set directory=%cd%
set /a namechooser=%random% * 20 / 32768 + 1
if %namechooser%==5 (set "name=booba") else (set "name=All Images")

echo Do you want to make every file unhidden before searching for these images?
choice /m ">"
if %errorlevel%==1 (attrib -h *.*)

echo Okay, searching for all common images now. Please wait . . .
for /r %%i in (*.bmp, *.gif, *.ico, *.jpg, *.jpe, *.jpeg, *.kra, *.png, *.svg, *.tif, *.tiff) do (echo %%i>>"allimages.log")
attrib +t allimages.log
echo Alright, do you want to organize all of the located files into a folder in this current directory?
choice /m ">"
if %errorlevel%==2 (cls & echo Oh, okay. & pause & exit)

mkdir "%cd%/%name%"
echo Copying the files into the folder. Please wait . . .
for /f "%%r" in (allimages.log) do (echo %%r)
del "allimages.log" >nul
echo Done!

:: Please don't question why I'm using so much extra unnecessary code just to do something that could be done :::::
:: in a few lines; this is an experiment that makes me use "for loops" in some sort of situation.             :::::
  • I believe Python is now supported (for free) by Microsoft -- would make life a whole lot easier. – GLRoman May 9 at 21:26
  • Thank you for your comment, but I do not know Python. I probably should learn it, as it is a very practical programming language, but I do not. – Joshua May 9 at 21:31
  • @GLRoman Just a comment, but Python has been free, easy to install and generally well supported on Windows for many years. Microsoft was exceptionally late to the game regarding Python downloads through the Microsoft Store. – Anaksunaman May 10 at 0:48

If I am not misunderstanding, you should try adding "tokens=*" to your log processing loop:

for /f "tokens=*" %%r in (allimages.log) do (echo %%r)

Note that using "%%r" can cause an issue (so not "tokens=*" "%%r").

Similarly, for copying the items, you could do something like:

for /f "tokens=*" %%r in (allimages.log) do (copy "%%r" "%cd%/%name%")

The "" in the copy command above are necessary when dealing with paths with spaces.


So you are aware, in your extension reading loop, *.jpe and *.jpeg will both match files with a .jpeg extension. This means your log file may have one or more groups of entries such as:


This shouldn't have much effect when processing files (i.e. you will likely simply process any duplicate entries twice when copying), but it is probably worth keeping in mind.

You can eliminate this behavior by either removing the *.jpe entry (to match only .jpeg files, not .jpe) or the *.jpeg entry (to match both .jpe and .jpeg files).

  • Thank you so much! I don't fully understand the "tokens" yet, so I appreciate you bringing that tip to my attention. As for the "move" command, I want to actually copy (not move) the images as to not disorganize one's files, but the move command would work just as well to. Thank you for also telling me about the ".jpe" and ".jpeg" confusion, as I didn't know that could happen previously. – Joshua May 9 at 9:00
  • 1
    Yeah, I misread the question regarding copy vs. move. Apologies. ;) In any case, your welcome. Glad to help. =) – Anaksunaman May 9 at 20:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.