Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi,
I've tried to answer my own question by reading through other posts but can't find what I'm looking for.I have a folder, c:\test, that contains many files with sequential numerical filenames with an .xml file extension. Files with the same name but with a .sem file extension could exist in another folder, c:\test\temp.
What I need is a batch file that will look to see if a .sem file exists in c:\test\temp with the same name as a .xml file in c:\test. If a .sem file does exist then do nothing, else do something. The do something part I've already written.
I picked this up from another post to try and get the compare to work but I don't know how to address the file extensions so it will compare how I want it to.
@echo on
for /f %%a IN ('dir "C:\test\%%~Na.xml" /b') do set file1=%%a
for /f %%b IN ('dir "C:\test\temp\%%~Nb.sem" /b') do set file2=%%bif %file1%==%file2% goto same
echo different filenames
goto end:same
echo same file names
goto end:end
Thanks in advance for any help.

Completely untested and quite inefficient, that said I think it will work.
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
for /f %%a in ('dir /b /a-d c:\test') do (
set sw=0
for /f %%b in ('dir /b /a-d c:\test\temp') do (
if "%%~na"=="%%~nb" set sw=1
)
if !sw! equ 0 (
this is where all
your stuff could go
)
)You may want to check out for /?, it has some really good info....

Thanks Judago! That worked perfectly with a little modification of my existing code.
For anyone who's interested here's my final script and an explanation of what it does.
It looks in a specified directory for a file and then looks to see if a file of the same name exists in another specified directory. If the file does exist, the script doesn't do anything. If the file doesn't exist it finds lines that begin with specific text within each file, removes them, and then saves the modified file as its original name.
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
for /f %%a in ('dir /b /a-d C:\test') do (
set sw=0
for /f %%b in ('dir /b /a-d C:\test\temp') do (
if "%%~na"=="%%~nb" set sw=1
)
if !sw! equ 0 (
Type "%%~Na.xml" | Find /V "text1" | Find /V "text2" | Find /V "text3" | Find /V "text4" | Find /V "text5" > "%%~Na.new"If exist "%%~Na.bak" Del "%%~Na.bak"
Ren "%%~Na.xml" %%~Na.bak
Ren "%%~Na.new" %%~Na.xml
DEL *.bak
)
)Thanks again Judago

I thought this would have been enough.
for %%a in (*.xml) do if not exist "%%~na.sem" (do something)
--
Holla.[Edited: added quotes to make it complete]

Holla, thanks for your suggestion. I'm a novice when it comes to batch files but looking at your suggestion it doesn't take into account that the files live in different directories? Can that be easily incorporated into your suggestion?

Yes,
If the files are in different directories,
for %%a in (Directory1\*.xml) do if not exist "Directory2\%%~na.sem" (do something)
where Directory1 is where xml is expected and Directory2 is where .sem files are expected.--
Holla.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |