0

I wrote a batch file using drag and drop to move multiple files in a folder to another directory with a new folder with the same name as the folder before.

Example if I have a folder that is name Stuff and within this folder I have three files a.png, b.txt, and c.jpg.

Let's say the first folder Stuff that holds the files that I want to move are in a directory like ...\flower\things\Stuff.

Then I want to make a new folder at C:\ with the name Stuff and then move the files within the first folder Stuff to the new folder Stuff at C:\

Example C:\Stuff\a.png, b.txt, c.jpg

I want to make it universal so if the first folder had a different name then Stuff, it will use the name of the folder that holds the files that I want to move.

What I have below works, but I ran to many problems before getting this to work and do not have much knowledge in coding. I was wondering if the batch file will run to any problems or if there is a simpler or cleaner way of doing this.

    @ECHO OFF
    ECHO "%~1"
    FOR %%I IN (.) DO SET CurrentD=%%~nI%%~xI
    MKDIR "C:\%CurrentD%"
    :loop
    MOVE "%~1" "C:\%CurrentD%"
    shift
    if not ["%~1"]==[""] goto loop
Tou
  • 5
  • 4
  • Instead of `%%~nI%%~xI` you could state `%%~nxI`, it returns the same... – aschipfl Oct 08 '15 at 23:44
  • Sorry if I was not clear before. Let's say the first folder Stuff that holds the files are in a directory like ...\flower\things\Stuff. Then I want to make a new folder at C:\ with the name Stuff and then move the files within the first folder Stuff to the new folder Stuff at C:\. I want to make it universal so if the first folder had a different name then Stuff, it will use the name of the folder that have the files that I want to move. – Tou Oct 08 '15 at 23:44
  • You should clarify your question by *editing* your post rather than just commenting... – aschipfl Oct 08 '15 at 23:47

1 Answers1

0
@echo off
if not exist destination-folder\nul (mkdir "destination-folder")
for %%i in (%*) do (
  move "%%~i" "destination-folder"
)

Wait, move command don't need for loop here.

@echo off
mkdir "destination-folder"
move %* "destination-folder"

hmm let me try

Just for the record: There is another way but that don't deal with space in filename.

@echo off
setlocal

:again
if "%~1" == "" goto done
move "%~1" "destination-folder"
shift
goto again
:done

Oh wait it does in drag and drop, but not in command-line. I mean if I type script.bat file 1 with space.txt it fail by splitting every space like

move "file" "destination-folder"
move "1" "destination-folder"
move "with" "destination-folder"
...
Community
  • 1
  • 1
Paul
  • 2,620
  • 2
  • 17
  • 27
  • Thank you. Your for loop was much cleaner and simpler then what I had. – Tou Oct 09 '15 at 00:10
  • Yes it is drag and drop to the batch file and I have replaced everything that was at :loop and below in my batch file with your for loop and is working great. – Tou Oct 09 '15 at 00:37
  • @Tou You welcome :) don't forget to upvote if you like the answer. – Paul Oct 09 '15 at 00:39