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

Author Topic: copy and rename file with path name  (Read 14359 times)

0 Members and 1 Guest are viewing this topic.

novice84

    Topic Starter


    Rookie

    • Experience: Beginner
    • OS: Windows 7
    copy and rename file with path name
    « on: June 25, 2013, 07:31:51 AM »
    HI
    can we copy files e.g. *.txt from specified folder to destination folder with file path added in file name.

    Geek-9pm


      Mastermind
    • Geek After Dark
    • Thanked: 1026
      • Gekk9pm bnlog
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 10
    Re: copy and rename file with path name
    « Reply #1 on: June 25, 2013, 11:34:39 AM »
    Is this homework?
    Did you want it to be all on one line? Hard to do.

    Maybe a single block of code as a FOR loop?

    The process would be to:
       make a DIR for the file,
       and then copy it there.
    repeat for every file in nth current location.

    Is that what you mean? or something else.

    novice84

      Topic Starter


      Rookie

      • Experience: Beginner
      • OS: Windows 7
      Re: copy and rename file with path name
      « Reply #2 on: June 25, 2013, 05:48:10 PM »
      C:\F1\F2\F3\F4\D1\1.TXT
      C:\F1\F2\F3\F4\D2\1.TXT
      C:\F1\F2\F3\F4\D3\1.TXT

      COPY THESE IN NEW FOLDER (OR EXISTING DIR) NAMED AS

      C:\NEW FOLDER\F1_F2_F3_F4_D1_1.TXT
      C:\NEW FOLDER\F1_F2_F3_F4_D2_1.TXT
      C:\NEW FOLDER\F1_F2_F3_F4_D3_1.TXT

      OR COPY WITH NAME LIKE THIS BELOW :

      C:\NEW FOLDER\F1F2F3F4D11.TXT
      C:\NEW FOLDER\F1F2F3F4D21.TXT
      C:\NEW FOLDER\F1F2F3F4D31.TXT

      Squashman



        Specialist
      • Thanked: 134
      • Experience: Experienced
      • OS: Other
      Re: copy and rename file with path name
      « Reply #3 on: June 25, 2013, 07:15:11 PM »
      Is F1 your real root folder.  Having this traverse the whole directory structure of C: may cause some undesirable results.

      novice84

        Topic Starter


        Rookie

        • Experience: Beginner
        • OS: Windows 7
        Re: copy and rename file with path name
        « Reply #4 on: June 25, 2013, 08:07:00 PM »
        no problem to skip F1 folder
        if required F2 folder can be skipped also.

        novice84

          Topic Starter


          Rookie

          • Experience: Beginner
          • OS: Windows 7
          Re: copy and rename file with path name
          « Reply #5 on: June 25, 2013, 09:05:40 PM »
          I mean if error occurs with full path, files can be Copied WITH NAMEs LIKE THIS BELOW :

          C:\NEW FOLDER\F3F4D11.TXT
          C:\NEW FOLDER\F3F4D21.TXT
          C:\NEW FOLDER\F3F4D31.TXT

          Squashman



            Specialist
          • Thanked: 134
          • Experience: Experienced
          • OS: Other
          Re: copy and rename file with path name
          « Reply #6 on: June 25, 2013, 09:24:34 PM »
          That is not what I mean.  I am asking where the root directory of all your data is.  I am not sure how else to explain this to you.  You do not want to run this on the entire C: drive and then try and output to the C: drive.  Is your ROOT folder with all your data F1?

          Geek-9pm


            Mastermind
          • Geek After Dark
          • Thanked: 1026
            • Gekk9pm bnlog
          • Certifications: List
          • Computer: Specs
          • Experience: Expert
          • OS: Windows 10
          Re: copy and rename file with path name
          « Reply #7 on: June 25, 2013, 09:48:36 PM »
          Squashman is saying, I think, that one does not do test of the directory
          C:\
          Which is root. Instead none make a directory for tests.
          C:\TESTS
          The if anything goes wrong, it is confined to that location.

          One unconfirmed source says the maxim number of files in root can not go over 2,048. That would be one reason for not using root for tests.

          novice84

            Topic Starter


            Rookie

            • Experience: Beginner
            • OS: Windows 7
            Re: copy and rename file with path name
            « Reply #8 on: June 25, 2013, 10:38:29 PM »
            OK I understood now what you are saying.
            say my all files are in D:\F1\F2\F3\F4 (source folder)

            so all files are in F4
            copy to C:\NEW FOLDER(destination folder)

            source and destination path will change as required e.g. if I want all files from folder D:\F1\F2\F3 or all files from folder D:\F1\F2\N3
            I will change source path as required.

            Squashman



              Specialist
            • Thanked: 134
            • Experience: Experienced
            • OS: Other
            Re: copy and rename file with path name
            « Reply #9 on: June 26, 2013, 04:39:28 AM »
            We can put the whole path into the filenames I was just amking sure you understood the dangers of doing this on an entire Drive letter and outputting to the same drive letter.

            Lemonilla



              Apprentice

            • "Too sweet"
            • Thanked: 70
            • Computer: Specs
            • Experience: Experienced
            • OS: Windows 7
            Re: copy and rename file with path name
            « Reply #10 on: June 26, 2013, 11:44:45 AM »
            Code: [Select]
            :: $renameWithPath.bat <source path> <destination path>

            @echo off
            setlocal EnableDelayedExpansion
            cd %1
            for %%A in (*.txt) do (
            set path=%%~pA
            set "path=!path:\=_!"
            set path=!path:~1,10000!
            copy "%%A" "%2\!path!%%~nxA" >nul && echo copied %%A to %2\!path!%%~nxA
            )
            Quote from: patio
            God Bless the DOS Helpers...
            Quote
            If it compiles, send the files.

            novice84

              Topic Starter


              Rookie

              • Experience: Beginner
              • OS: Windows 7
              Re: copy and rename file with path name
              « Reply #11 on: June 27, 2013, 06:30:58 AM »
              almost there
              sorry I think I made it confusing for you by putting *.txt as files need to be copied
              I just put that as an example
              if you look at my example I have mentioned files names same in all folders.
              files could be similar in folders
              File name or extension can be any.
              say I want to copy files containing words "few*" or "*files"
              or look at example below :

              E:\Checking\Routine\Afternoon\15-04-13\copy.txt
              E:\Checking\Routine\Afternoon\15-04-13\New copy.doc
              E:\Checking\Routine\Afternoon\16-04-13\copy.txt
              E:\Checking\Routine\Afternoon\16-04-13\old copy.doc
              E:\Checking\Routine\Afternoon\17-04-13\new type\copy.txt
              E:\Checking\Routine\Afternoon\17-04-13\new type\New copy.doc

              So i can specify if want to copy files in E:\Checking\Routine\Afternoon folder all files containing word "new" to folder e.g. "C:\Temp"
              or if I want I can copy all file *.txt or *.doc or by letters/words in file name

              in this code it doesn't search sub folders and I could not specify paths as first line is for remarks
              :: $renameWithPath.bat <source path> <destination path>

              Sorry I don't know much about coding
              so i want how to specify paths in this code
              at the moment batch file is looking and running in the folder where it is located

              Lemonilla



                Apprentice

              • "Too sweet"
              • Thanked: 70
              • Computer: Specs
              • Experience: Experienced
              • OS: Windows 7
              Re: copy and rename file with path name
              « Reply #12 on: June 27, 2013, 07:59:31 AM »
              Just a quick reply, I'll look into subfolders in a bit. The way the script is set up, you specify the folders by running it with arguments from command prompt.

              command prompt:
              Code: [Select]

              C:\>$a.bat "C:\test\1\2\3" "C:\test2"
              copied 1.txt to "C:\test2"\test_1_2_3_1.txt
              copied 2.txt to "C:\test2"\test_1_2_3_2.txt
              copied 3.txt to "C:\test2"\test_1_2_3_3.txt
              copied 4.txt to "C:\test2"\test_1_2_3_4.txt
              copied 5.txt to "C:\test2"\test_1_2_3_5.txt
              copied 6.txt to "C:\test2"\test_1_2_3_6.txt
              copied 7.txt to "C:\test2"\test_1_2_3_7.txt
              copied 8.txt to "C:\test2"\test_1_2_3_8.txt
              copied 9.txt to "C:\test2"\test_1_2_3_9.txt


              C:\>dir test2
               Volume in drive C is Acer
               Volume Serial Number is D0E0-609C

               Directory of C:\test2

              06/27/2013  09:57 AM    <DIR>          .
              06/27/2013  09:57 AM    <DIR>          ..
              06/27/2013  09:54 AM                 3 test_1_2_3_1.txt
              06/27/2013  09:54 AM                 3 test_1_2_3_2.txt
              06/27/2013  09:54 AM                 3 test_1_2_3_3.txt
              06/27/2013  09:54 AM                 3 test_1_2_3_4.txt
              06/27/2013  09:54 AM                 3 test_1_2_3_5.txt
              06/27/2013  09:54 AM                 3 test_1_2_3_6.txt
              06/27/2013  09:54 AM                 3 test_1_2_3_7.txt
              06/27/2013  09:54 AM                 3 test_1_2_3_8.txt
              06/27/2013  09:54 AM                 3 test_1_2_3_9.txt
                             9 File(s)             27 bytes
                             2 Dir(s)  196,188,549,120 bytes free

              C:\>

              I will work on allowing for you to search by word, but you may have to edit the code every time.

              EDIT:
              This works for everything but searching, not sure how to do that.
              Code: [Select]
              :: $renameWithPath.bat <source path> <destination path> <extentions>

              @echo off
              setlocal EnableDelayedExpansion
              cd %1
              set ext=%3

              for %%A in (*.!ext!) do (
              set path=%%~pA
              set "path=!path:\=_!"
              set path=!path:~1,10000!
              copy "%%A" "%2\!path!%%~nxA" >nul && echo copied %%A to %2\!path!%%~nxA
              )

              for /f "delims=" %%B in ('dir /b /a:d') do (
              cd %%B
              for %%A in (*.!ext!) do (
              set path=%%~pA
              set "path=!path:\=_!"
              set path=!path:~1,10000!
              copy "%%A" "%2\!path!%%~nxA" >nul && echo copied %%A to %2\!path!%%~nxA
              )
              cd ..
              )
              « Last Edit: June 27, 2013, 08:10:36 AM by Lemonilla »
              Quote from: patio
              God Bless the DOS Helpers...
              Quote
              If it compiles, send the files.

              novice84

                Topic Starter


                Rookie

                • Experience: Beginner
                • OS: Windows 7
                Re: copy and rename file with path name
                « Reply #13 on: June 28, 2013, 05:08:27 AM »
                 nothing happening :( :( :(

                Does it matter what we name this batch file? and where we plcae it ?

                in code top line is :

                :: $renameWithPath.bat <source path> <destination path> <extentions>
                I am just changing top line of code like this :

                :: $renameWithPath.bat "E:\Checking\Routine\Afternoon\15-04-13" "C:\test" "*.txt"

                can you please tell me what am missing or making mistake ?

                Lemonilla



                  Apprentice

                • "Too sweet"
                • Thanked: 70
                • Computer: Specs
                • Experience: Experienced
                • OS: Windows 7
                Re: copy and rename file with path name
                « Reply #14 on: June 28, 2013, 07:39:54 AM »
                Problem 1) the double :: allows for the batch file to not run the command, thus if you are simply copying and pasting, then it will not run in command line either.

                Problem 2) The "$renameWithPath.bat" is the name i gave the file. You would replace this with the name you saved the code as.

                Problem 3) "*.txt" should be "txt".
                Quote from: patio
                God Bless the DOS Helpers...
                Quote
                If it compiles, send the files.

                novice84

                  Topic Starter


                  Rookie

                  • Experience: Beginner
                  • OS: Windows 7
                  Re: copy and rename file with path name
                  « Reply #15 on: June 28, 2013, 08:40:07 AM »
                  THANKS FOR REPLY
                  1. SO TOP LINE NO NEED TO EDIT, IT'S A REMARK
                  2. I GAVE FILE NAME P.BAT AND AS TOP LINE JUST FOR REMARK, SO DOESN'T MATTER IF CORRECT NAME AND TYPE LOCATIONS.
                  3. I PLACED P.BAT IN C:\TEST AND OPEN CMD THERE, TRIED TYPING COMMAND IN CMD AS SHOWN BELOW :
                  C:\TEST> P.BAT E:\FOLDER\NAME C:\TEST TXT
                  NOTHING HAPPENED  :( :( :( :(

                  Lemonilla



                    Apprentice

                  • "Too sweet"
                  • Thanked: 70
                  • Computer: Specs
                  • Experience: Experienced
                  • OS: Windows 7
                  Re: copy and rename file with path name
                  « Reply #16 on: June 28, 2013, 12:46:15 PM »
                  try adding quotes
                  Code: [Select]
                  p.bat "e:\folder\name" "c:\test" txt
                  Quote from: patio
                  God Bless the DOS Helpers...
                  Quote
                  If it compiles, send the files.

                  novice84

                    Topic Starter


                    Rookie

                    • Experience: Beginner
                    • OS: Windows 7
                    Re: copy and rename file with path name
                    « Reply #17 on: June 28, 2013, 06:21:45 PM »
                    Also Tried that but doesn't work
                    there is something wrong with path specification and ext specification
                    I think I can't specify it correct way

                    Lemonilla



                      Apprentice

                    • "Too sweet"
                    • Thanked: 70
                    • Computer: Specs
                    • Experience: Experienced
                    • OS: Windows 7
                    Re: copy and rename file with path name
                    « Reply #18 on: June 28, 2013, 07:31:58 PM »
                    Try adding "::" before '@echo off ' (just like in line 1)
                    This should be the 3rd line.

                    then:
                    Code: [Select]
                    p.bat "e:\folder\name" "c:\test" txt 1>p_bat_Test_Log.txt 2>&1
                    and post p_bat_Test_Log.txt

                    This will allow us to look at what is running and what is not.
                    Quote from: patio
                    God Bless the DOS Helpers...
                    Quote
                    If it compiles, send the files.

                    novice84

                      Topic Starter


                      Rookie

                      • Experience: Beginner
                      • OS: Windows 7
                      Re: copy and rename file with path name
                      « Reply #19 on: June 28, 2013, 08:38:10 PM »
                      attached 3 files generated in C:test folder and batch file was also in same folder


                      [recovering disk space, attachment deleted by admin]

                      Lemonilla



                        Apprentice

                      • "Too sweet"
                      • Thanked: 70
                      • Computer: Specs
                      • Experience: Experienced
                      • OS: Windows 7
                      Re: copy and rename file with path name
                      « Reply #20 on: June 28, 2013, 09:01:51 PM »
                      try changing 'copy' to 'xcopy'? I have never tried to move things between drives with batch.  It seems to be copying the logs.
                      Quote from: patio
                      God Bless the DOS Helpers...
                      Quote
                      If it compiles, send the files.

                      novice84

                        Topic Starter


                        Rookie

                        • Experience: Beginner
                        • OS: Windows 7
                        Re: copy and rename file with path name
                        « Reply #21 on: June 28, 2013, 10:04:28 PM »
                        nope still nothing working.
                        it look like it is looking in same folder where batch file is located and the specified paths not picking up

                        Squashman



                          Specialist
                        • Thanked: 134
                        • Experience: Experienced
                        • OS: Other
                        Re: copy and rename file with path name
                        « Reply #22 on: June 29, 2013, 12:51:57 AM »
                        Read the help on the CD command. I normally dont use the option that is in there that you are missing. I normally use pushd.

                        Salmon Trout

                        • Guest
                        Re: copy and rename file with path name
                        « Reply #23 on: June 29, 2013, 02:17:51 AM »

                        in code top line is :

                        :: $renameWithPath.bat <source path> <destination path> <extentions>
                        I am just changing top line of code like this :

                        :: $renameWithPath.bat "E:\Checking\Routine\Afternoon\15-04-13" "C:\test" "*.txt"

                        can you please tell me what am missing or making mistake ?

                        A line starting with a colon is a label. A line starting with two colons is a broken label, sometimes used as a comment. (This is a bad practice, better to use REM). Comments are not executed, so you can change them all you like and it won't change what the batch script does.

                        foxidrive



                          Specialist
                        • Thanked: 268
                        • Experience: Experienced
                        • OS: Windows 8
                        Re: copy and rename file with path name
                        « Reply #24 on: June 29, 2013, 02:43:43 AM »
                        Rem lines are parsed by cmd - if you put < and | in a REM it will barf.

                        When using :: it doesn't parse the commant at all, which is why it is a useful way to add comments.

                        Salmon Trout

                        • Guest
                        Re: copy and rename file with path name
                        « Reply #25 on: June 29, 2013, 03:15:12 AM »
                        Rem lines are parsed by cmd - if you put < and | in a REM it will barf.

                        When using :: it doesn't parse the commant at all, which is why it is a useful way to add comments.

                        A good point, which I had overlooked. I suppose I don't like them because if a person gets in the habit of always using broken labels as comments, one day they will use it in a parenthetical block, which will also fail or barf.



                        novice84

                          Topic Starter


                          Rookie

                          • Experience: Beginner
                          • OS: Windows 7
                          Re: copy and rename file with path name
                          « Reply #26 on: June 29, 2013, 03:34:10 AM »
                          thanks Salmon Trout & foxidrive for explaining REM & ::
                          @Squashman - I tried Pushd command also, but I think something need to be fixed in code.

                          is there any way we can get this code working ?
                          is it possible to copy files with path name in file name ?

                          thanks in advance.

                          foxidrive



                            Specialist
                          • Thanked: 268
                          • Experience: Experienced
                          • OS: Windows 8
                          Re: copy and rename file with path name
                          « Reply #27 on: June 29, 2013, 05:27:25 AM »
                          This is meant to rename certain filetypes and include the path from the current folder.
                          It writes RENFILE.BAT which will rename the files if you run it, so you can check it first.


                          Code: [Select]
                          @echo off
                          echo.@echo off> renfile.bat
                          for /f "delims=" %%a in ('dir *.jpg *.png *.gif /b /s /a-d ') do call :next "%%a"
                          echo renfile.bat created
                          pause
                          goto :eof
                          :next
                          set "var=%~1"
                          call set "var=%%var:%cd%=%%"
                          set "var=%var:\=_%"
                          >>renfile.bat echo ren "%~1" "%var:~1%"

                          novice84

                            Topic Starter


                            Rookie

                            • Experience: Beginner
                            • OS: Windows 7
                            Re: copy and rename file with path name
                            « Reply #28 on: June 29, 2013, 05:52:07 AM »
                            Hi Foxidrive
                            In another thread I appreciate you help.
                            but from this code it looks it is to rename files, but as mentioned I want to copy file to different location first and then rename those.
                            Problem is I have folders with name as different dates and files names are same
                            If you look at posts in this thread I have mentioned I have same file name in different folders. So I want to copy as well as rename them to different location with path in the name.
                            Some folders have sub folders also, so code should be able to copy files from deep tree sub folders also.
                            I was wondering if we can have user input of source and destination paths with SET variable like
                            set /p source=
                            and
                            set /p Dest= 


                            foxidrive



                              Specialist
                            • Thanked: 268
                            • Experience: Experienced
                            • OS: Windows 8
                            Re: copy and rename file with path name
                            « Reply #29 on: June 29, 2013, 05:56:12 AM »
                            All you have to do is change this line

                            Code: [Select]
                            >>renfile.bat echo ren "%~1" "%var:~1%"
                            to this:

                            Code: [Select]
                            >>renfile.bat echo copy "%~1" "d:\new\folder\%var:~1%"
                            Then see if the copy commands look right before launching the renfile.bat batch file.  Make sure d:\new\folder exists first.

                            novice84

                              Topic Starter


                              Rookie

                              • Experience: Beginner
                              • OS: Windows 7
                              Re: copy and rename file with path name
                              « Reply #30 on: June 29, 2013, 06:29:51 AM »
                              Hi Foxidrive
                              sorry I didn't look at first sight and replied.
                              thanks for that, I checked it and changed it 3 different versions as per requirement before I saw you reply.
                              I have a question again :-X
                              As I asked for advise before is there any such kind of thing if you don't know DOS and C++ you can't leran/Understand VB very well (as my 2 friends advised)
                              my knowledge of DOS is very of few basic commands which I learned 15 years before in school.
                              I want to learn VBA to automate few task in softwares like excel and many other applications which has customisation thru VBA so we can automate our daily tasks.
                              what's your advise as per your experience should I spend time learning DOS and C++ or VBA we can learn without learning these.

                              foxidrive



                                Specialist
                              • Thanked: 268
                              • Experience: Experienced
                              • OS: Windows 8
                              Re: copy and rename file with path name
                              « Reply #31 on: June 29, 2013, 06:48:07 AM »
                              I haven't used VBA so I can't give an informed opinion.

                              Lemonilla



                                Apprentice

                              • "Too sweet"
                              • Thanked: 70
                              • Computer: Specs
                              • Experience: Experienced
                              • OS: Windows 7
                              Re: copy and rename file with path name
                              « Reply #32 on: June 29, 2013, 08:34:25 AM »
                              another problem that I noticed was that you can't change drives with 'cd'

                              so change 'cd %1' to
                              Code: [Select]
                              %~d1
                              cd %1

                              this should put you in the write directory. You can add 'cd' to a third line and it will echo the current directory for you to double check if you want.
                              Quote from: patio
                              God Bless the DOS Helpers...
                              Quote
                              If it compiles, send the files.

                              Salmon Trout

                              • Guest
                              Re: copy and rename file with path name
                              « Reply #33 on: June 29, 2013, 09:01:33 AM »
                              another problem that I noticed was that you can't change drives with 'cd'

                              You can use the cd command to change to a folder on another drive if you use the /d switch. I always use that switch after cd, it has no effect if the new folder is on the same drive.

                              you are in c:\test, you want to go to D:\pictures\holidays you do this: cd /d D:\pictures\holidays, it is the same as doing first D: and then cd pictures/holidays.

                              Salmon Trout

                              • Guest
                              Re: copy and rename file with path name
                              « Reply #34 on: June 29, 2013, 12:14:17 PM »
                              I always use that switch after cd, it has no effect if the new folder is on the same drive.

                              That means, more clearly, the /d switch does no harm if the new folder is on the same drive.

                              Squashman



                                Specialist
                              • Thanked: 134
                              • Experience: Experienced
                              • OS: Other
                              Re: copy and rename file with path name
                              « Reply #35 on: June 29, 2013, 12:53:47 PM »
                              another problem that I noticed was that you can't change drives with 'cd'

                              so change 'cd %1' to
                              Code: [Select]
                              %~d1
                              cd %1

                              this should put you in the write directory. You can add 'cd' to a third line and it will echo the current directory for you to double check if you want.
                              That was the point of my previous post and told you to read the help for that command. You were not using the correct syntax.

                              Lemonilla



                                Apprentice

                              • "Too sweet"
                              • Thanked: 70
                              • Computer: Specs
                              • Experience: Experienced
                              • OS: Windows 7
                              Re: copy and rename file with path name
                              « Reply #36 on: June 29, 2013, 04:17:25 PM »
                              That was the point of my previous post and told you to read the help for that command. You were not using the correct syntax.
                              My apologies, I missed your earlier post. Thank you for pointing it out, could come in handy.
                              Quote from: patio
                              God Bless the DOS Helpers...
                              Quote
                              If it compiles, send the files.