Batch Move Problem

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
dav1dr0well
Posts: 1
Joined: 05 Feb 2021 09:56

Batch Move Problem

#1 Post by dav1dr0well » 05 Feb 2021 10:16

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

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Batch Move Problem

#2 Post by aGerman » 05 Feb 2021 11:05

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

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Batch Move Problem

#3 Post by dbenham » 05 Feb 2021 14:24

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

Post Reply