简体   繁体   中英

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. 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. 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.

There is no download or installation process required. JREPL.BAT is pure script that runs natively on any Windows machine from XP onward. Simply copy the script into a new file named JREPL.BAT on your local machine.

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. You may want to drop the /L switch and do a regular expression search instead.

If you put the command within another script, then you will need to double the percents and use 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 when I look at the help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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