Announcement

Collapse
No announcement yet.

Batch File-Copy/Rename

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    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
    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?
    Last edited by Skippybox; 26.09.2008, 12:49 AM.

    Comment


      #3
      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.

      Comment


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

        Comment


          #5
          Hi Skippybox,

          in your case xcopy won't be easier.

          Greets, Nils.

          Comment


            #6
            Greetings Nils,

            Thanks for letting me know.

            Comment


              #7
              File names with spaces .. yeghh ..
              To avoid trouble, better replaced by underscores.
              0.6180339887
              Rest In Peace, Sam!

              Comment


                #8
                Originally posted by Sam_Zen View Post
                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.

                Comment


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

                  Comment


                    #10
                    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
                    Rest In Peace, Sam!

                    Comment


                      #11
                      Hi Sam,

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

                      Regards, Nils.

                      Comment


                        #12
                        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= "

                        Comment


                          #13
                          That's what Nils said. Thanks though. This simple script has been very useful in so many places.

                          Comment


                            #14
                            so it's working for you now, problem solved ?

                            Comment


                              #15
                              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.

                              Comment

                              Working...
                              X