Page 1 of 2

Recursive copy files from folders and rename (folder name)

Posted: 04 Aug 2014 10:53
by BinaryBen
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

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

Posted: 04 Aug 2014 10:59
by foxidrive
What have you tried so far?

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

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

Posted: 04 Aug 2014 16:04
by penpen
@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

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

Posted: 04 Aug 2014 16:50
by foxidrive
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.

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

Posted: 05 Aug 2014 03:24
by BinaryBen
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 :-/

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

Posted: 05 Aug 2014 04:07
by foxidrive
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

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

Posted: 05 Aug 2014 09:49
by Compo
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

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

Posted: 06 Aug 2014 03:19
by BinaryBen
Fantastic Compo!!

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

Thanx

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

Posted: 06 Aug 2014 07:29
by Squashman
BinaryBen wrote:Fantastic Compo!!

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

Thanx

Did you test Foxidrive's solution?

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

Posted: 06 Aug 2014 09:34
by BinaryBen
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 :)

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

Posted: 06 Aug 2014 09:38
by BinaryBen
Yup, foxidrive's solution works - so I'm going with this.

Great, thanx Foxidrive

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

Posted: 06 Aug 2014 10:27
by foxidrive
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.

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

Posted: 07 Aug 2014 03:41
by BinaryBen
That's fine foxidrive - it's just what I needed.

Thanx again

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

Posted: 07 Aug 2014 12:07
by Compo
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.

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

Posted: 07 Aug 2014 18:58
by foxidrive
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.