在文本中使用csv文件名替换批处理文件

时间:2018-08-09 09:45:10

标签: file batch-file

下面的代码查找文件“ a.csv”,用文件中的abc替换123,然后将其另存为“ 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

我试图让它查找目录中的所有csv文件,而不仅仅是“ a.csv”,在字符串替换中使用csv的文件名,并覆盖文件,而不是将其另存为一个新的。所以可以说我有这个文件结构:

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

如果我运行了批处理文件,我将得到以下结果:

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

到目前为止,我已经尝试过:

@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

我以为我可以使用* .csv定位所有csv,并通过与原始文件相同的名称来覆盖该文件,但是我无法使其正常工作。我也很不确定如何获取csv的文件名?

在工作时要使用的csv文件示例。此代码将用csv的文件名替换此处的所有“ REPLACE”文本。

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","","","","","","","","","",""

1 个答案:

答案 0 :(得分:1)

非常感谢aschipfl的帮助,这是其他寻求类似内容的人的最终工作代码。

@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