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

Author Topic: Copy the filename of a file and replace the strings inside the same file-batch  (Read 5001 times)

0 Members and 1 Guest are viewing this topic.

_unknown_

    Topic Starter


    Beginner

    • Experience: Beginner
    • OS: Windows 7
    I have a folder with numerous files wherein I want to copy the file name of the file and want to replace strings inside that same file with the file name copied but the last 4 character should be changed. *This isn't the same with the previous post.

    I have this files with this file name:
    Code: [Select]
    Data2016001045000.level1_level2.newDATA_main.vrt
    Data2016002055000.level1_level2.newDATA_main.vrt
    Data2016003055000.level1_level2.newDATA_main.vrt
    and so on . . .
    Each file looks like this:
    Code: [Select]
       <Insert="Line 1">
         <Insert="Line 2">[b]file1.vrt[/b]</Line2>
         <Insert="Line 2">1</Line2>
         <Insert="Line 3">file2.vrt</Line3>
         <Insert="Line 3">1</Line3>
         <Another line="Line 4">0</Line4>
       </Insert>

    Let's say the first file name to copy is Data2016001045000.level1_level2.newDATA _main.vrt. Copied file name will replace the file1.vrt and file2.vrt but having the last 4 character(main) changed to data1 and data2. So the file should look like this:
    Code: [Select]
       <Insert="Line 1">
         <Insert="Line 2">[b]Data2016001045000.level1_level2.newDATA_data1.vrt[/b]</Line2>
         <Insert="Line 2">1</Line2>
         <Insert="Line 3">[b]Data2016001045000.level1_level2.newDATA_data2.vrt[/b]</Line3>
         <Insert="Line 3">1</Line3>
         <Another line="Line 4">0</Line4>
       </Insert>
    Here is what I have so far. Need help:
    Code: [Select]
    @echo off

    set "search1=file1.vrt"
    set "search2=file2.vrt"
    set "src=C:\path\to\source\files"
    pushd "%src%"

    for /f "delims=" %%a in ('dir /b /a-d "*.newDATA_main.vrt" ') do (
       echo Processing "%%a"
       set "filename=%%a"
       (
          for /f "usebackq delims=" %%b in ('type "%filename%" ^& break ^> "%filename%" ') do (
             set "line=%%b"
             set "line=!line:%search%=%filename%data1.vrt!"
             set "line=!line:%search%=%filename%data2.vrt!"
          )   
       ))>"%dst%\%%a.tmp" echo(!line1! !line2!
    « Last Edit: February 07, 2016, 12:00:33 PM by _unknown_ »

    foxidrive



      Specialist
    • Thanked: 268
    • Experience: Experienced
    • OS: Windows 8
    The details you show are terrible.  Nothing learned from my previous rant? :D
    There was a good deal of comment via PM...

    This gives you a way to separate the filename elements using the data that you provided.

    Code: [Select]
    @echo off
    set "src=C:\path\to\source\files"
    pushd "%src%"
       for /f "tokens=1,2 delims=_" %%a in ('dir /b /a-d "*.newDATA_main.vrt" ') do (
         echo "%%a_%%b_data1.vrt"
         echo "%%a_%%b_data2.vrt"
       )
    pause

    foxidrive



      Specialist
    • Thanked: 268
    • Experience: Experienced
    • OS: Windows 8
    What do you mean terrible? :( I've provided a post with complete detail

    Complete detail is when the line says "My name is Peter"
    and you provide information that says something like  "My name is xxxxx"
    instead of "abc 1111 data"

    The reason why that is so important, is that Batch file code often changes when the data/text changes.

    The code I provided above just shows how you can separate the filename tokens - it was quick to post.

    Squashman



      Specialist
    • Thanked: 134
    • Experience: Experienced
    • OS: Other
    Is this like the 4th thread started on this same topic?

    _unknown_

      Topic Starter


      Beginner

      • Experience: Beginner
      • OS: Windows 7
      Is this like the 4th thread started on this same topic?
      Sorry but it's not the same with the previous topic. Previous topic was to insert a file inside another file. This is copying the filename and then inside that same file you'll have to search and replace a string using the filename you copied and just change the last part.

      _unknown_

        Topic Starter


        Beginner

        • Experience: Beginner
        • OS: Windows 7
        I can do the "replace" thing for every newDATA_main.vrt using this code:

        This is for data1.bat
        Code: [Select]
        @echo off
            setlocal enableextensions disabledelayedexpansion

            set "search=file1.vrt"
            set "replace=D2016001045000.level1_level2.newDATA_data1.vrt"

            set "textFile=D2016001045000.level1_level2.newDATA_main.vrt"

            for /f "delims=" %%i in ('type "%textFile%" ^& break ^> "%textFile%" ') do (
                set "line=%%i"
                setlocal enabledelayedexpansion
                set "line=!line:%search%=%replace%!"
                >>"%textFile%" echo(!line!
                endlocal
            )

        This is for data2.bat
        Code: [Select]
        @echo off
            setlocal enableextensions disabledelayedexpansion

            set "search=file2.vrt"
            set "replace=D2016001045000.level1_level2.newDATA_data2.vrt"

            set "textFile=D2016001045000.level1_level2.newDATA_main.vrt"

            for /f "delims=" %%i in ('type "%textFile%" ^& break ^> "%textFile%" ') do (
                set "line=%%i"
                setlocal enabledelayedexpansion
                set "line=!line:%search%=%replace%!"
                >>"%textFile%" echo(!line!
                endlocal
            )

        But this is only good for every single file and all the time, data1 and data2 were specified individually and run individually which is not good. I want to do it with more files and replace the file1.vrt and file2.vrt simultaneously in one batch code .