Solved Batch script to move files into folders?

February 11, 2014 at 11:53:35
Specs: Windows 7
Hi, I need a batch script to move ebook files into the correct Author folders.
The ebook files are all named in the format: "Authors Name - Book Title.ext" and I have thousands of folders already with the "Authors Name" as the folder name, all are first name then last name. all the ebook files and Author folders are together in one folder called 'EBOOKS'. Thanks.

See More: Batch script to move files into folders?

Report •


✔ Best Answer
February 19, 2014 at 14:01:43
Yeh, I'd meant to ask you about that: if you wanted to move the files or copy them. Just change this line:
set copy=copy "%%a-%%b" &::

to this:
set copy=move "%%a-%%b" &::

Any files remaining in the ebooks directory did not get moved, so listing the directory after running the batch will show any ones that failed for whaterver reason. If you are still insecure, you can add this to the line that has !copy!:
!copy! ||>>logfile echo "%%a-%%b" not processed
But that is redundant and unnecessary, generating another unneeded file.



#1
February 11, 2014 at 12:45:45
tested only briefly

@echo off & setLocal enableDELAYedeXpansioN

:main
for /f "tokens=* delims= " %%a in ('dir/b/a-d *.ext') do (
call :sub1 %%a
move "%%a" "!DEST!"
)
goto :eof

:sub1
for /f "tokens=1 delims=-" %%i in ("%*") do (
set DEST=%%i
)
goto :eof
::====== script ends here =================

=====================
M2 Golden-Triangle


Report •

#2
February 11, 2014 at 15:51:06
I made it into a batch file and put it into a test folder with some ebooks and folders with the authors names then ran it, it just flashes and is gone, nothings moved, not sure if I'm doing something wrong or if it's the code.

Report •

#3
February 11, 2014 at 21:02:55
For the "flashing", you just need a "pause", preferably between active lines 4 and 5, which will show you what's up with each item. I figure it to be leading and trailing spaces causing havoc with finding the directories (any item within quotes will incude leading and i guess trailing spaces under the "new" rules) . Here's a revision that tries to account for that as well as adding pauses:
@echo off & setlocal enabledelayedexpansion
set target=
set /p target=Enter the full path to the working directory:
if not defined target set target=c:\somewhere
cd %target%||set /p =%target% not accessible&goto :eof
for /f "tokens=1,2* delims=-" %%a in ('dir /b /a-d *.ext') do (
set copy=copy
call :alltrim "%%b"
call :alltrim "%%a"
:: remove the "echo" from the next line, and the "pause", if things look right
echo !copy!
pause
)
goto :eof

:alltrim
set y=%1
:1
set yy=%y%
set y=%y: "="%
set y=%y:" ="%
if %y% neq %yy% goto 1
set copy=%copy% %y%
::=== end

message edited by nbrane


Report •

Related Solutions

#4
February 12, 2014 at 03:03:43
Unfortunately that made no difference, it still just flashes and is gone even with the pause.

Report •

#5
February 13, 2014 at 04:06:15
Can anyone help me figure out what's happening here?
Thanks

Report •

#6
February 13, 2014 at 04:10:35
Open a cmd window instead, and run it from the correct folder without @echo off at the top of the program. Then post the output back here.

::mike


Report •

#7
February 13, 2014 at 12:06:31
Yeh, that was gonna be my next suggestion as well, but then I ran a couple of tests running the batch as a desktop icon (guessing that's what OP is doing), and realized that the batch is probably running in the wrong directory. I modified resp. #3 to try and fix this, interactively. Mike's CMD suggestion still applies for debugging a script (Althjough I find that ECHO ON usually generates so much "noise" that I have difficulty filtering out what's really going on, so i just use echos inside the batch, but that's just a matter of personal preference.

message edited by nbrane


Report •

#8
February 13, 2014 at 13:21:49
Thank you, this is what I get:


setlocal enabledelayedexpansion
for /f "tokens=1,2* delims=-" %%a in ('dir /b /a-d *.ext') do (
%%a was unexpected at this time.
set copy=copy
call :alltrim "%%b"
Invalid attempt to call batch label outside of batch script.
call :alltrim "%%a"
Invalid attempt to call batch label outside of batch script.
:: remove the "echo" from the next line, and the "pause", if things look right
echo !copy!
!copy!
pause
Press any key to continue . . .


Report •

#9
February 13, 2014 at 20:11:44
Did you save the script as a ".BAT" (text) file? It looks for all the world like you're running this from some other venue. The errors you've given would indicate that you are keying in each line from the CMD prompt, and not running a batch file. Otherwise I'm lost, because otherwise I can't duplicate your error using batch-script. If you cut/paste the response(s) into a file with extension .BAT, then execute the .BAT, I do not believe you could get the reported anomalies and errors. Could you perhaps elaborate on exactly what steps you used to get the errors referred to?

Report •

#10
February 14, 2014 at 10:50:17
If I try running it as a .bat file it just flashes and is gone, nothing happens even with 'pause' at the end, so Mikelinus suggested above that I try running it in a command window, so I was posting the result. Was that not what I was supposed to do?

message edited by Splodgeness


Report •

#11
February 14, 2014 at 10:54:36
My humble apologies, I meant to open a command window and then type in
c:\whereitis\filename.bat

or navigate to the directory

cd whereitis
filename.bat

::mike

message edited by mikelinus


Report •

#12
February 15, 2014 at 11:42:16
OK, Sorry, please presume I dont know anything. I now get:


C:\Users\-\Desktop\ebooks>setlocal enabledelayedexpansion

C:\Users\-\Desktop\ebooks>set target=

C:\Users\-\Desktop\ebooks>set /p target=Enter the full path to the working direc
tory:
Enter the full path to the working directory:


If I enter the path: C:\Users\-\Desktop\ebooks it just vanishes.


Report •

#13
February 15, 2014 at 18:56:20
Are you running the batchfile now? or still entering statements at the cmd prompt?
I do realize that you are working in an unfamiliar environment, and confusion ensues.
I'll try to help, but this is based on you running the batch script, and not otherwise.
Try inserting lines as shown:

set /p target=Enter the full path to the working directory:
echo target entered: %target%
pause
if not defined target set target=c:\somewhere
echo tentative: %target%, preparing to CD
cd %target%||set /p =%target% not accessible&goto :eof
echo CD was successful to %target%
pause

Modify the batch, inserting the lines as shown, and let me know how far it gets etc.


Report •

#14
February 16, 2014 at 02:26:51
I'm running the batch file now. This is what I get:


C:\Users\-\Desktop\ebooks>set /p target=Enter the full path to the working direc
tory:
Enter the full path to the working directory:


If I then enter the full path and press enter I get:


C:\Users\-\Desktop\ebooks>set /p target=Enter the full path to the working direc
tory:
Enter the full path to the working directory:C:\Users\-\Desktop\ebooks

C:\Users\-\Desktop\ebooks>echo target entered: C:\Users\-\Desktop\ebooks
target entered: C:\Users\-\Desktop\ebooks

C:\Users\-\Desktop\ebooks>pause
Press any key to continue . . .

If I then press enter it vanishes.


Report •

#15
February 16, 2014 at 13:25:27
My most humble apologies, I messed up this line:
cd %target%||set /p =%target% not accessible&goto :eof

It does not perform as it was meant to, but this should fix it (add parentheses):
cd %target% || (set /p =%target% not accessible&goto :eof)

Although your records show the script aborting even prior to this point (it's not displaying the "tentative" marker). If this fixes the problem, you should next see "CD was successful..." with another pause before the "FOR" loop. Another unfortunate misunderstanding is the persistance of ".ext" in the following line:
for /f "tokens=1,2* delims=-" %%a in ('dir /b /a-d *.ext') do (

You need to change .ext to the actual extension that your ebooks filenames have, or you will get a "file not found" error.


Report •

#16
February 16, 2014 at 14:21:32
OK, thanks, I think I got that, so this is the code now?

setlocal enabledelayedexpansion
set target=
set /p target=Enter the full path to the working directory:
echo target entered: %target%
pause
if not defined target set target=c:\somewhere
echo tentative: %target%, preparing to CD
cd %target% || (set /p =%target% not accessible&goto :eof)
echo CD was successful to %target%
pause
for /f "tokens=1,2* delims=-" %%a in ('dir /b /a-d *.epub') do (
set copy=copy
call :alltrim "%%b"
call :alltrim "%%a"
:: remove the "echo" from the next line, and the "pause", if things look right
echo !copy!
pause
)
goto :eof

:alltrim
set y=%1
:1
set yy=%y%
set y=%y: "="%
set y=%y:" ="%
if %y% neq %yy% goto 1
set copy=%copy% %y%


There are about a dozen different ebook extensions is it possible to specify more than one?
Now it appears to be working but the books are still not being moved.
I removed all but two ebooks and two author folders otherwise this was huge:

setlocal enabledelayedexpansion

C:\Users\-\Desktop\ebooks>set target=

C:\Users\-\Desktop\ebooks>set /p target=Enter the full path to the working direc
tory:
Enter the full path to the working directory:C:\Users\-\Desktop\ebooks

C:\Users\-\Desktop\ebooks>echo target entered: C:\Users\-\Desktop\ebooks
target entered: C:\Users\-\Desktop\ebooks

C:\Users\-\Desktop\ebooks>pause
Press any key to continue . . .

C:\Users\-\Desktop\ebooks>if not defined target set target=c:\somewhere

C:\Users\-\Desktop\ebooks>echo tentative: C:\Users\-\Desktop\ebooks, preparing t
o CD
tentative: C:\Users\-\Desktop\ebooks, preparing to CD

C:\Users\-\Desktop\ebooks>cd C:\Users\-\Desktop\ebooks || (set /p =C:\Users\-\
Desktop\ebooks not accessible & goto :eof )

C:\Users\-\Desktop\ebooks>echo CD was successful to C:\Users\-\Desktop\ebooks
CD was successful to C:\Users\-\Desktop\ebooks

C:\Users\-\Desktop\ebooks>pause
Press any key to continue . . .

C:\Users\-\Desktop\ebooks>for /F "tokens=1,2* delims=-" %a in ('dir /b /a-d *.ep
ub') do (
set copy=copy
call :alltrim "%b"
call :alltrim "%a"
echo !copy!
pause
)

C:\Users\-\Desktop\ebooks>(
set copy=copy
call :alltrim " Reapers are Angels 02 "
call :alltrim "Alden Bell "
echo !copy!
pause
)

C:\Users\-\Desktop\ebooks>set y=" Reapers are Angels 02 "

C:\Users\-\Desktop\ebooks>set yy=" Reapers are Angels 02 "

C:\Users\-\Desktop\ebooks>set y=" Reapers are Angels 02"

C:\Users\-\Desktop\ebooks>set y="Reapers are Angels 02"

C:\Users\-\Desktop\ebooks>if "Reapers are Angels 02" NEQ " Reapers are Angels 02
" goto 1

C:\Users\-\Desktop\ebooks>set yy="Reapers are Angels 02"

C:\Users\-\Desktop\ebooks>set y="Reapers are Angels 02"

C:\Users\-\Desktop\ebooks>set y="Reapers are Angels 02"

C:\Users\-\Desktop\ebooks>if "Reapers are Angels 02" NEQ "Reapers are Angels 02"
goto 1

C:\Users\-\Desktop\ebooks>set copy=copy "Reapers are Angels 02"

C:\Users\-\Desktop\ebooks>set y="Alden Bell "

C:\Users\-\Desktop\ebooks>set yy="Alden Bell "

C:\Users\-\Desktop\ebooks>set y="Alden Bell"

C:\Users\-\Desktop\ebooks>set y="Alden Bell"

C:\Users\-\Desktop\ebooks>if "Alden Bell" NEQ "Alden Bell " goto 1

C:\Users\-\Desktop\ebooks>set yy="Alden Bell"

C:\Users\-\Desktop\ebooks>set y="Alden Bell"

C:\Users\-\Desktop\ebooks>set y="Alden Bell"

C:\Users\-\Desktop\ebooks>if "Alden Bell" NEQ "Alden Bell" goto 1

C:\Users\-\Desktop\ebooks>set copy=copy "Reapers are Angels 02" "Alden Bell"
copy "Reapers are Angels 02" "Alden Bell"
Press any key to continue . . .
C:\Users\-\Desktop\ebooks>(
set copy=copy
call :alltrim " Ape and Essence.epub"
call :alltrim "Aldous Huxley "
echo !copy!
pause
)

C:\Users\-\Desktop\ebooks>set y=" Ape and Essence.epub"

C:\Users\-\Desktop\ebooks>set yy=" Ape and Essence.epub"

C:\Users\-\Desktop\ebooks>set y=" Ape and Essence.epub"

C:\Users\-\Desktop\ebooks>set y="Ape and Essence.epub"

C:\Users\-\Desktop\ebooks>if "Ape and Essence.epub" NEQ " Ape and Essence.epub"
goto 1

C:\Users\-\Desktop\ebooks>set yy="Ape and Essence.epub"

C:\Users\-\Desktop\ebooks>set y="Ape and Essence.epub"

C:\Users\-\Desktop\ebooks>set y="Ape and Essence.epub"

C:\Users\-\Desktop\ebooks>if "Ape and Essence.epub" NEQ "Ape and Essence.epub" g
oto 1

C:\Users\-\Desktop\ebooks>set copy=copy "Ape and Essence.epub"

C:\Users\-\Desktop\ebooks>set y="Aldous Huxley "

C:\Users\-\Desktop\ebooks>set yy="Aldous Huxley "

C:\Users\-\Desktop\ebooks>set y="Aldous Huxley"

C:\Users\-\Desktop\ebooks>set y="Aldous Huxley"

C:\Users\-\Desktop\ebooks>if "Aldous Huxley" NEQ "Aldous Huxley " goto 1

C:\Users\-\Desktop\ebooks>set yy="Aldous Huxley"

C:\Users\-\Desktop\ebooks>set y="Aldous Huxley"

C:\Users\-\Desktop\ebooks>set y="Aldous Huxley"

C:\Users\-\Desktop\ebooks>if "Aldous Huxley" NEQ "Aldous Huxley" goto 1

C:\Users\-\Desktop\ebooks>set copy=copy "Ape and Essence.epub" "Aldous Huxley"
copy "Ape and Essence.epub" "Aldous Huxley"
Press any key to continue . . .


If I press any key it then vanishes.

message edited by Splodgeness


Report •

#17
February 16, 2014 at 17:40:46
Ah, progress at last. The books aren't being copied or moved due to this:
:: remove the "echo" from the next line, and the "pause", if things look right
echo !copy!
pause

You need it to look like this:
!copy!
if you don't remove the pause after !copy!, you'll have to hit ENTER for each book, which could get tedious. You might want to turn echo off to trim the screen-output volume. Make that the first line: @echo off

As for multiple extensions, we can add another loop, or you can just move everything out of the ebooks directory except ebooks. (you can leave the batch in place, it will generate an error but not a critical one). Then you would want the for-loop to look like:

for /f "tokens=1,2* delims=-" %%a in ('dir /b /a-d *.*') do (

As you can see, this will process all the files in the ebooks directory.


Report •

#18
February 17, 2014 at 03:43:19
OK, so now the code I have is:

@echo off
setlocal enabledelayedexpansion
set target=
set /p target=Enter the full path to the working directory:
echo target entered: %target%
pause
if not defined target set target=c:\somewhere
echo tentative: %target%, preparing to CD
cd %target% || (set /p =%target% not accessible&goto :eof)
echo CD was successful to %target%
pause
for /f "tokens=1,2* delims=-" %%a in ('dir /b /a-d *.*') do (
set copy=copy
call :alltrim "%%b"
call :alltrim "%%a"
!copy!
)
goto :eof

:alltrim
set y=%1
:1
set yy=%y%
set y=%y: "="%
set y=%y:" ="%
if %y% neq %yy% goto 1
set copy=%copy% %y%

All files in the folder are ebooks except the batchfile so no problem there.
When I run that I get:

Enter the full path to the working directory:C:\Users\-\Desktop\ebooks
target entered: C:\Users\-\Desktop\ebooks
Press any key to continue . . .
tentative: C:\Users\-\Desktop\ebooks, preparing to CD
CD was successful to C:\Users\-\Desktop\ebooks
Press any key to continue . . .

If I press any key it vanishes and no books are moved, did I do something wrong?


Report •

#19
February 17, 2014 at 20:42:15
Another goof on my part, although I don't know why you get no error "file not found".
Here is a new set of code that fixes the problem. It also attempts to create directories in case they do not happen to be already in existance:
@echo off
setlocal enabledelayedexpansion
:: note: if you're running this from the ebooks directory, you can save time by entering "."
set target=
set /p target=Enter the full path to the working directory:
echo target entered: %target%
pause
if not defined target set target=c:\somewhere
echo tentative: %target%, preparing to CD
cd %target% || (set /p =%target% not accessible&goto :eof)
echo CD was successful to %target%
pause
for /f "tokens=1,2* delims=-" %%a in ('dir /b /a-d *.*') do (
:: TRAILING SPACE REQUIRED IN FOLL LINE!
set copy=copy "%%a-%%b" &::
call :alltrim "%%a" x
set copy=!copy!\
call :alltrim "%%b"
:: i put this back in to show what's trying to happen
echo !copy!
!copy!
rem PAUSE is optional as content of this line, if you want to test
)
goto :eof

:alltrim
set y=%1
:1
set yy=%y%
set y=%y: "="%
set y=%y:" ="%
if %y% neq %yy% goto 1
set copy=%copy%%y%
if "%2" neq "" md %y% 2>nul
::======= end batchscript

Thanks for being patient.

message edited by nbrane


Report •

#20
February 18, 2014 at 02:33:45
Thank you for your patience as well!

I dont know what this means:

":: TRAILING SPACE REQUIRED IN FOLL LINE!"

I ran the new code, there were no errors but the two ebooks in my test folder were not moved. Instead two new folders were created and named with the book titles and in them a blank file with the authors names and no extension. Something is backwards I guess?


Report •

#21
February 18, 2014 at 07:06:36
mine seemed to work fine when
call :alltrim "%%b" x
set copy=!copy!\
call :alltrim "%%a"

was changed to
call :alltrim "%%a" x
set copy=!copy!\
call :alltrim "%%b"

unless I misunderstood, appologies

::mike

message edited by mikelinus


Report •

#22
February 18, 2014 at 08:55:32
Thanks Mike, now that's doing something but instead of moving the files into the folders, it's making new ones there renamed to just the title, (for example: Aldous Huxley - Brave New World.epub becomes: Brave New World.epub) and leaves the old ones behind, I need to keep the existing file names.

message edited by Splodgeness


Report •

#23
February 18, 2014 at 15:44:23
OK, I've fixed it, just changed the line: call :alltrim "%%a" to: call :alltrim "%%a-%%b"

Now copies them into the folders without changing the names:

@echo off
setlocal enabledelayedexpansion
set target=
set /p target=Enter the full path to the working directory:
echo target entered: %target%
pause
if not defined target set target=c:\somewhere
echo tentative: %target%, preparing to CD
cd %target% || (set /p =%target% not accessible&goto :eof)
echo CD was successful to %target%
pause
for /f "tokens=1,2* delims=-" %%a in ('dir /b /a-d *.*') do (
:: TRAILING SPACE REQUIRED IN FOLL LINE!
set copy=copy "%%a-%%b" &::
call :alltrim "%%a" x
set copy=!copy!\
call :alltrim "%%a-%%b"
:: i put this back in to show what's trying to happen
echo !copy!
!copy!
rem PAUSE is optional as content of this line, if you want to test
)
goto :eof

:alltrim
set y=%1
:1
set yy=%y%
set y=%y: "="%
set y=%y:" ="%
if %y% neq %yy% goto 1
set copy=%copy%%y%
if "%2" neq "" md %y% 2>nul
::======= end batchscript

Some of the books also have a series title in the name, (for example: Authors Name - Series Name - Book Title.ext) these are not being moved though, is there a way to include them?

message edited by Splodgeness


Report •

#24
February 18, 2014 at 21:03:07
Yes, I managed to get the order mixed up in the format of the filenames. Also, I misunderstood, thinking you wanted to remove the author from the title once it was moved to the author directory. It appears that you've fixed these mistakes - excellent work! As for the last, maybe changing this line:
for /f "tokens=1,2* delims=-" %%a in ('dir /b /a-d *.*') do (

to this:
for /f "tokens=1,* delims=-" %%a in ('dir /b /a-d *.*') do (


Report •

#25
February 19, 2014 at 04:50:32
That did it, brilliant!

What happens if it has a problem with any of the ebooks? Would be good if it could delete them after copying to the new folder or at least report any errors, at the moment I dont see any way that I'll know.

message edited by Splodgeness


Report •

#26
February 19, 2014 at 14:01:43
✔ Best Answer
Yeh, I'd meant to ask you about that: if you wanted to move the files or copy them. Just change this line:
set copy=copy "%%a-%%b" &::

to this:
set copy=move "%%a-%%b" &::

Any files remaining in the ebooks directory did not get moved, so listing the directory after running the batch will show any ones that failed for whaterver reason. If you are still insecure, you can add this to the line that has !copy!:
!copy! ||>>logfile echo "%%a-%%b" not processed
But that is redundant and unnecessary, generating another unneeded file.


Report •

#27
February 20, 2014 at 02:37:27
After changing that it worked perfect so I ran it, was really cool watching 100,000+ ebooks sort themselves all into folders in a couple of minutes. Thank you everyone.

Report •


Ask Question