1.

Solve : command line syntax?

Answer»

If exist 1.txt and 1.doc del 1.txt

Would this be the right command line syntax to find out if both a .txt and a .doc version of the same file name exists in a directory and then delete the file with the .txt extention if both exist?You could try this:

Code: [Select]if exist file.txt (
if exist file.doc del file.doc
)Yes, thanks. I tried that for one filename and it works but what I really meant to ask was how to do all of the files in a directory? Would I proceed the if exist statement with DIR > and if so what kind of parameter would I use in the if exist statement to receive the directory statement output?Quote from: Dizzzy on June 16, 2008, 12:05:19 AM
I really meant to ask was how to do all of the files in a directory?

DEL *.* does that.

Quote from: Dias de verano on June 16, 2008, 12:17:03 AM
Quote from: Dizzzy on June 16, 2008, 12:05:19 AM
I really meant to ask was how to do all of the files in a directory?

DEL *.* does that.



You have taken my statement out of context and then provided an mindless and irrelevant response.

DEL *.* does not do what I asked.

DEL *.* does not test all filenames to see if a particular file extension exists and then delete a different file extension for the same filename. Quote from: Dizzzy on June 24, 2008, 11:45:33 PM
You have taken my statement out of context and then provided an mindless and irrelevant response.

I understand now. Sorry. I do know an answer to your query, but since you were so rude, I shall not bother posting it. Good luck.One reason people ask and answer questions on a public forum is so that other members can benefit from the question and answers as well.

Your answer was serious enough to put everyone at risk. If it was only a thoughtless answer without intent of causing harm you would have corrected it.

I couldn't think of a way to do this with a batch file, but I made a VBScript that should do the job sufficiently.

I apologize for it's length. It probably could have been shorter, but it worked for all my test cases I could contrive.
I recommend you back up the files before running this script on them. I don't want to be responsible for loss of important data.


Just Copy & Paste it into a script file- I recommend extvdel.vbs, but anything goes, as long as you remember the filename.

Code: [Select]


'extvdel VBScript program

'given two extensions, deletes all files in the current directory that have the SECOND extension but only if a file with the same base name and the first extensions
'also exists.


Dim FSO
Dim FileLoop,Folduse,outstream
Dim Extension1,Extension2
wscript.echo "HI!"
If Wscript.arguments.count < 2 then
ShowHelp
WScript.Quit()
End If
Extension1 = Wscript.Arguments(0)
Extension2 = WScript.Arguments(1)
If Extension1 = "/?" then
showhelp
WScript.Quit
end if
Set FSO = CreateObject("Scripting.FileSystemObject")
set outstream = FSO.getStandardStream(1) 'retrieve the stdout stream
set folduse = FSO.GetFolder(FSO.GetAbsolutePathName(".") ) 'retrieve current directory.
For each FileLoop in folduse.Files
if FSO.getExtensionName(fileLoop.name) = Extension1 Then
'determine if a Doc file exists with the same base name.
'on error resume next
wscript.echo "found """ & fileloop.name & """. Finding:""" & FSO.GetBaseName(fileloop.name) & "." & extension2 & """..."
wscript.echo
on error resume next
if not folduse.Files(FSO.GetBaseName(fileloop.name) & "." & extension2) is nothing then
If Err.Number = 0 then


on error goto 0
Wscript.echo "deleting file, " & fso.getbasename(fileloop.name) & "." & Extension2
' on error goto 0
folduse.Files(FSO.getbaseName(fileloop.name) & "." & extension2).Delete
else
wscript.echo """" & fso.getbasename(fileloop.name) & "." & Extension2 & """ not found."
end if 'err
end if
end if

next


Sub showhelp
Wscript.Echo "Extension verifier/Deleter"
Wscript.echo "determines if files with the same basename and two different extensions exist,"
Wscript.echo "And of so, Deletes one of them."
Wscript.echo ""
wscript.echo "Usage: ExtVdel.vbs <extension1> <Extension2>"
wscript.echo "(might require CScript extvdel.vbs <extension1> <extension2>"
Wscript.echo ""
Wscript.echo "For example, ExtVDel txt doc"
Wscript.echo "the second parameter is the extension whose file will be deleted."
end Sub




I won't go to in-depth into it, but for some reason I felt compelled to have it work for other extensions.
just save it to the folder you wish to operate on, and run "Cscript extvdel.vbs txt doc" to get it to run on your files there. In the future if you need similiar functionality with other file types, you can just change the txt and doc portions of the commandline.


I hope this works for you...


And Dias- how was he rude? he only said anything even CLOSE to offensive after you contrived a new question out of a contextually removed sentence of one of his posts, and even then he only told it like it was that is, "mindless and irrelevant", and even if that was arguable, one thing definitely stands- it didn't help, and as far as I can see that was the whole reason he posted here in the first place (once again, )

You are right I was out of order and I apologise.

I hope this might make up for it in a small way

I called it dnam.bat

Code: [Select]@echo off
REM Find files with same name but 2 extensions
REM 2 parameters from command line
REM %1 extension to look for & keep
REM %2 extension to look for & kill
REM E.g. dnam doc txt
REM For each doc file delete a txt file
REM that has same name part, if it exists
set ext1=%1
set ext2=%2
for /F "delims==" %%A in ('dir /b /a-d *.%ext1%') do (
if exist "%%~nA.%ext2%" (
echo found %%A. Deleting %%~nA.%ext2%
del %%~nA.%ext2%
)
)
Quote from: BC_Programmer on June 26, 2008, 09:42:19 PM
I couldn't think of a way to do this with a batch file

Call me old fashioned- I haven't read up on these new thingies they added for batch commands and such- I just go with what I know for DOS 6.0



Mine may be longer, but, do I get points for having it look more complicated? I wasted a good hour learning the WSH objects just to make it... and I just read it over...

Quote from: BC_Programmer on June 26, 2008, 09:42:19 PM
Code: [Select]set outstream = FSO.getStandardStream(1) 'retrieve the stdout stream
why the heck did I put that there? Completely pointless...



Quote from: BC_Programmer on June 27, 2008, 10:45:02 AM

Mine may be longer, but, do I get points for having it look more complicated? I wasted a good hour learning the WSH objects just to make it... and I just read it over...




I deliberately made mine verbose but you can fit it all in one line

Code: [Select]for /f "delims==" %%A in ('dir /b /a-d *.%1') do if exist "%%~nA.%2" del "%%~nA.%2"&& echo deleted %%~nA.%2
Batch language is very good at doing the things it was designed for, and can be beautifully concise IMHO.
Hey BC,
Please take a look at your last post. (Reply #9)
It has an extra end quote symbol at the end of the message.
I think that is why the screen is so wide.
Please modify & correct.
Thanks!


Quote from: BC_Programmer on June 27, 2008, 10:45:02 AM
Call me old fashioned- ... I just go with what I know for DOS 6.0

I certainly wouldn't consider your VBScript to be "old fashioned" I am way behind on newer DOS batch programming also, and have a hard time following the loops that Dias creates, but the VBScript is even more difficult for me to follow. You must have spent a considerable amount of time learning it.

I personally favor sticking with the batchfile programming if at all possible, because of the fact that most of the "mainstream" antivirus software throws up errors when you run VBScript (like sendkeys). Yes, you can click that it's OK to run it, but the message will scare the "H" out of most users. They will think that you have infected their computer. While that is not true, the average user will not be able to handle the error message without a lot of panic.Quote from: llmeyer1000 on June 27, 2008, 11:18:53 AM
Hey BC,
Please take a look at your last post. (Reply #9)
It has an extra end quote symbol at the end of the message.
I think that is why the screen is so wide.
Please modify & correct.
Thanks!

Fixed. I think. still looks wide to me though, not sure.
Quote from: llmeyer1000 on June 27, 2008, 11:18:53 AM


I certainly wouldn't consider your VBScript to be "old fashioned" I am way behind on newer DOS batch programming also, and have a hard time following the loops that Dias creates, but the VBScript is even more difficult for me to follow. You must have spent a considerable amount of time learning it.
I didn't spend any time learning VBScript per se, but rather Visual Basic 6.0, if that Script makes your head spin, I'd hate to see the mess when you view the code in the Class module I felt compelled to attach... You can't do anything with it, since you don't have the evaluation Library, to which the class is just a PLUGIN. "just" a plugin. performs the core operations/Functions for the parser itself. Imagine, if you will, some of THAT String parsing.

Quote from: llmeyer1000 on June 27, 2008, 11:18:53 AM
I personally favor sticking with the batchfile programming if at all possible, because of the fact that most of the "mainstream" antivirus software throws up errors when you run VBScript (like sendkeys). Yes, you can click that it's OK to run it, but the message will scare the "H" out of most users. They will think that you have infected their computer. While that is not true, the average user will not be able to handle the error message without a lot of panic.

that's the main reason I tried to write a batch program first. I don't know any of this new-fangled batch stuff, so I couldn't FIGURE out a way to do it in my (comparatively limited) DOS 6.0 Batch knowledge. I think I got to, "For %%P in (*.txt) do"... and then I realized, that I couldn't do string manip. So I started a Script. That was my very first shell script

[recovering disk space -- attachment deleted by admin]Quote from: BC_Programmer on June 27, 2008, 11:58:24 AM
I didn't spend any time learning VBScript per se, but rather Visual Basic 6.0, if that Script makes your head spin, I'd hate to see the mess when you view the code in the Class module I felt compelled to attach... You can't do anything with it, since you don't have the evaluation Library, to which the class is just a PLUGIN. "just" a plugin. performs the core operations/Functions for the parser itself. Imagine, if you will, some of THAT String parsing.

Like I said,

Quote
Batch language is very good at doing the things it was designed for, and can be beautifully concise IMHO.
BC, It looks like Reply #9 is OK now, but there is still something wrong somewhere, making the screen so wide. It's probably something I did in one of mine, but I can't find it. I don't have time to look for it anymore. (It could be anywhere. When I found your extra end quote code, I thought that had to be it.)

Help! Someone.


Discussion

No Comment Found