Page 1 of 1

Batch Move Problem

Posted: 05 Feb 2021 10:16
by dav1dr0well
I'm a newb when it comes to batch files so I apologize in advance...

I have over 800 folders which contain a number of files including a .txt file. I'm trying to move the .txt file from each of the folders into a single folder called "_Text Versions" (it already exists).

C:\
--->Users
------>David
--------->"Calibre Library"
------------>"Book Summaries"
--------------->(the folder name which is book title)
------------------>*.txt

But when I run my batch file I get no results ;-(

(Here's the .bat file contents)

@echo off
move C:\Users\David\"Calibre Library"\"Book Summaries"\*\*.txt C:\Users\David\"Calibre Library"\"Book Summaries"\"_Text Versions"

Thx in advance,

D

Re: Batch Move Problem

Posted: 05 Feb 2021 11:05
by aGerman
Two things upfront
- It's sufficient and best praxis to enclose the entire path into quotes.
- Wildcards (like asterisk) are allowed in the last element of the path only. C:\foo\*\*.txt won't work. Not even C:\foo\*\bar.txt.

untested:

Code: Select all

for /d %%i in ("%userprofile%\Calibre Library\Book Summaries\*") do (
  move /y "%%~i\*.txt" "%userprofile%\Calibre Library\Book Summaries\_Text Versions\"
)
Note: All text files will be moved into the "_Text Versions" folder. No subfolders are created.

Steffen

Re: Batch Move Problem

Posted: 05 Feb 2021 14:24
by dbenham
That code won't quite work - the "_Text Versions" folder needs to be excluded as a source. And you can use ~dp to shorten the code a bit.

Code: Select all

for /d %%D in ("%userprofile%\Calibre Library\Book Summaries\*") do (
  if "%%~nxD" neq "_Text Versions" move /y "%%~D\*.txt" "%~dpD_Text Versions"
)
or maybe even simpler if you set current directory to the root

Code: Select all

pushd "%userprofile%\Calibre Library\Book Summaries"
for /d %%D in (*) do if "%%D" neq "_Text Versions" move /y "%%D\*.txt" "_Text Versions"
popd
But either way, there will be problems if the same file name appears in multiple locations.

The following will prefix each name with the parent folder name so it is extremely likely there are no collisions

Code: Select all

pushd "%userprofile%\Calibre Library\Book Summaries"
for /d %%D in (*) do if "%%D" neq "_Text Versions" for %%F in ("%%D\*.txt") move /y "%%F" "_Text Versions\%%~nxD__%%~nxF"
popd

Dave Benham