1

Hi I'm trying to make a batch file that digs thru a directory and checks if all the files in its sub-directories are the same as in another directory (with the same name and same sub-directory names)

ex:

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

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

I'm able to fetch all the files that need to be compared with a for loop The echo %%f gives me a path like:

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

But I want to remove the beginning and store it in a new variable so I can easily compare files with an FC command. I don't know how to correctly do a string substitution when in a for loop. I want to get just this:

/global/config/project.config

my code for now

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
)
5
  • Strong suggestion: DON'T do this with a .bat file. Use a VBScript, or Powershell. Or Python or Perl. Or C# or VB.Net. Anything but DOS .bat files. You'll be much happier - and much more productive - in the long run. IMHO... Commented Oct 10, 2013 at 17:29
  • PS: If you really must, this might help: dostips.com/DtTipsStringManipulation.php Commented Oct 10, 2013 at 17:31
  • I agree... But I'm expanding an already existing batch file... I don't want to start launching other stuff thru this .bat I could create a .vbs in temp and insert the code lines thru the bat file but I want to stay kosher and stick with dos commands. Commented Oct 10, 2013 at 17:45
  • you need setlocal enableDelayedExpansion -> ss64.com/nt/delayedexpansion.html Commented Oct 10, 2013 at 18:19
  • how do you get only the path without the filename after? I tried doing this but doesnt work set "j=!file_path:%dir1%=%dir2%!" then echo !~pj! Commented Oct 11, 2013 at 15:01

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

0

The better way to use a call function

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% 
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.