简体   繁体   English

Bat脚本可查找和替换多个子文件夹中的文件-将.java文件替换为特定文件夹中的.class文件

[英]Bat script to find and replace files in multiple subfolders - replace .java files with .class files from specific folder

I am new to windows batch scripting .. please help on this scenario 我是Windows批处理脚本的新手..请在这种情况下提供帮助

I have file structures as below :: 我的文件结构如下:

dir1 : c:\\workspace\\changeset\\com\\folder dir1 :c:\\ workspace \\ changeset \\ com \\ folder

subfolder1 子文件夹1

  • one.java 一个.java

subfolder-2 子文件夹2

  • two.java 二.java

dir2 : c:\\workspace\\target\\class\\com\\folder dir2 :c:\\ workspace \\ target \\ class \\ com \\ folder

subfolder1 子文件夹1

  • one.class 一类

subfolder2 子文件夹2

  • two.class 二班

Subfolder3 子文件夹3

  • three.class 三班

Need to find and replace dir1 files in respective subfolders ie one.java and two.java from dir2 files ie one.class and two.class ( need to replace certain .java files with .class files from specific folder ) 需要在dir2文件(即one.class和two.class)的相应子文件夹(即one.java和two.java)中查找并替换dir1文件(需要用特定文件夹中的.class文件替换某些.java文件)

much appreciated for your help. 非常感谢您的帮助。

Thanks Arjun 感谢Arjun

for /f "delims=" %%a in ('dir /b /a-d "c:\workspace\changeset\com\folder\*.java"') do (
 if exist "c:\workspace\target\class\com\folder\%%~na.class" (
  echo copy "c:\workspace\target\class\com\folder\%%~na.class" "c:\workspace\changeset\com\folder\%%a")
)

The required COPY commands are merely ECHO ed for testing purposes. 所需的COPY命令仅仅ECHO编用于测试目的。 After you've verified that the commands are correct , change ECHO COPY to COPY to actually copy the files. 验证命令正确无误后 ,将ECHO COPY更改为COPY以实际复制文件。 Append >nul to suppress report messages (eg. 1 file copied ) 附加>nul以禁止显示报告消息(例如, 1 file copied

Note that execution directly from the prompt and as lines with a batch file are different. 请注意,直接从提示符处执行和带有批处理文件的行是不同的。 The metavariable (loop-control variable) %%x must be referenced as %%x for a batch line and %x if executed from the command prompt. 所述metavariable (循环控制变量) %%x必须以引用%%x为一批线和%x如果从命令提示来执行。 Since it makes little sense to repeatedly execute a line containing a for from the prompt (it's easier to create a batch file), I post the batch-file version. 由于从提示重复执行包含for的行几乎没有意义(创建批处理文件更容易),因此我发布了批处理文件版本。 User's responsibility to adjust for direct-from-prompt if desired. 如果需要,用户有责任根据提示进行直接调整。

Read each filename in /b basic form /ad without directories and assign the filename+extension to %%a . /b基本格式/ad读取每个没有目录的文件名,并将文件名+扩展名分配给%%a

If a file in the other directory called thenamepartofthefile .class exists, then copy that file to the first directory. 如果存在另一个目录中名为文件 .class的名称的文件,则将该文件复制到第一个目录。


Please post the entire problem to be solved. 请发布要解决的整个问题。 The approach can change radically, as in this case. 在这种情况下,方法可能会发生根本变化。

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir1=U:\sourcedir\changeset"
SET "sourcedir2=U:\sourcedir\target"
FOR /f "delims=" %%a IN (
 'dir /b /s /a-d "%sourcedir1%\*.java" '
 ) DO (
 SET "javadir=%%~dpa"
 SET "classfile=!javadir:%sourcedir1%=%sourcedir2%!%%~na.class"
 IF EXIST !classfile! ECHO COPY "!classfile!" "%%a"
)

GOTO :EOF

You would need to change the settings of sourcedir1 and sourcedir2 to suit your circumstances. 您需要更改sourcedir1sourcedir2的设置以适合您的情况。

Essentially, the same approach and the same comments re messages. 从本质上讲,相同的方法和相同的注释会重现消息。 The difference is that this procedure uses files and subdirectories in the dir list and substitutes the first part of the directoryname in deriving the expected name of the .class file. 不同之处在于此过程使用dir列表中的文件和子目录,并在导出.class文件的预期名称时替换目录名的第一部分。

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

相关问题 windows bat script find替换文件夹和子文件夹中的每个文件 - windows bat script find replace every file in folder and subfolders 在多个文件中查找并替换 - find and replace in in multiple files 批处理脚本替换多个文件中的特定字符串 - Batch script to replace specific string in multiple files 我应该怎样写.bat文件才能找到文件夹中的所有文件,并将其替换为另一个文件? - What should I write into the .bat file for it to find all files in folder and replace them with file from another? BAT脚本按文件名将文件移动到子文件夹中 - BAT script to move files into subfolders by file name 用多个目录中的文件替换 - replace with files from multiple directories 递归查找和替换文件 - Recursively find and replace files Powershell 将特定文件从所有子文件夹复制到单个文件夹 - Powershell copying specific files from all subfolders to a single folder 我坚持使用 powershell 脚本,该脚本必须将多个子文件夹中的所有文件压缩到目标文件夹 - Im stuck on a powershell script that has to compress all files from multiple subfolders to a destination folder 创建Bat文件以对文件夹和子文件夹中的所有文件执行命令 - Creating Bat file to execute a command on all files in folder and subfolders
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM