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 31685 times)

0 Members and 1 Guest are viewing this topic.

Dias de verano

  • Guest
Re: Move batch command with no replace if filename exists?
« Reply #30 on: July 30, 2008, 12:14:24 AM »
Is the directory name "L:\!Temp_all" correctly spelled? You said it was called "L:\!Temp all" at first. Are there actually any files in that folder?


gitman7

    Topic Starter


    Rookie

    Re: Move batch command with no replace if filename exists?
    « Reply #31 on: July 30, 2008, 11:01:46 PM »
    Yes it's correctly spelled.  It was originally !Temp all but then I renamed it !Temp_all to make sure there wasn't any issues with there being a space in the folder name.  There are lots of files in the folder.

    Dias de verano

    • Guest
    Re: Move batch command with no replace if filename exists?
    « Reply #32 on: July 31, 2008, 12:29:31 AM »
    I should have realised this before. In batch scripting, ! is a special character and is causing the problem.
    « Last Edit: July 31, 2008, 12:47:56 AM by Dias de verano »

    gitman7

      Topic Starter


      Rookie

      Re: Move batch command with no replace if filename exists?
      « Reply #33 on: July 31, 2008, 01:23:04 AM »
      ah...  I'll have to work on that.  Guess I need to rename the folder again :-)

      gitman7

        Topic Starter


        Rookie

        Re: Move batch command with no replace if filename exists?
        « Reply #34 on: August 04, 2008, 09:55:10 PM »
        That worked!  I just had to remove the ! and rename the directories.  I noticed it doesn't work on any files that have spaces in the filenames though.  Is there a way to fix that or is that a DOS thing you can't get around.  Thanks again for all your help. 

        Dias de verano

        • Guest
        Re: Move batch command with no replace if filename exists?
        « Reply #35 on: August 05, 2008, 12:12:23 AM »
        Post the code you are now using - there are probably some quote marks needed in certain places.

        gitman7

          Topic Starter


          Rookie

          Re: Move batch command with no replace if filename exists?
          « Reply #36 on: August 05, 2008, 01:55:51 PM »
          This is what I'm using:

          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!
                             move "!src!" "!dest!"
                             ) else (
                             echo !dest! already exists
                             )
                       )
              )

          Dias de verano

          • Guest
          Re: Move batch command with no replace if filename exists?
          « Reply #37 on: August 05, 2008, 02:37:40 PM »
          try this

          for /f "delims==" %%F in ('dir /a-d /b L:\Temp_all\%%D*.*') do (

          the delims section should fix the spaces problem

          [update]

          I tested it & it seems to work

          « Last Edit: August 05, 2008, 03:35:46 PM by Dias de verano »

          Dias de verano

          • Guest
          Re: Move batch command with no replace if filename exists?
          « Reply #38 on: August 06, 2008, 12:58:11 AM »
          The reason the previous code did not work with filenames with spaces is that, by default, the FOR /F behaviour is to treat the lines processed (in this case, the lines of text output by DIR /a-d /b) as a series of space-delimited "tokens". That is, if it found the line

          c:\my folder\whatever path\something.txt

          It would treat the lines as having 3 tokens and %%A would only contain the first token

          c:\my

          because FOR /F would stop at the first space.

          This behaviour can be modified with a suitable "delims=" section. I won't go into what "tokens=" does, except to say that you can use it to chop up the line into tokens and isolate them.

          The modifier "delims==" instructs FOR /F to consider the delimiters to be the line start and finish, thus preserving the whole line.

          The problem would have become visible earlier if the path to the source folder had spaces, since these would have cause the batch to not function at all