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

Author Topic: multiple renaming files  (Read 5801 times)

0 Members and 1 Guest are viewing this topic.

Blisk

    Topic Starter


    Intermediate

    Thanked: 1
    • Experience: Familiar
    • OS: Windows 7
    multiple renaming files
    « on: December 21, 2018, 02:36:32 PM »
    Can someone help me rename multiple files.
    I have about 50 folders with subfolders and need a batch which will rename file if it finds another file in folder
    So I need script to find file exist.txt and if that file exist than script should rename not_copied.txt to copied.txt
    like belov

    d:\myfolder\archive\john\documents\exist.txt
    d:\myfolder\archive\john\documents\today\not_copied.txt
    renamed to
    d:\myfolder\archive\john\documents\today\copied.txt

    do nothing
    d:\myfolder\archive\george\documents\
    d:\myfolder\archive\george\documents\today\not_copied.txt


    d:\myfolder\archive\nina\documents\exist.txt
    d:\myfolder\archive\nina\documents\today\not_copied.txt
    rename to
    d:\myfolder\archive\nina\documents\today\copied.txt

    do nothing
    d:\myfolder\archive\peter\documents\
    d:\myfolder\archive\peter\documents\today\not_copied.txt

    do nothing
    d:\myfolder\archive\gil\documents\
    d:\myfolder\archive\gil\documents\today\not_copied.txt

    I will run that batch in folder d:\myfolder

    Salmon Trout

    • Guest
    Re: multiple renaming files
    « Reply #1 on: December 21, 2018, 04:39:40 PM »
    I'm kinda wondering... are John, George, Nina, Peter, and Gil real people?

    Blisk

      Topic Starter


      Intermediate

      Thanked: 1
      • Experience: Familiar
      • OS: Windows 7
      Re: multiple renaming files
      « Reply #2 on: December 22, 2018, 01:22:41 AM »
      yes they are real people and they have real documents

      Salmon Trout

      • Guest
      Re: multiple renaming files
      « Reply #3 on: December 22, 2018, 01:53:23 AM »
      Just kidding... under d:\myfolder\archive there could be any number of folders called [person] with a folder called \documents underneath?

      Salmon Trout

      • Guest
      Re: multiple renaming files
      « Reply #4 on: December 22, 2018, 02:13:20 AM »
      Questions: For each of these persons (or whatever)...
      Does the \documents\today folder always exist?
      Does the \documents\today folder always have a file called not_copied.txt in it?
      If it does, is there never a file called copied.txt in that folder?



      Blisk

        Topic Starter


        Intermediate

        Thanked: 1
        • Experience: Familiar
        • OS: Windows 7
        Re: multiple renaming files
        « Reply #5 on: December 22, 2018, 02:58:30 AM »
        yes all exist.
        Also exist copied.txt in some folders and that file is deleted once per week and it is created again after some other batch is finished.

        Salmon Trout

        • Guest
        Re: multiple renaming files
        « Reply #6 on: December 22, 2018, 03:58:12 AM »
        If copied.txt exists already, what should happen to it?

        Blisk

          Topic Starter


          Intermediate

          Thanked: 1
          • Experience: Familiar
          • OS: Windows 7
          Re: multiple renaming files
          « Reply #7 on: December 22, 2018, 06:29:40 AM »
          If copied.txt exists already, what should happen to it?

          nothing.
          copied.txt should stay as it is, but not_copied.txt should rename to copied.txt

          Salmon Trout

          • Guest
          Re: multiple renaming files
          « Reply #8 on: December 22, 2018, 07:09:02 AM »
          nothing.
          copied.txt should stay as it is, but not_copied.txt should rename to copied.txt


          Rename will fail because you cannot have two files in a folder with the same name.

          Blisk

            Topic Starter


            Intermediate

            Thanked: 1
            • Experience: Familiar
            • OS: Windows 7
            Re: multiple renaming files
            « Reply #9 on: December 22, 2018, 07:20:33 AM »
            Rename will fail because you cannot have two files in a folder with the same name.
            there is no two files in folder with same name, it is only one file.

            Salmon Trout

            • Guest
            Re: multiple renaming files
            « Reply #10 on: December 22, 2018, 07:31:11 AM »
            Your original question seeks to do this:

            Quote
            d:\myfolder\archive\john\documents\today\not_copied.txt
            renamed to
            d:\myfolder\archive\john\documents\today\copied.txt
             

            That is, not_copied.txt renamed to copied.txt in that folder.

            I asked:

            Quote
            If copied.txt exists already, what should happen to it?

            I clearly meant "if d:\myfolder\archive\john\documents\today\copied.txt already exists in that folder, what should happen to it?"

            One of us is confused, which one?

            Blisk

              Topic Starter


              Intermediate

              Thanked: 1
              • Experience: Familiar
              • OS: Windows 7
              Re: multiple renaming files
              « Reply #11 on: December 22, 2018, 10:37:46 AM »
              if that file exist than it should leave it
              if there exist not_copied.txt it should rename it if laso file exist.txt exist.

              Salmon Trout

              • Guest
              Re: multiple renaming files
              « Reply #12 on: December 22, 2018, 12:17:29 PM »
              You cannot have 2 files called copied.txt in the same folder.

              If copied.txt already exists in the same folder, what new name should not_copied.txt have? And what if that new name already exists etc?

              Are these really the names, or just placeholders used in your question?

              I have a script which looks OK, but I shall not post it here unless you address this.

              I will soon click "unnotify", so please answer if you want any more help.
              « Last Edit: December 22, 2018, 12:28:47 PM by Salmon Trout »

              Squashman



                Specialist
              • Thanked: 134
              • Experience: Experienced
              • OS: Other
              Re: multiple renaming files
              « Reply #13 on: December 22, 2018, 10:11:06 PM »
              Your original question seeks to do this:

              That is, not_copied.txt renamed to copied.txt in that folder.

              I asked:

              I clearly meant "if d:\myfolder\archive\john\documents\today\copied.txt already exists in that folder, what should happen to it?"

              One of us is confused, which one?
              Did they edit their question at some point because I see this.

              d:\myfolder\archive\nina\documents\exist.txt
              d:\myfolder\archive\nina\documents\today\not_copied.txt

              Salmon Trout

              • Guest
              Re: multiple renaming files
              « Reply #14 on: December 23, 2018, 02:07:01 AM »
              Did they edit their question at some point because I see this.

              d:\myfolder\archive\nina\documents\exist.txt
              d:\myfolder\archive\nina\documents\today\not_copied.txt

              That was not edited; that is not the problem. The requirement appears to be this:

              Quote
              d:\myfolder\archive\nina\documents\exist.txt
              d:\myfolder\archive\nina\documents\today\not_copied.txt
              rename to
              d:\myfolder\archive\nina\documents\today\copied.txt

              See? Same folder for copied.txt (the desired end result) I guess this could be an error.

              That is

              Look in folder $NAME\documents for a file called exists.txt
                  If it is found:
                  Look in folder $NAME\documents\today for a file called not_copied.txt
                       If that is found:
                       Rename it to copied_txt

              I appear to have set the cat among the pigeons by asking innocently "What happens if copied.txt already exists?".

              Looking behind the code for a moment, I am wondering if the desired end result is to copy or move not_copied.txt to another folder as copied.txt. That is, conceptually, the names copied and not_copied clash with the idea of 'rename'. Copy duplicates a file; rename leaves it in place. The question of what to do if the desired destination file place/name already exists still remains, though.

              Blisk

                Topic Starter


                Intermediate

                Thanked: 1
                • Experience: Familiar
                • OS: Windows 7
                Re: multiple renaming files
                « Reply #15 on: December 23, 2018, 03:03:37 AM »
                Sorry still don't know where is a problem.
                If script found file copied.txt it will skip it and go further.
                If that is a problem for script than it should delete copied.txt and create it again.

                Salmon Trout

                • Guest
                Re: multiple renaming files
                « Reply #16 on: December 23, 2018, 04:25:04 AM »
                So...
                IF this file is found:
                d:\myfolder\archive\NAME\documents\exist.txt
                THEN
                This file (if it exists):
                d:\myfolder\archive\NAME\documents\today\not_copied.txt
                SHALL BE RENAMED TO
                d:\myfolder\archive\NAME\documents\today\copied.txt
                go to next NAME

                BUT IF d:\myfolder\archive\NAME\documents\today\copied.txt ALREADY EXISTS...
                don't do anything; leave not.copied.txt alone; leave copied.txt alone,
                go to next NAME
                Is that right?

                Blisk

                  Topic Starter


                  Intermediate

                  Thanked: 1
                  • Experience: Familiar
                  • OS: Windows 7
                  Re: multiple renaming files
                  « Reply #17 on: December 23, 2018, 04:51:05 AM »
                  BUT IF d:\myfolder\archive\NAME\documents\today\copied.txt ALREADY EXISTS...
                  don't do anything; leave not.copied.txt alone; leave copied.txt alone,
                  go to next NAME
                  Is that right?

                  no, there should never exist not_copied.txt and copied.txt both the same time in folder
                  d:\myfolder\archive\NAME\documents\today
                  is should exist only one file, not_copied.txt which will be renamed to copied.txt
                  or copied.txt which should be as it is.

                  Salmon Trout

                  • Guest
                  Re: multiple renaming files
                  « Reply #18 on: December 23, 2018, 06:23:03 AM »
                  This may work for you... perhaps you can try it...

                  Batch to be run in D:\Myfolder

                  In my test setup, folders under "d:\myfolder\archive" are:

                  Bill
                  George
                  Gil
                  Joe
                  John
                  Mary
                  Mike
                  Nina
                  Peter
                  Sam
                  Steve

                  Of these,

                  Bill, Mike, Peter, do NOT have an exist.txt in their folder d:\myfolder\Archive\NAME\documents\

                  The others all do

                  all NAMEs have a not_copied.txt in folder d:\myfolder\Archive\NAME\documents\today\

                  I suggest you run the batch on a COPY of your data until you are happy that it does what you want!!!

                  @echo off
                  setlocal enabledelayedexpansion
                  set "thisdir=%cd%"
                  for /f "delims=" %%A in ('dir archive /b /ad') do (
                      set NAME=%%A
                      REM echo Checking archive\!NAME!
                      if exist "Archive\!NAME!\documents\exist.txt" (
                      echo Archive\!NAME!\documents\documents\exist.txt FOUND
                      if exist "Archive\!NAME!\documents\today\not_copied.txt" (   
                         echo Archive\!NAME!\documents\today\not_copied.txt FOUND
                         echo RENAME to copied.txt
                              ren "archive\!NAME!\documents\today\not_copied.txt" "copied.txt" && echo Renamed OK
                              ) else (
                              echo Archive\!NAME!\documents\today\not_copied.txt NOT FOUND
                          )
                          ) else (
                          echo Archive\!NAME!\documents%%A\documents\exist.txt NOT FOUND
                      )
                      echo.
                  )



                  Here is an example of the batch console output:

                  Archive\Bill\documentsBill\documents\exist.txt NOT FOUND

                  Archive\George\documents\documents\exist.txt FOUND
                  Archive\George\documents\today\not_copied.txt FOUND
                  RENAME to copied.txt
                  Renamed OK

                  Archive\Gil\documents\documents\exist.txt FOUND
                  Archive\Gil\documents\today\not_copied.txt FOUND
                  RENAME to copied.txt
                  Renamed OK

                  Archive\Joe\documents\documents\exist.txt FOUND
                  Archive\Joe\documents\today\not_copied.txt FOUND
                  RENAME to copied.txt
                  Renamed OK

                  Archive\John\documents\documents\exist.txt FOUND
                  Archive\John\documents\today\not_copied.txt FOUND
                  RENAME to copied.txt
                  Renamed OK

                  Archive\Mary\documents\documents\exist.txt FOUND
                  Archive\Mary\documents\today\not_copied.txt FOUND
                  RENAME to copied.txt
                  Renamed OK

                  Archive\Mike\documentsMike\documents\exist.txt NOT FOUND

                  Archive\Nina\documents\documents\exist.txt FOUND
                  Archive\Nina\documents\today\not_copied.txt FOUND
                  RENAME to copied.txt
                  Renamed OK

                  Archive\Peter\documentsPeter\documents\exist.txt NOT FOUND

                  Archive\Sam\documents\documents\exist.txt FOUND
                  Archive\Sam\documents\today\not_copied.txt FOUND
                  RENAME to copied.txt
                  Renamed OK

                  Archive\Steve\documents\documents\exist.txt FOUND
                  Archive\Steve\documents\today\not_copied.txt FOUND
                  RENAME to copied.txt
                  Renamed OK



                  Blisk

                    Topic Starter


                    Intermediate

                    Thanked: 1
                    • Experience: Familiar
                    • OS: Windows 7
                    Re: multiple renaming files
                    « Reply #19 on: December 23, 2018, 07:22:40 AM »
                    Thank you it work great.
                    Also I have learned something new regarding scripts.

                    I can also use this script in reverse, so renaming files not_copied into copied in folders which doesn't have file exist.txt
                    If I need that, for now I don't need it.
                    Thank you



                    @echo off
                    setlocal enabledelayedexpansion
                    set "thisdir=%cd%"
                    for /f "delims=" %%A in ('dir archive /b /ad') do (
                        set NAME=%%A
                        REM echo Checking archive\!NAME!
                        if not exist "Archive\!NAME!\documents\exist.txt" (
                        echo Archive\!NAME!\documents\documents\exist.txt NOT FOUND
                        if exist "Archive\!NAME!\documents\today\not_copied.txt" (   
                           echo Archive\!NAME!\documents\today\not_copied.txt FOUND
                           echo RENAME to copied.txt
                                ren "archive\!NAME!\documents\today\not_copied.txt" "copied.txt" && echo Renamed OK
                                ) else (
                                echo Archive\!NAME!\documents\today\not_copied.txt NOT FOUND
                            )
                            ) else (
                            echo Archive\!NAME!\documents%%A\documents\exist.txt NOT FOUND
                        )
                        echo.
                    )
                    « Last Edit: December 23, 2018, 07:36:36 AM by Blisk »

                    Salmon Trout

                    • Guest
                    Re: multiple renaming files
                    « Reply #20 on: December 23, 2018, 07:46:59 AM »
                    Well done! It is always satisfying when a script is used for learning.