修改for循环批处理文件中的变量

时间:2013-10-10 17:24:34

标签: windows batch-file for-loop path

您好我正在尝试创建一个通过目录挖掘的批处理文件,并检查其子目录中的所有文件是否与另一个目录中的文件相同(具有相同的名称和相同的子目录名称)

前:

C:/Users/Administrator/desktop/directory1/global/config/project.config

C:/directory2/global/config/project.config

我能够获取需要与for循环进行比较的所有文件 echo %% f给了我一条路径:

C:/Users/Administrator/directory1/global/config/project.config

但我想删除开头并将其存储在一个新变量中,以便我可以轻松地将文件与FC命令进行比较。在for循环中我不知道如何正确地进行字符串替换。我想得到这个:

/global/config/project.config

我现在的代码

set b="c:\Users\Administrator\desktop\"
set j=""
for /f %%f in ('dir /a:-d /s /b /r c:\Users\Administrator\desktop\global') do (
set c=%%f
%c:b=j%
echo %%c
)

2 个答案:

答案 0 :(得分:3)

@echo off
set "dir1=c:\smth"
set "dir2=c:\dir\smth2"

setlocal enableDelyaedExpansion
for /f %%F in ('dir  /a:-d /s /b /r "%dir1%"') do (
  set "file_path=%%F"
  if not exist "!file_path:%dir1%=%dir2%!" (
     echo file %%F does not exists in %dir2% 
  )
)
endlocal

答案 1 :(得分:0)

使用通话功能的更好方法

setlocal enableDelyaedExpansion
for /f %%F in ('dir  /a:-d /s /b /r "%dir1%"') do call :foo %%F

:foo 
set "file_path=%1"
if not exist "!file_path:%dir1%=%dir2%!" (
echo file %1 does not exists in %dir2% 
)