PDA

View Full Version : Batch File-Copy/Rename



Skippybox
25.09.2008, 11:32 PM
Someone gave me this batch file so I could copy the contents of folders, but it doesn't seem to recognize file names with spaces. Can anyone help correct it? Thanks.


@echo off
set Source=d:\New
for /F %%a in ('dir /ad /b "%Source%"') do call :Sub %%a
goto :eof

:Sub
for /F %%b in ('dir /a-d /b "%Source%\%*\*.*"') do ^
echo copy /y "%Source%\%*\%%b" "%Source%\%*\Copy of %%b"
pause

Skippybox
25.09.2008, 11:43 PM
Would this be valid?


@echo off
set Source=d:\New
for /F %%a in ('dir /ad /b "%Source%"') do call :Sub %%a
goto :eof

:Sub
for /F "tokens=* delims= " %%b in ('dir /a-d /b "%Source%\%*\*.*"') do ^
echo copy /y "%Source%\%*\%%b" "%Source%\%*\Copy of %%b"
pause

How might I copy all the subdirectories in the Source, not just one level down?

derniwi
26.09.2008, 08:21 AM
Hi Skippybox,

what about xcopy.exe?
I think that would be better.

Your batch file has at least two problems:

for /F %%a in ('dir /ad /b "%Source%"') do call :Sub %%a

This would give you the left part of the directories, if they contain a blank.


for /F "tokens=* delims= " %%b in ('dir /a-d /b "%Source%\%*\*.*"') do ^

This code is a little bit better. But it won't work if your file or directory contains two blanks in follow.

I think it would be better if you try this (if you don't like xcopy):



@echo off
set Source=d:\New
for /F "tokens=* delims=" %%a in ('dir /ad /b /s "%Source%"') do call :Sub "%%a"
goto :eof

:Sub
for /F "tokens=* delims=" %%b in ('dir /a-d /b "%~1\*.*"') do ^
echo copy /y "%~1\%%b" "%~1\Copy of %%b"
pause
goto :eof

In the third line the delimiter is set to nothing so the variable %%a will contain the whole line. Using "dir /b /s" will give you the complete path for all subdirectories, so you don't have to use "%source%\%*....".

The call is changed to "%%a" so the variable is enclosed by double qoutes. This will ensure that only one variable is submitted, and multiple blanks shall work.

The "%*" would give you all parameters separated by one blank. I'm not sure if this will work if you have a directory or file with more than one blank in follow. So it is better to know exactly what you submit, therefore I'll changed to "%~1", which is the first parameter given to the subroutine, but the "~" will remove the enclosing quotes.

I haven't tested this script, but I think this sould work. :rolleyes:

Regards, Nils.

Skippybox
26.09.2008, 04:02 PM
Excellent, as always Nils!

I would use xcopy, but would it really be easier than this script if I want the copied files to be named Copy of ...?

derniwi
26.09.2008, 10:11 PM
Hi Skippybox,

in your case xcopy won't be easier. :o

Greets, Nils.

Skippybox
26.09.2008, 11:05 PM
Greetings Nils,

Thanks for letting me know.

Sam_Zen
27.09.2008, 12:04 AM
File names with spaces .. yeghh ..
To avoid trouble, better replaced by underscores.

Skippybox
29.09.2008, 09:04 PM
File names with spaces .. yeghh ..
To avoid trouble, better replaced by underscores.

As in your name. Good advice, but unfortunately not everyone will, so you have to brace for that.

derniwi
30.09.2008, 10:24 PM
@Sam_Zen: we are all working on Software, that will made it easier to work with a computer in a human way, so we don't need to learn thinking in a computer way... ;) and a blank in file names is something a computer should deal with, also with file names longer than eight characters... :rolleyes:

Sam_Zen
01.10.2008, 02:27 AM
I've had a lot of discussions about this on several software-forums, so I know these arguments.
Including this 'eight characters' argument, suggesting that someone is an old fool, hanging on to the DOS days.

1) I'm not promoting thinking in a computer way. But a computer is a tool, so it's handy to know and adapt to some properties.
The same reason why nobody would try to drive away in a car using the 5th gear. It's asking for malfunction.

2) A blank in a filename on a local system goes fine most of the time. But filenames often are on the Inet.
On the net things are interpreted by UNIX. And I just don't like to see all those %20 in the URL.

derniwi
05.10.2008, 10:30 PM
Hi Sam,

yes, I agree for the moment. But I believe the real next generation of Internet will handle blanks much better. ;)

Regards, Nils.

boarder's paradise
06.12.2008, 05:05 AM
And have you solved this one already, Skippyboy, or are you still in need ?
Skip-reading the issue tells me you would need
delims="
instead of
delims= "

Skippybox
07.12.2008, 04:15 AM
That's what Nils said. Thanks though. This simple script has been very useful in so many places.

boarder's paradise
07.12.2008, 08:29 AM
so it's working for you now, problem solved ?

Skippybox
09.12.2008, 11:22 PM
Yes, this problem was solved since September. I can see the benefits of the prefixes you described prior to show something was solved. Course, it's always nice to still get posts afterwards if anything else develops. This was actually meant for someone else (again!), but I have used it myself and others, so everyone benefits.

boarder's paradise
10.12.2008, 03:44 AM
Hi. I don't know what "prefixes" you are talking about, but don't bother explaining, if everything's fine for everyone, we should happily leave it at that :o

Skippybox
10.12.2008, 11:40 PM
This post (http://en.irfanview-forum.de/vb/showpost.php?p=16297&postcount=8).