Computing.Net > Forums > Programming > Batch file, if filename exists

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch file, if filename exists

Reply to Message Icon

Name: Monkey
Date: December 11, 2008 at 03:13:31 Pacific
OS: WinXP. DOS
CPU/Ram: ....
Product: .... / ....
Comment:

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=%%b

if %file1%==%file2% goto same

echo different filenames
goto end

:same
echo same file names
goto end

:end

Thanks in advance for any help.



Sponsored Link
Ads by Google

Response Number 1
Name: Judago
Date: December 11, 2008 at 05:40:43 Pacific
Reply:

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....


0

Response Number 2
Name: Monkey
Date: December 11, 2008 at 07:42:17 Pacific
Reply:

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


0

Response Number 3
Name: Holla
Date: December 11, 2008 at 22:41:30 Pacific
Reply:

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]


0

Response Number 4
Name: Monkey
Date: December 12, 2008 at 03:10:51 Pacific
Reply:

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?


0

Response Number 5
Name: Holla
Date: December 12, 2008 at 04:32:04 Pacific
Reply:

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.


0

Related Posts

See More



Response Number 6
Name: Judago
Date: December 12, 2008 at 20:06:50 Pacific
Reply:

Geez, I was really over complicating things. Good one holla, much more efficient.


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Batch file, if filename exists

batch file parameters question www.computing.net/answers/programming/batch-file-parameters-question/15242.html

if filename exist, append it www.computing.net/answers/programming/if-filename-exist-append-it/18634.html

path problem in batch file... www.computing.net/answers/programming/path-problem-in-batch-file/9916.html