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 14360 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.