Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Move batch command with no replace if filename exists?  (Read 31592 times)

0 Members and 1 Guest are viewing this topic.

gitman7

    Topic Starter


    Rookie

    Re: Move batch command with no replace if filename exists?
    « Reply #15 on: July 25, 2008, 03:24:34 PM »
    So does this look right: ?  If my source files are all in "L:\!Temp all"  and the destination folders are all in L:\!TLF\a, etc. with every letter of alphabet in separate folder?

    @echo off
    setlocal enabledelayedexpansion
    for %%D in (! 0-9 a b c d e f g h i j k l m n o p q r s t u v w x y z) do (
        for /f %%F in ('dir /a-d /b L:\!Temp all\%%D*.*') do (
                    set src=L:\!Temp all\%%F
                    set dest=L:\!TLF\%%D\%%F
                    echo Source file: !src!
                    if not exist "!dest!" (
                            echo Moving !src! to !dest!
                       move "!src!" "!dest!"
                       ) else (
                       echo !dest! already exists
                       )
                 )
        )

    Dias de verano

    • Guest
    Re: Move batch command with no replace if filename exists?
    « Reply #16 on: July 25, 2008, 04:56:46 PM »
    explain about the 0-9 folder

    gitman7

      Topic Starter


      Rookie

      Re: Move batch command with no replace if filename exists?
      « Reply #17 on: July 25, 2008, 09:54:50 PM »
      Thats a folder for filenames that begin with a number

      gitman7

        Topic Starter


        Rookie

        Re: Move batch command with no replace if filename exists?
        « Reply #18 on: July 25, 2008, 10:03:11 PM »
        I can skip the files that start with numbers and do those manually.  Would this be right then?

        @echo off
        setlocal enabledelayedexpansion
        for %%D in (a b c d e f g h i j k l m n o p q r s t u v w x y z) do (
            for /f %%F in ('dir /a-d /b L:\!Temp all\%%D*.*') do (
                        set src=L:\!Temp all\%%F
                        set dest=L:\!TLF\%%D\%%F
                        echo Source file: !src!
                        if not exist "!dest!" (
                                echo Moving !src! to !dest!
                           move "!src!" "!dest!"
                           ) else (
                           echo !dest! already exists
                           )
                     )
            )

        Dias de verano

        • Guest
        Re: Move batch command with no replace if filename exists?
        « Reply #19 on: July 25, 2008, 11:41:50 PM »
        I can skip the files that start with numbers and do those manually.  Would this be right then?

        Since the folder name L:\!Temp all has a space in it, it needs to be quoted thus

        for /f %%F in ('dir /a-d /b "L:\!Temp all"\%%D*.*') do (

        Otherwise it seems OK. You could try it out with some dummy data or you could change this line

        move "!src!" "!dest!"

        to this

        echo  move "!src!" "!dest!"

        & run the batch to see what happens & change it back when you are happy.

        I note that the ! filenames got removed too...



         

        gitman7

          Topic Starter


          Rookie

          Re: Move batch command with no replace if filename exists?
          « Reply #20 on: July 25, 2008, 11:58:36 PM »
          Wow you rock!  Thanks man.  I tried a few different forums and lots of web pages - I even got all my old books out - one was DOS 3.20 for Blue Chip computer from 1984!  lol  Didn't even have the move command but had xcopy and a few other things.  I still don't understand the whole thing but I think I got most of it. This is the best forum. 

          Thanks again - I'm gonna try the echo thing and see how it does.  Tomorrow :-)  Time for bed.

          Dias de verano

          • Guest
          Re: Move batch command with no replace if filename exists?
          « Reply #21 on: July 26, 2008, 12:10:59 AM »
          sleep well

          Code: [Select]
          @echo off
          setlocal enabledelayedexpansion
          for %%D in (! a b c d e f g h i j k l m n o p q r s t u v w x y z) do (
              for /f %%F in ('dir /a-d /b "L:\!Temp all"\%%D*.*') do (
                          set src=L:\!Temp all\%%F
                          set dest=L:\!TLF\%%D\%%F
                          echo Source file: !src!
                          if not exist "!dest!" (
                                  echo Moving !src! to !dest!
                             move "!src!" "!dest!"
                             ) else (
                             echo !dest! already exists
                             )
                       )
              )


          for %%D in (0 1 2 3 4 5 6 7 8 9) do (
              for /f %%F in ('dir /a-d /b "L:\!Temp all"\%%D*.*') do (
                          set src=L:\!Temp all\%%F
                          set dest=L:\!TLF\0-9
                          echo Source file: !src!
                          if not exist "!dest!" (
                                  echo Moving !src! to !dest!
                             move "!src!" "!dest!"
                             ) else (
                             echo !dest! already exists
                             )
                       )
              )

          gitman7

            Topic Starter


            Rookie

            Re: Move batch command with no replace if filename exists?
            « Reply #22 on: July 28, 2008, 08:45:52 AM »
            When I try running these I get the error:  The system cannot find the file specified 

            Can't figure out what's wrong

            gitman7

              Topic Starter


              Rookie

              Re: Move batch command with no replace if filename exists?
              « Reply #23 on: July 28, 2008, 08:56:02 AM »
              I even tried renaming the directory !Temp all to !Temp_all to remove the space and changed the bat file to this:  (just removed the quotes)

              @echo off
              setlocal enabledelayedexpansion
              for %%D in (! a b c d e f g h i j k l m n o p q r s t u v w x y z) do (
                  for /f %%F in ('dir /a-d /b L:\!Temp_all\%%D*.*') do (
                              set src=L:\!Temp_all\%%F
                              set dest=L:\!TLF\%%D\%%F
                              echo Source file: !src!
                              if not exist "!dest!" (
                                      echo Moving !src! to !dest!
                                 echo move "!src!" "!dest!"
                                 ) else (
                                 echo !dest! already exists
                                 )
                           )
                  )

              But I still get the error.

              The source and destination directories are correct.  I'm not sure how the %%D and %%F are supposed to work.  Or the letter and number folders in the parenthesis - I even tried putting this .bat file in the L:\!Temp_all folder to see if that helped but same error.  TIA any more help :-)

              Dias de verano

              • Guest
              Re: Move batch command with no replace if filename exists?
              « Reply #24 on: July 28, 2008, 10:43:13 AM »
              do you see the "source file: !src!" message?

              gitman7

                Topic Starter


                Rookie

                Re: Move batch command with no replace if filename exists?
                « Reply #25 on: July 29, 2008, 02:25:06 PM »
                No only message I see is that the file can't be found and it repeats a number of times then the window closes

                Dias de verano

                • Guest
                Re: Move batch command with no replace if filename exists?
                « Reply #26 on: July 29, 2008, 02:36:03 PM »
                the ! symbol has a special meaning if delayed expasion is enabled.

                Try changing these lines thus

                Code: [Select]
                set src="L:\!Temp_all\%%F"
                set dest="L:\!TLF\%%D\%%F"

                gitman7

                  Topic Starter


                  Rookie

                  Re: Move batch command with no replace if filename exists?
                  « Reply #27 on: July 29, 2008, 03:23:43 PM »
                  Still doesn't work.  This is what I have now:    (I put the echo in too to test)

                  @echo off
                  setlocal enabledelayedexpansion
                  for %%D in (! a b c d e f g h i j k l m n o p q r s t u v w x y z) do (
                      for /f %%F in ('dir /a-d /b L:\!Temp_all\%%D*.*') do (
                                  set src="L:\!Temp_all\%%F"
                                  set dest="L:\!TLF\%%D\%%F"
                                  echo Source file: !src!
                                  if not exist "!dest!" (
                                          echo Moving !src! to !dest!
                                     echo move "!src!" "!dest!"
                                     ) else (
                                     echo !dest! already exists
                                     )
                               )
                      )

                  Dias de verano

                  • Guest
                  Re: Move batch command with no replace if filename exists?
                  « Reply #28 on: July 29, 2008, 03:31:32 PM »
                  Try this

                  Please state what is reported by the echo lines

                  Code: [Select]
                  @echo off
                  setlocal enabledelayedexpansion
                  for %%D in (a b c d e f g h i j k l m n o p q r s t u v w x y z) do (
                      for /f %%F in ('dir /a-d /b L:\!Temp_all\%%D*.*') do (
                                 
                                  set src="L:\!Temp_all\%%~nxF"
                                  set dest="L:\!TLF\%%D\%%~nxF"
                                  echo File name : %%F
                                  echo Source file: !src!
                                  echo Dest file    : !dest!
                                  if not exist "!dest!" (
                                          echo Moving !src! to !dest!
                                     echo move "!src!" "!dest!"
                                     ) else (
                                     echo !dest! already exists
                                     )
                               )
                      )

                  gitman7

                    Topic Starter


                    Rookie

                    Re: Move batch command with no replace if filename exists?
                    « Reply #29 on: July 29, 2008, 04:09:03 PM »
                    The system cannot find the file specified.    - that's all it says about 20 times then quits