Recursive copy files from folders and rename (folder name)

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
BinaryBen
Posts: 7
Joined: 04 Aug 2014 10:48

Recursive copy files from folders and rename (folder name)

#1 Post by BinaryBen » 04 Aug 2014 10:53

Hi all,
this might seem easy - or maybe it can't be done -

however, I'm trying to write a small bat script, that copies all the file types in the current sub folders and renames them based on the 'top' folders name:

./cats/cata/file.bmp
./cats/catb/file.bmp
./cats/catc/file.bmp
./cats/grab.bat


by grab.bat will loop over the current folders and get all the .bmp files, rename them to the top directory (e.g., 'cata.bmp') and put them in a folder, so I get (cata.bmp, catb.bmp, catc.bmp.

I've been trying to get it to work, but can't get the renaming to just the folder type

Thanx,

Ben

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Recursive copy files from folders and rename (folder nam

#2 Post by foxidrive » 04 Aug 2014 10:59

What have you tried so far?

And how come you are using Unix slashes in a Windows file path?

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Recursive copy files from folders and rename (folder nam

#3 Post by penpen » 04 Aug 2014 16:04

@BinaryBen
How do you want to handle multimaps to one name
.\cats\cata\file.bmp -?-> .\cats\cata.bmp
.\cats\cata\file2.bmp -?-> .\cats\cata.bmp (in use)

@foxidrive
Since WinXP these are no more pure "Unix slashes", as it is (rudimentary) supported by some cmd.exe commands, too:

Code: Select all

Z:\>if exist ./test/add echo works
works

Z:\>cd ./test/add

Z:\test\add>cd/ & rem <-- doesn't work as expected you have to use: cd\

Z:\test\add>cd ..

Z:\test>cd /test

Z:\test\test>cd /test

Z:\test\test>cd\

Z:\>@for %a in (./test/add/*) do @echo %a
a1052.txt
Eingabeaufforderung.lnk
test.bat
test.txt
debug.txt

Z:\>cd /test    /test & rem <-- some strange behavior (actually i don't know if unix supports something like that)

Z:\test\test>
And if i remember right it was possible when programming in c (using an old Borland compiler) under MS-DOS 6.22 to change the default path component seperator character,
so i assume it was not hardcoded in MS-DOS, but some kind of default value.
(But it also could be some internal feature of the Borland C compiler.)

penpen

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Recursive copy files from folders and rename (folder nam

#4 Post by foxidrive » 04 Aug 2014 16:50

penpen wrote:@foxidrive
Since WinXP these are no more pure "Unix slashes", as it is (rudimentary) supported by some cmd.exe commands, too:


That's right penpen, but not all commands support forward slashes so it's not a good practice.

BinaryBen
Posts: 7
Joined: 04 Aug 2014 10:48

Re: Recursive copy files from folders and rename (folder nam

#5 Post by BinaryBen » 05 Aug 2014 03:24

Thanx guys.
My example was to illustrate the issue (i.e., should be "/"). I was tired of fiddling with the bat script and wanted to give a simple example.

I thought I could put a bat file in a folder, and loop over all subfolders and find '.pdf' files and copy them to the location of the .bat (but rename the .pdf to the name of the folder they're from)

set fType=*.pdf
for /f "delims=" %%f in ('dir /a-d /b /s "\%fType%"') do (
copy /V "%%f" "%???%\"
)
?? was issue of copying and renaming I couldn't get to work :-/

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Recursive copy files from folders and rename (folder nam

#6 Post by foxidrive » 05 Aug 2014 04:07

This will display copy commands on the screen for you to check.
If they are right then remove the echo before the copy and run it for real.

It will copy the PDF files to the same folder that the batch file is in, and recurse through the folders below that same folder.

Code: Select all

@echo off
set ftype=*.pdf
for /f "delims=" %%a in ('dir /a-d /b /s "%ftype%" ') do (
for /f "delims=" %%b in ("%%~dpa\.") do echo copy "%%a" "%%~nxb%%~xa"
)
pause

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Recursive copy files from folders and rename (folder nam

#7 Post by Compo » 05 Aug 2014 09:49

grab.bat

Code: Select all

@Echo Off & SetLocal EnableExtensions DisableDelayedExpansion
Set _ext=.pdf
PushD %~dp0
For /D %%a In (*) Do If Exist "%%a\*%_ext%" Call :Sub "%%a"
Exit/B

:Sub
Set _cnt=0
For %%a In ("%~1\*%_ext%") Do Set/A _cnt+=1
If %_cnt% NEq 1 GoTo :EOF
If Not Exist "%~1%_ext%" Copy "%~1\*%_ext%" "%~1%_ext%">Nul

BinaryBen
Posts: 7
Joined: 04 Aug 2014 10:48

Re: Recursive copy files from folders and rename (folder nam

#8 Post by BinaryBen » 06 Aug 2014 03:19

Fantastic Compo!!

It's perfect!!! Just what I wanted!!

Thanx

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Recursive copy files from folders and rename (folder nam

#9 Post by Squashman » 06 Aug 2014 07:29

BinaryBen wrote:Fantastic Compo!!

It's perfect!!! Just what I wanted!!

Thanx

Did you test Foxidrive's solution?

BinaryBen
Posts: 7
Joined: 04 Aug 2014 10:48

Re: Recursive copy files from folders and rename (folder nam

#10 Post by BinaryBen » 06 Aug 2014 09:34

Awwww.... I tested Compo solution out and I thought it worked - however, the copied/renamed pdf files are all corrupted :(

Doesn't seem to copy them right :-/

Going to test foxidrive's solution now :)

BinaryBen
Posts: 7
Joined: 04 Aug 2014 10:48

Re: Recursive copy files from folders and rename (folder nam

#11 Post by BinaryBen » 06 Aug 2014 09:38

Yup, foxidrive's solution works - so I'm going with this.

Great, thanx Foxidrive

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Recursive copy files from folders and rename (folder nam

#12 Post by foxidrive » 06 Aug 2014 10:27

BinaryBen wrote:Yup, foxidrive's solution works - so I'm going with this.


Just be aware that if there is more than one PDF file in a folder then it will be an issue.

Thanks Squashman.

BinaryBen
Posts: 7
Joined: 04 Aug 2014 10:48

Re: Recursive copy files from folders and rename (folder nam

#13 Post by BinaryBen » 07 Aug 2014 03:41

That's fine foxidrive - it's just what I needed.

Thanx again

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Recursive copy files from folders and rename (folder nam

#14 Post by Compo » 07 Aug 2014 12:07

Also bear in mind that both foxidrive's and my versions are using COPY in the same way and there is no reason I can see why either one would cause corruption of your files.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Recursive copy files from folders and rename (folder nam

#15 Post by foxidrive » 07 Aug 2014 18:58

Compo wrote:here is no reason I can see why either one would cause corruption of your files.


Yours copies using a wildcard and it adds an EOF marker in my test.

Post Reply