标题: [文本处理] 请教bat批处理,文件名或其存放路径名含有空格,需替换字符串也带有空格 [打印本页]
作者: qspringleaf 时间: 2021-10-4 18:26 标题: 请教bat批处理,文件名或其存放路径名含有空格,需替换字符串也带有空格
致论坛的老师&大神:
请教关于bat批处理,有两个地方的空格导致不知该如何解决,1,待处理的文件名或其存放路径名含有空格,2.需要替换文件里边内容,该内容是带有空格的字符串。搜变全网没找到都满足的解决方案。
比方说有一个txt文档,他的文件名是: address update.txt,文件名或其存放路径名含有空格,
要替换的内容为: 000- woodlands drive 79,想将文本内所有的该字符串替换为 000- sembawang street 66
address update.txt文本内容如下:- Update = Open( Concat( New_path, "000- woodlands drive 79.csv" ) );
-
- Update << select where( :RAW_VALUE == 0 );
- Update << Delete Rows;
-
- Update << New Column( "Batch",
- Character,
- Nominal,
- Formula(
- If(
- Contains( :ID, "ID001" ), "001",
- Contains( :ID, "ID002" ), "002",
- )
- )
- );
-
- Update << Move Selected Columns({“Batch”}, To First);
-
- Column( Update, "shift ID" ) << data type( Character );
- Column( Update, "shift ID" ) << Set Modeling Type( Nominal );
- Column( Update, "shift ID" ) << set name( "Batch ID" );
-
- For Each Row(
- Update,
- :Batch ID = Match( :Batch ID,
- "9653856", "shift1",
- "9673409", "shift2",
- "9653427", "shift1",
- "9663511", "shift2",
- "9653427", "shift1",
- "9663550", "shift2",
- "Unspecified"
- )
- );
-
- Update << New Column( "rename",
- Character,
- Nominal,
- Formula(
- If(
- Contains( :address, "param1" ), "000- woodlands drive 79",
- Contains( :address, "param2" ), "000- woodlands drive 79",
- Contains( :address, "param3" ), "000- woodlands drive 79",
- Contains( :address, "param4" ), "000- woodlands drive 79",
- Contains( :address, "param5" ), "000- woodlands drive 79",
- Contains( :address, "param6" ), "000- woodlands drive 79",
- )
- )
- );
-
- dtWaferLevel = Update << Split(
- Split By( :rename ),
- Split( :RAW_VALUE ),
- Group(
- :Batch,
- :Category,
- :LOT_ID,
- :Batch ID,
- ),
- Remaining Columns( Drop All )
- );
复制代码
作者: idwma 时间: 2021-10-4 19:08
- @echo off
- setlocal enabledelayedexpansion
- for /f "tokens=1* delims=:" %%i in ('findstr /n ".*" "address update.txt"') do (
- set a=%%j
- if "%%j"=="" (echo;) else (
- echo;!a:000- woodlands drive 79=000- sembawang street 66!
- )
- )
- pause
复制代码
作者: qspringleaf 时间: 2021-10-4 20:49
回复 2# idwma
感谢🙏,想再请教如果该文件 address update.txt的完整路径是c:\address name\address update.txt, 当字符串被替换完成后,如何实现自动保存在原路径下,并覆盖原文件。感激不尽。
作者: idwma 时间: 2021-10-4 21:08
- @echo off
- setlocal enabledelayedexpansion
- set "f=c:\address name\address update.txt"
- (for /f "tokens=1* delims=:" %%i in ('findstr /n ".*" "%f%"') do (
- set a=%%j
- if "%%j"=="" (echo;) else (
- echo;!a:000- woodlands drive 79=000- sembawang street 66!
- )
- )>tmptmp
- move /y tmptmp %f%
- pause
复制代码
作者: Batcher 时间: 2021-10-4 21:10
回复 3# qspringleaf - @echo off
- setlocal enabledelayedexpansion
- cd /d "C:\address name"
- (for /f "tokens=1* delims=:" %%i in ('findstr /n ".*" "address update.txt"') do (
- set a=%%j
- if "%%j"=="" (
- echo;
- ) else (
- echo;!a:000- woodlands drive 79=000- sembawang street 66!
- )
- ))>"address update.tmp"
- move /y "address update.tmp" "address update.txt"
复制代码
作者: qspringleaf 时间: 2021-10-5 20:18
回复 5# Batcher
感谢老师,试过了,成功!
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |