[英]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
subfolder-2 子文件夹2
dir2 : c:\\workspace\\target\\class\\com\\folder dir2 :c:\\ workspace \\ target \\ class \\ com \\ folder
subfolder1 子文件夹1
subfolder2 子文件夹2
Subfolder3 子文件夹3
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. 您需要更改sourcedir1
和sourcedir2
的设置以适合您的情况。
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.