+ Auf Thema antworten
Seite 1 von 2 1 2 LetzteLetzte
Zeige Ergebnis 1 bis 10 von 17

Thema: Batch File-Copy/Rename

  1. #1
    Professional User Skippybox befindet sich auf einem aufstrebenden Ast Benutzerbild von Skippybox
    Registriert seit
    Jul 2008
    Ort
    Detroit, MI USA
    Beiträge
    2.034
    Renommee-Modifikator
    5

    Question Batch File-Copy/Rename

    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.

    Code:
    @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

  2. #2
    Professional User Skippybox befindet sich auf einem aufstrebenden Ast Benutzerbild von Skippybox
    Registriert seit
    Jul 2008
    Ort
    Detroit, MI USA
    Beiträge
    2.034
    Renommee-Modifikator
    5

    Standard

    Would this be valid?

    Code:
    @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?
    Geändert von Skippybox (25.09.2008 um 23:09 Uhr)

  3. #3
    Moderator derniwi befindet sich auf einem aufstrebenden Ast
    Registriert seit
    Aug 2007
    Ort
    Germany
    Beiträge
    312
    Renommee-Modifikator
    4

    Standard

    Hi Skippybox,

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

    Your batch file has at least two problems:
    Code:
    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.

    Code:
    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):

    Code:
    @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.

    Regards, Nils.

  4. #4
    Professional User Skippybox befindet sich auf einem aufstrebenden Ast Benutzerbild von Skippybox
    Registriert seit
    Jul 2008
    Ort
    Detroit, MI USA
    Beiträge
    2.034
    Renommee-Modifikator
    5

    Standard

    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 ...?

  5. #5
    Moderator derniwi befindet sich auf einem aufstrebenden Ast
    Registriert seit
    Aug 2007
    Ort
    Germany
    Beiträge
    312
    Renommee-Modifikator
    4

    Standard

    Hi Skippybox,

    in your case xcopy won't be easier.

    Greets, Nils.

  6. #6
    Professional User Skippybox befindet sich auf einem aufstrebenden Ast Benutzerbild von Skippybox
    Registriert seit
    Jul 2008
    Ort
    Detroit, MI USA
    Beiträge
    2.034
    Renommee-Modifikator
    5

    Standard

    Greetings Nils,

    Thanks for letting me know.

  7. #7
    Moderator Sam_Zen befindet sich auf einem aufstrebenden Ast Benutzerbild von Sam_Zen
    Registriert seit
    May 2007
    Ort
    NL
    Beiträge
    1.908
    Renommee-Modifikator
    5

    Standard

    File names with spaces .. yeghh ..
    To avoid trouble, better replaced by underscores.
    0.6180339887

  8. #8
    Professional User Skippybox befindet sich auf einem aufstrebenden Ast Benutzerbild von Skippybox
    Registriert seit
    Jul 2008
    Ort
    Detroit, MI USA
    Beiträge
    2.034
    Renommee-Modifikator
    5

    Standard

    Zitat Zitat von Sam_Zen Beitrag anzeigen
    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.

  9. #9
    Moderator derniwi befindet sich auf einem aufstrebenden Ast
    Registriert seit
    Aug 2007
    Ort
    Germany
    Beiträge
    312
    Renommee-Modifikator
    4

    Standard

    @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...

  10. #10
    Moderator Sam_Zen befindet sich auf einem aufstrebenden Ast Benutzerbild von Sam_Zen
    Registriert seit
    May 2007
    Ort
    NL
    Beiträge
    1.908
    Renommee-Modifikator
    5

    Standard

    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.
    0.6180339887

+ Auf Thema antworten

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Forumregeln

  • Es ist dir nicht erlaubt, neue Themen zu verfassen.
  • Es ist dir nicht erlaubt, auf Beiträge zu antworten.
  • Es ist dir nicht erlaubt, Anhänge hochzuladen.
  • Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.