简体   繁体   English

创建/查找批处理脚本,该脚本在目录和子目录中执行“查找和替换”,适用于具有不同名称的 excel 文件

[英]Create/find batch script which performs a 'find and replace' within a directory and sub-directories, for excel files with varied names

I have many files (Excel/CSV) contained in varied layers of sub-folders under a single directory.我在单个目录下的不同层子文件夹中包含许多文件(Excel/CSV)。 Many of the files contain a single cell with a common text string, which all need to be updated to a new string.许多文件包含一个带有公共文本字符串的单元格,所有这些都需要更新为新字符串。 The files all have different names.这些文件都有不同的名称。 How difficult would it be to create a (preferably batch) script to search the entire directory (inclusive of sub-directories) for a given text string, replace all instances with a new string, and leave everything else in its place.创建一个(最好是批处理)脚本来搜索给定文本字符串的整个目录(包括子目录),用新字符串替换所有实例,并将其他所有内容保留在原处,这有多困难。

I am new to scripting, I have been searching the site and haven't found a solution that has worked for me.我是脚本的新手,我一直在搜索该站点,但没有找到适合我的解决方案。 I want to stress that I cannot download, install or run third-party software due to security measures, and so applications like FART are out.我想强调的是,由于安全措施,我无法下载、安装或运行第三方软件,因此像 FART 这样的应用程序已被淘汰。 Is anybody able to provide and input for the creation of something like this, or link me to such a script that already exists?有没有人能够提供和输入创建这样的东西,或者将我链接到这样一个已经存在的脚本? Thanks in advance!!提前致谢!!

Robust text editing using pure batch is difficult and slow.使用纯批处理进行稳健的文本编辑既困难又缓慢。

Unless your admin techs have disabled CSCRIPT, you can use my JREPL.BAT - a hybrid JScript/batch text processing utility.除非您的管理技术人员禁用了 CSCRIPT,否则您可以使用我的JREPL.BAT - 混合 JScript/批处理文本处理实用程序。

There is no download or installation process required.无需下载或安装过程。 JREPL.BAT is pure script that runs natively on any Windows machine from XP onward. JREPL.BAT 是纯脚本,可​​以在 XP 以后的任何 Windows 机器上本地运行。 Simply copy the script into a new file named JREPL.BAT on your local machine.只需将脚本复制到本地机器上名为 JREPL.BAT 的新文件中即可。

Once you have your own copy, then all you need is something like the following command, run from the command console:拥有自己的副本后,您只需要从命令控制台运行以下命令即可:

for /r "c:\your\root\path" %F in (*.csv) do @jrepl "search string" "replace string" /L /F "%F" /O -

I used the /L switch to treat the search as a literal.我使用 /L 开关将搜索视为文字。 You may want to drop the /L switch and do a regular expression search instead.您可能想要删除 /L 开关并改为执行正则表达式搜索。

If you put the command within another script, then you will need to double the percents and use CALL JREPL.如果将命令放在另一个脚本中,则需要将百分比加倍并使用 CALL JREPL。

@echo off
for /r "c:\your\root\path" %%F in (*.csv) do call jrepl "search string" "replace string" /L /F "%%F" /O -

Issue the following command from the console prompt to get full documentation:从控制台提示符发出以下命令以获取完整文档:

jrepl /? | more

I configure my console window with a large buffer height so I can scroll back to see prior output, thus I don't need | more我用一个大的缓冲区高度配置了我的控制台窗口,这样我就可以向后滚动以查看先前的输出,因此我不需要| more | more when I look at the help. | more的时候我帮忙看一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM