简体   繁体   English

windows bat script find替换文件夹和子文件夹中的每个文件

[英]windows bat script find replace every file in folder and subfolders

First of all let me just say that I'm completely new to scripting. 首先,我要说的是,我对脚本完全不熟悉。

I'm trying to develop a script which will find a given string and replace it with another given string. 我正在尝试开发一个脚本,它将找到一个给定的字符串并用另一个给定的字符串替换它。 It must do this for every file in the folder/subfolders. 必须对文件夹/子文件夹中的每个文件执行此操作。

This must be a bat file. 这必须是一个bat文件。 Here is what I have so far: 这是我到目前为止:

@echo off
set RESULT_FILE="result.txt"
set /p "var1=Enter the String to Find: "
set /p "var2=Enter the String to Replace with: "

pushd %~p0
for /f "delims=" %%a in ('dir /B /S *.js') do (
  for /f "tokens=3 delims=:" %%c in ('find /i /c "%var1%" "%%a"') do (
    if %%c neq 0 (
        echo %var1%
    )
 )
) 

popd

This is returning the found variable, I'm just struggling on how I can replace it. 这是返回找到的变量,我只是在努力如何替换它。

Thanks in advance for any help. 在此先感谢您的帮助。

Can you use sed for Windows? 你可以在Windows上使用sed吗?

for /f "delims=" %%a in ('dir /B /S /A-D *.js') do sed -ibak "s/%var1%/%var2%/g" "%%~fa"
Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...

  -n, --quiet, --silent
                 suppress automatic printing of pattern space
  -e script, --expression=script
                 add the script to the commands to be executed
  -f script-file, --file=script-file
                 add the contents of script-file to the commands to be executed
  -i[suffix], --in-place[=suffix]
                 edit files in place (makes backup if extension supplied)
  -l N, --line-length=N
                 specify the desired line-wrap length for the `l' command
  -r, --regexp-extended
                 use extended regular expressions in the script.
  -s, --separate
                 consider files as separate rather than as a single continuous
                 long stream.
  --text     switch to text mode
  -u, --unbuffered
                 load minimal amounts of data from the input files and flush
                 the output buffers more often
      --help     display this help and exit
  -V, --version  output version information and exit

If no -e, --expression, -f, or --file option is given, then the first
non-option argument is taken as the sed script to interpret.  All
remaining arguments are names of input files; if no input files are
specified, then the standard input is read.

The link below might be of some help. 以下链接可能会有所帮助。 The proposed solution is to create a new file with the replaced strings, and to delete the old file. 建议的解决方案是使用替换的字符串创建一个新文件,并删除旧文件。

How to replace substrings in windows batch file 如何在Windows批处理文件中替换子字符串

With fart , you can do what you want in a single line of windows bat script. 使用fart ,您可以在一行Windows bat脚本中执行您想要的操作。

fart.exe -r "C:\path\to\folder\*.*" string_to_be_replaced replace_string 

where -r stands for Process sub-folders recursively . 其中-r代表Process sub-folders recursively

暂无
暂无

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

相关问题 Bat脚本可查找和替换多个子文件夹中的文件-将.java文件替换为特定文件夹中的.class文件 - Bat script to find and replace files in multiple subfolders - replace .java files with .class files from specific folder bat函数用于在文件夹和子文件夹中查找文件并对其执行某些操作。 - bat function to find a file in folder and subfolders and do something with it. BAT脚本按文件名将文件移动到子文件夹中 - BAT script to move files into subfolders by file name 如何在Windows bat脚本中每1分钟检查一次文件? - How to check a file every 1 minute in a windows bat script? Windows bat文件按名称查找文件夹并返回其路径 - windows bat file to find a Folder by name and return its path 创建Bat文件以对文件夹和子文件夹中的所有文件执行命令 - Creating Bat file to execute a command on all files in folder and subfolders 查找并替换脚本后运行bat命令 - Running a bat command after find and replace script 我应该怎样写.bat文件才能找到文件夹中的所有文件,并将其替换为另一个文件? - What should I write into the .bat file for it to find all files in folder and replace them with file from another? 脚本蝙蝠,Windows找不到.exe - Script bat, Windows can not find .exe Windows 10.bat 脚本,用于映射包含与其 AD 用户名同名的文件夹的文件共享 - Windows 10 .bat script for mapping a file share that contains a folder the same name as their A.D. username
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM