0

My code below looks for the file "a.csv", replaces 123 with abc within the file, then saves it as "b.csv".

@echo off
setlocal enableextensions enabledelayedexpansion

(for /f "tokens=*" %%f in (a.csv) do if not "%%f"=="" (
        set "line=%%f"
        set "line=!line:123=abc!"
        echo(!line!
)) > b.csv

endlocal

I'm trying to get it to look for all the csv files in the directory, not just "a.csv", use the file name of the csv within the string replacement, and overwrite the file, not save it as a new one. So lets say I have this file structure:

Main Folder/
├── batchfile.bat
├── Folder1/
│   ├── 1.csv - contains "123"
├── Folder2/
│   ├── 2.csv - contains "123"

If I ran the batch file, I'd end up with this:

Main Folder/
├── batchfile.bat
├── Folder1/
│   ├── 1.csv - now contains "1"
├── Folder2/
│   ├── 2.csv - now contains "2"

So far I've tried this:

@echo off
setlocal enableextensions enabledelayedexpansion

(for /f "tokens=*" %%f in (*.csv) do if not "%%f"=="" (
        set "line=%%f"
        set "line=!line:123=abc!"
        echo(!line!
)) > a.csv

endlocal

I thought I could target all the csv's with *.csv, and overwrite the file by calling it the same thing as the original, but I'm having trouble getting it to work. I'm also really quite unsure how to get the file names of the csv's?

Example of the csv file I'm going to work with when it's working. This code will replace all the "REPLACE" text in here with the file name of the csv.

element_type,div_size,section_header_size,section_header_title,section_header_title_position,section_header_title_color,section_header_subtitle,section_header_subtitle_position,section_header_subtitle_color,section_divider_type,sections,sections_slides,sections_size,sections_uniqid,sections_style,sections_placement,sections_type,sections_class,sections_clogic,sections_logic,sections_internal_name,selectbox_internal_name,selectbox_header_size,selectbox_header_title,selectbox_header_title_position,selectbox_header_title_color,selectbox_header_subtitle,selectbox_header_subtitle_position,selectbox_header_subtitle_color,selectbox_divider_type,selectbox_enabled,selectbox_required,selectbox_text_before_price,selectbox_text_after_price,selectbox_price_type,selectbox_hide_amount,selectbox_quantity,selectbox_quantity_min,selectbox_quantity_max,selectbox_quantity_step,selectbox_quantity_default_value,selectbox_placeholder,selectbox_use_url,selectbox_changes_product_image,multiple_selectbox_options_default_value,multiple_selectbox_options_title,multiple_selectbox_options_image,multiple_selectbox_options_imagec,multiple_selectbox_options_imagep,multiple_selectbox_options_imagel,multiple_selectbox_options_value,multiple_selectbox_options_price,multiple_selectbox_options_sale_price,multiple_selectbox_options_price_type,multiple_selectbox_options_description,multiple_selectbox_options_url,selectbox_uniqid,selectbox_clogic,selectbox_logic,selectbox_class,selectbox_container_id,selectbox_include_tax_for_fee_price_type,selectbox_tax_class_for_fee_price_type,selectbox_hide_element_label_in_cart,selectbox_hide_element_value_in_cart,selectbox_hide_element_label_in_order,selectbox_hide_element_value_in_order,selectbox_hide_element_label_in_floatbox,selectbox_hide_element_value_in_floatbox
    "selectbox","w100","3","","","","","","","none","15","","w100","5b6a1572b89c01.49663444","","before","","","{""toggle"":""show"",""what"":""any"",""rules"":[]}","","Section","Framing Options","10","","","","","","","none","1","0","","","","","","","","","","","","","0","Fine Art Print|Framed Fine Art Print|Fine Art Canvas","||","||","||","||","Fine Art Print|Framed Fine Art Print|Fine Art Canvas","||","||","||","||","||","5b6a1572b89c75.54224467","{""element"":""5b6a1572b89c75.54224467"",""toggle"":""show"",""what"":""all"",""rules"":[{""section"":""5b6a1572b89c01.49663444"",""element"":""1"",""operator"":""is"",""value"":""Mounted""}]}","","","","","","","","","","",""
    "selectbox","w100",,,,,,,,,,,,,,,,,,,,"Fine Art Print Options","10","","","","","","","none","1","0","","","","","","","","","","","","custom","0","Mounted|Unmounted","|","|","URL/wp-content/uploads/2018/08/REPLACE-EEDBU-PRM.jpg|URL/wp-content/uploads/2018/08/REPLACE-EEDBU-PR.jpg","|","Mounted|Unmounted","|","|","|","|","|","5b6a1572b89c96.65721126","{""element"":""5b6a1572b89c96.65721126"",""toggle"":""show"",""what"":""all"",""rules"":[{""section"":""5b6a1572b89c01.49663444"",""element"":""0"",""operator"":""is"",""value"":""Fine%20Art%20Print""}]}","1","","","","","","","","","",""
    "selectbox","w100",,,,,,,,,,,,,,,,,,,,"Fine Art Print Sizes - Mounted","10","","","","","","","none","1","0","","","","","","","","","","","","","0","60 x 80 cm (24 x 32 in)|50 x 70 cm (20 x 28 in)|40 x 50 cm (16 x 20 in)|30 x 40 cm (12 x 16 in)","|||","|||","|||","|||","60 x 80 cm (24 x 32 in)|50 x 70 cm (20 x 28 in)|40 x 50 cm (16 x 20 in)|30 x 40 cm (12 x 16 in)","2222|222|22|2","|||","|||","|||","|||","5b6a1572b89ca4.24145542","{""element"":""5b6a1572b89ca4.24145542"",""toggle"":""show"",""what"":""all"",""rules"":[{""section"":""5b6a1572b89c01.49663444"",""element"":""0"",""operator"":""is"",""value"":""Fine%20Art%20Print""},{""section"":""5b6a1572b89c01.49663444"",""element"":""1"",""operator"":""is"",""value"":""Mounted""}]}","1","","","","","","","","","",""
    "selectbox","w100",,,,,,,,,,,,,,,,,,,,"Fine Art Print Sizes - Unmounted","10","","","","","","","none","1","0","","","","","","","","","","","","","0","60 x 80 cm (24 x 32 in)|50 x 70 cm (20 x 28 in)|40 x 50 cm (16 x 20 in)|30 x 40 cm (12 x 16 in)","|||","|||","|||","|||","60 x 80 cm (24 x 32 in)|50 x 70 cm (20 x 28 in)|40 x 50 cm (16 x 20 in)|30 x 40 cm (12 x 16 in)","1111|111|11|1","|||","|||","|||","|||","5b6a1572b89cc1.28559565","{""element"":""5b6a1572b89cc1.28559565"",""toggle"":""show"",""what"":""all"",""rules"":[{""section"":""5b6a1572b89c01.49663444"",""element"":""0"",""operator"":""is"",""value"":""Fine%20Art%20Print""},{""section"":""5b6a1572b89c01.49663444"",""element"":""1"",""operator"":""is"",""value"":""Unmounted""}]}","1","","","","","","","","","",""
    "selectbox","w100",,,,,,,,,,,,,,,,,,,,"Fine Art Canvas Sizes","10","","","","","","","none","1","0","","","","","","","","","","","","custom","0","60 x 80 cm (24 x 32 in)|50 x 70 cm (20 x 28 in)|40 x 50 cm (16 x 20 in)|30 x 40 cm (12 x 16 in)","|||","|||","URL/wp-content/uploads/2018/08/REPLACE-EEDBU-CA.jpg|URL/wp-content/uploads/2018/08/REPLACE-EEDBU-CA.jpg|URL/wp-content/uploads/2018/08/REPLACE-EEDBU-CA.jpg|URL/wp-content/uploads/2018/08/REPLACE-EEDBU-CA.jpg","|||","60 x 80 cm (24 x 32 in)|50 x 70 cm (20 x 28 in)|40 x 50 cm (16 x 20 in)|30 x 40 cm (12 x 16 in)","3333|333|33|3","|||","|||","|||","|||","5b6a1572b89ce0.42422459","{""element"":""5b6a1572b89ce0.42422459"",""toggle"":""show"",""what"":""all"",""rules"":[{""section"":""5b6a1572b89c01.49663444"",""element"":""0"",""operator"":""is"",""value"":""Fine%20Art%20Canvas""}]}","1","","","","","","","","","",""
    "selectbox","w100",,,,,,,,,,,,,,,,,,,,"Framed options","10","","","","","","","none","1","0","","","","","","","","","","","","","0","White Frame|Black Frame|Natural Frame","||","||","||","||","White Frame|Black Frame|Natural Frame","||","||","||","||","||","5b6a1572b89d00.69455004","{""element"":""5b6a1572b89d00.69455004"",""toggle"":""show"",""what"":""all"",""rules"":[{""section"":""5b6a1572b89c01.49663444"",""element"":""0"",""operator"":""is"",""value"":""Framed%20Fine%20Art%20Print""}]}","1","","","","","","","","","",""
    "selectbox","w100",,,,,,,,,,,,,,,,,,,,"White Frame","10","","","","","","","none","1","0","","","","","","","","","","","","custom","0","Mounted|Unmounted","|","|","URL/wp-content/uploads/2018/08/REPLACE-EEDBU-FRM-WH.jpg|URL/wp-content/uploads/2018/08/REPLACE-EEDBU-FR-WH.jpg","|","Mounted|Unmounted","|","|","|","|","|","5b6a1572b89d19.51579868","{""element"":""5b6a1572b89d19.51579868"",""toggle"":""show"",""what"":""all"",""rules"":[{""section"":""5b6a1572b89c01.49663444"",""element"":""5"",""operator"":""is"",""value"":""White%20Frame""}]}","1","","","","","","","","","",""
    "selectbox","w100",,,,,,,,,,,,,,,,,,,,"White Frame Sizes - Mounted","10","","","","","","","none","1","0","","","","","","","","","","","","","0","60 x 80 cm (24 x 32 in)|50 x 70 cm (20 x 28 in)|40 x 50 cm (16 x 20 in)|30 x 40 cm (12 x 16 in)","|||","|||","|||","|||","60 x 80 cm (24 x 32 in)|50 x 70 cm (20 x 28 in)|40 x 50 cm (16 x 20 in)|30 x 40 cm (12 x 16 in)","5555|555|55|5","|||","|||","|||","|||","5b6a1572b89d35.77353810","{""element"":""5b6a1572b89d35.77353810"",""toggle"":""show"",""what"":""all"",""rules"":[{""section"":""5b6a1572b89c01.49663444"",""element"":""5"",""operator"":""is"",""value"":""White%20Frame""},{""section"":""5b6a1572b89c01.49663444"",""element"":""6"",""operator"":""is"",""value"":""Mounted""}]}","1","","","","","","","","","",""
    "selectbox","w100",,,,,,,,,,,,,,,,,,,,"White Frame Sizes - Unmounted","10","","","","","","","none","1","0","","","","","","","","","","","","","0","60 x 80 cm (24 x 32 in)|50 x 70 cm (20 x 28 in)|40 x 50 cm (16 x 20 in)|30 x 40 cm (12 x 16 in)","|||","|||","|||","|||","60 x 80 cm (24 x 32 in)|50 x 70 cm (20 x 28 in)|40 x 50 cm (16 x 20 in)|30 x 40 cm (12 x 16 in)","4444|444|44|4","|||","|||","|||","|||","5b6a1572b89d53.22331786","{""element"":""5b6a1572b89d53.22331786"",""toggle"":""show"",""what"":""all"",""rules"":[{""section"":""5b6a1572b89c01.49663444"",""element"":""5"",""operator"":""is"",""value"":""White%20Frame""},{""section"":""5b6a1572b89c01.49663444"",""element"":""6"",""operator"":""is"",""value"":""Unmounted""}]}","1","","","","","","","","","",""
    "selectbox","w100",,,,,,,,,,,,,,,,,,,,"Black Frame","10","","","","","","","none","1","0","","","","","","","","","","","","custom","0","Mounted|Unmounted","|","|","URL/wp-content/uploads/2018/08/REPLACE-EEDBU-FRM-BL.jpg|URL/wp-content/uploads/2018/08/REPLACE-EEDBU-FR-BL.jpg","|","Mounted|Unmounted","|","|","|","|","|","5b6a1572b89d68.62685492","{""element"":""5b6a1572b89d68.62685492"",""toggle"":""show"",""what"":""all"",""rules"":[{""section"":""5b6a1572b89c01.49663444"",""element"":""5"",""operator"":""is"",""value"":""Black%20Frame""}]}","1","","","","","","","","","",""
    "selectbox","w100",,,,,,,,,,,,,,,,,,,,"Black Frame Sizes - Mounted","10","","","","","","","none","1","0","","","","","","","","","","","","","0","60 x 80 cm (24 x 32 in)|50 x 70 cm (20 x 28 in)|40 x 50 cm (16 x 20 in)|30 x 40 cm (12 x 16 in)","|||","|||","|||","|||","60 x 80 cm (24 x 32 in)|50 x 70 cm (20 x 28 in)|40 x 50 cm (16 x 20 in)|30 x 40 cm (12 x 16 in)","8888|888|88|8","|||","|||","|||","|||","5b6a1572b89d75.66420811","{""element"":""5b6a1572b89d75.66420811"",""toggle"":""show"",""what"":""all"",""rules"":[{""section"":""5b6a1572b89c01.49663444"",""element"":""5"",""operator"":""is"",""value"":""Black%20Frame""},{""section"":""5b6a1572b89c01.49663444"",""element"":""9"",""operator"":""is"",""value"":""Mounted""}]}","1","","","","","","","","","",""
    "selectbox","w100",,,,,,,,,,,,,,,,,,,,"Black Frame Sizes - Unmounted","10","","","","","","","none","1","0","","","","","","","","","","","","","0","60 x 80 cm (24 x 32 in)|50 x 70 cm (20 x 28 in)|40 x 50 cm (16 x 20 in)|30 x 40 cm (12 x 16 in)","|||","|||","|||","|||","60 x 80 cm (24 x 32 in)|50 x 70 cm (20 x 28 in)|40 x 50 cm (16 x 20 in)|30 x 40 cm (12 x 16 in)","7777|777|77|7","|||","|||","|||","|||","5b6a1572b89d98.29842294","{""element"":""5b6a1572b89d98.29842294"",""toggle"":""show"",""what"":""all"",""rules"":[{""section"":""5b6a1572b89c01.49663444"",""element"":""5"",""operator"":""is"",""value"":""Black%20Frame""},{""section"":""5b6a1572b89c01.49663444"",""element"":""9"",""operator"":""is"",""value"":""Unmounted""}]}","1","","","","","","","","","",""
    "selectbox","w100",,,,,,,,,,,,,,,,,,,,"Natural Frame","10","","","","","","","none","1","0","","","","","","","","","","","","custom","0","Mounted|Unmounted","|","|","URL/wp-content/uploads/2018/08/REPLACE-EEDBU-FRM-NA.jpg|URL/wp-content/uploads/2018/08/REPLACE-EEDBU-FR-NA.jpg","|","Mounted|Unmounted","|","|","|","|","|","5b6a1572b89da7.45321633","{""element"":""5b6a1572b89da7.45321633"",""toggle"":""show"",""what"":""all"",""rules"":[{""section"":""5b6a1572b89c01.49663444"",""element"":""5"",""operator"":""is"",""value"":""Natural%20Frame""}]}","1","","","","","","","","","",""
    "selectbox","w100",,,,,,,,,,,,,,,,,,,,"Black Frame Sizes - Mounted","10","","","","","","","none","1","0","","","","","","","","","","","","","0","60 x 80 cm (24 x 32 in)|50 x 70 cm (20 x 28 in)|40 x 50 cm (16 x 20 in)|30 x 40 cm (12 x 16 in)","|||","|||","|||","|||","60 x 80 cm (24 x 32 in)|50 x 70 cm (20 x 28 in)|40 x 50 cm (16 x 20 in)|30 x 40 cm (12 x 16 in)","1111|111|11|1","|||","|||","|||","|||","5b6a1572b89dc7.71345110","{""element"":""5b6a1572b89dc7.71345110"",""toggle"":""show"",""what"":""all"",""rules"":[{""section"":""5b6a1572b89c01.49663444"",""element"":""5"",""operator"":""is"",""value"":""Natural%20Frame""},{""section"":""5b6a1572b89c01.49663444"",""element"":""12"",""operator"":""is"",""value"":""Mounted""}]}","1","","","","","","","","","",""
    "selectbox","w100",,,,,,,,,,,,,,,,,,,,"Black Frame Sizes - Unmounted","10","","","","","","","none","1","0","","","","","","","","","","","","","0","60 x 80 cm (24 x 32 in)|50 x 70 cm (20 x 28 in)|40 x 50 cm (16 x 20 in)|30 x 40 cm (12 x 16 in)","|||","|||","|||","|||","60 x 80 cm (24 x 32 in)|50 x 70 cm (20 x 28 in)|40 x 50 cm (16 x 20 in)|30 x 40 cm (12 x 16 in)","9999|999|99|9","|||","|||","|||","|||","5b6a1572b89dd8.56141959","{""element"":""5b6a1572b89dd8.56141959"",""toggle"":""show"",""what"":""all"",""rules"":[{""section"":""5b6a1572b89c01.49663444"",""element"":""5"",""operator"":""is"",""value"":""Natural%20Frame""},{""section"":""5b6a1572b89c01.49663444"",""element"":""12"",""operator"":""is"",""value"":""Unmounted""}]}","1","","","","","","","","","",""
13
  • 1
    So the string to replace is a full cell value? if so, I'd change the replacement to !line:,123,=,abc,! to not match a field like 012345. Anyway, to loop over multiple files, wrap a for loop around; to overwrite, do the replacement in a temporary file (e. g., add extension .tmp to current file name), then move the temp. file onto the original one using move /Y... Commented Aug 9, 2018 at 10:16
  • Thanks. The string to replace will be just something like "REPLACE" in my csv file, and that will be replaced with the name of the csv file. Commented Aug 9, 2018 at 10:34
  • I've got the overwrite working with your suggestion so thanks for that (edited code in main question), but this line you suggested !line:,123,=,abc,! doesn't work. Do you know how to make that line say set "line=!line:REPLACE= *get the name of the csv file here* !" Commented Aug 9, 2018 at 11:00
  • Great, renand del is also a way to replace the original file... You should provide a short example excerpt of the file contents... If you have a for %%i in (*.csv) do ( ... ) loop wrapped around to loop through all .csv files, use %%~ni to get the pure name of the current file... Commented Aug 9, 2018 at 11:17
  • 1
    The command rename *.csv temp.tmp needs to be changed to ren "%%~i" temp.tmp, and the part > a.csv must be changed to > "%%~i", the rest should be fine. Thanks for adding a CSV sample, so it is clear that my suggestion !line:,123,=,abc,! cannot work, because the string to be replaced can occur everywhere. By the way, if you find a solution by yourself, you should still not append it to the question, but post an answer instead... Commented Aug 9, 2018 at 11:45

1 Answer 1

1

Huge thanks to aschipfl for the help, here's the final working code for anyone else looking for something similar.

@echo off
setlocal enableextensions enabledelayedexpansion
for /R %%i in (*.csv) do (
ren "%%~i" temp.tmp
(for /F "usebackq tokens=*" %%f in ("%%~dpitemp.tmp") do (
        set "line=%%f"
        set "line=!line:123=%%~ni!"
        echo(!line!
)) > "%%~i"
del "%%~dpitemp.tmp"
)
endlocal
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.