

InterviewSolution
Saved Bookmarks
1. |
Solve : File name problem...? |
Answer» <html><body><p>I have a batch file "Test.bat":<br/> Code: <a>[Select]</a>echo on<br/>msg * This is a test.<br/>del Test.bat<br/>pause<br/><br/>Well this script looks fine but if i change the file name "Test.bat" to "Testing.bat" (Not the name in script).<br/>The script seems not to work. The error is:<br/>Del Test.bat<br/>Could Not Find C:\Test.bat<br/><br/>I need a script that can detect its own file name.<br/>Could anyone help me find a way out of this problem?As long has you have not used the SHIFT command, then<br/>%~f0<br/>refers to the fully qualified name of the batch file<br/>Thats a zero at the end btwI am sure a script can detect itself,<br/>and can even copy itself,<br/>but I would expect the O.S. to <a href="https://interviewquestions.tuteehub.com/tag/consider-2017521" style="font-weight:bold;" target="_blank" title="Click to know more about CONSIDER">CONSIDER</a> the file in use if it tried to move or delete itself.<br/><br/>Alan<br/>It is certainly possible for a batch file to delete or rename itself, but there will be an error message when cmd.exe comes back from carrying out the del or ren command to find the script gone or renamed.<br/><br/>consider...<br/><br/> Code: <a>[Select]</a>C:\>echo del %~f0>test.bat<br/><br/>C:\>test.bat<br/><br/>C:\>del test.bat<br/>The batch file cannot be found.<br/>and...<br/><br/> Code: <a>[Select]</a>C:\>echo ren %~f0 test1.bat>test.bat<br/><br/>C:\>test.bat<br/><br/>C:\>ren test.bat test1.bat<br/>The batch file cannot be found.<br/><br/><br/><br/> Quote from: ALAN_BR on September 24, 2010, 02:08:19 PM</p><blockquote>I am sure a script can detect itself,<br/>and can even copy itself,<br/>but I would expect the O.S. to consider the file in use if it tried to move or delete itself.<br/><br/>Alan<br/><br/></blockquote> <br/>Simply opening a file doesn't constitute "in-use".<br/><br/>The call to CreateFile() would need to explicitly lock the file, by not specifying any of the FILE_SHARE_X flags.<br/><br/>For example, if the CreateFile() is passed FILE_SHARE_READ, other programs can open the file for reading, but cannot write to the file or open it with write flags, or delete the file. FILE_SHARE_WRITE will allow programs to write to the file but not read or delete it, and FILE_SHARE_DELETE will allow the file to be deleted.<br/><br/><a href="https://blogs.msdn.com/b/larryosterman/archive/2004/05/13/131263.aspx">Larry Osterman</a> writes about this, and although he doesn't explicitly note anything to do with batch files there is an implication there; he notes that by default, DOS applications would open files in a manner so that any other program could access them, because of the single-tasking nature of DOS nobody considered this to be a problem. Therefore it's safe to assume that CMD.EXE opens batch files in the same mode that command.com does- that is, in a mode whereby the file is free to be changed (FILE_SHARE_WRITE+FILE_SHARE_READ+FILE_SHARE_DELETE). <br/><br/>He also mentions that the NT Loader will open Executables with the FILE_SHARE_DELETE flag; this means that the file cannot be opened for READ or WRITE access by any program. additional invocations of the executable don't cause a problem, because the loader simply uses the same Mapped View of the executable file that it created the first time around. <br/><br/>It's not as cut-and-dried as "if the file is open, no other program can access it".<br/><br/>Wether that is truly the case for cmd, I don't know. It's entirely possible it opens the batch file, reads the next line, and then closes it, too.Sorry, you are right and I was wrong.<br/><br/>I was remembering past experiences before CMD.EXE and before Windows 98,<br/>possibly back in the days of DOS v3.?,<br/>and I learnt then to expect grief from a *.BAT script if it tried to alter itself.<br/>I assumed the same was true now.<br/><br/>My more recent experience with XP is that the O.S. may complain that a file is in use when I try to delete it,<br/>so I thought my old memories still applied.<br/><br/>Sorry<br/><br/>Regards<br/>Alan<br/> Quote from: ALAN_BR on September 25, 2010, 02:03:18 AM<blockquote>I was remembering past experiences before CMD.EXE and before Windows 98,<br/>possibly back in the days of DOS v3.?,<br/>and I learnt then to expect grief from a *.BAT script if it tried to alter itself.<br/>I assumed the same was true now.<br/><br/>My more recent experience with XP is that the O.S. may complain that a file is in use when I try to delete it,<br/>so I thought my old memories still applied.<br/><br/>Sorry<br/><br/>Regards<br/>Alan<br/></blockquote> <br/>Alan, this is just my 2 (euro) cents <a href="https://interviewquestions.tuteehub.com/tag/worth-1461684" style="font-weight:bold;" target="_blank" title="Click to know more about WORTH">WORTH</a>, but I don't really think you have any reason to apologise or say 'sorry' to anybody over this. You may have been inaccurate to say that a batch script is prevented from modifying or deleting itself, but you are certainly correct in your recollection that writing self-modifying code can be a risky business. A script that cuts the legs from under itself can lurch to <a href="https://interviewquestions.tuteehub.com/tag/destruction-949662" style="font-weight:bold;" target="_blank" title="Click to know more about DESTRUCTION">DESTRUCTION</a>. Having said that, self-modification is not always a bad thing. There is a good summary here<br/><br/><a href="https://en.wikipedia.org/wiki/Self-modifying_code">http://en.wikipedia.org/wiki/Self-modifying_code</a><br/><br/>As Wikipedia notes, "Because of the security implications of self-modifying code, all of the major operating systems are careful to remove such vulnerabilities as they become known. The concern is typically not that programs will intentionally modify themselves, but that they could be maliciously changed by an exploit." and "Self-modifying code is also sometimes used by programs that do not want to reveal their presence — such as computer viruses and some shellcodes. Viruses and shellcodes that use self-modifying code mostly do this in combination with polymorphic code. Modifying a piece of running code is also used in certain attacks, such as buffer overflows."<br/><br/>In this regard, I am curious about the intentions of the OP.<br/><br/><br/><br/><br/><br/><br/>Thank you Salmon<br/><br/>I remember the good old days when an anti-virus was pleased with itself if it could find and delete a suspect batch file.<br/>The I.T. department chose one that excelled in deleting any batch file that had the command FORMAT C:\<br/><br/>I had problems with self modifying code.<br/>I created a batch file called CD#.bat which <a href="https://interviewquestions.tuteehub.com/tag/looked-1733988" style="font-weight:bold;" target="_blank" title="Click to know more about LOOKED">LOOKED</a> for arguments,<br/>and in the absence of arguments would skip to the end and execute the final line,<br/>which was something like<br/>CD C:\z\z\z\z\z\z\z\z\z\z\z\z\z\z\z\z\z\z\<br/>If it was given the argument '#' it would alter the final line to aim at whatever the current path was,<br/>and the next day I immediately returned to yesterday's weird and wonderful directory with<br/>CD#<br/>Typically the new path was one extra 'z\'<br/>I tried to design self modifying code but failed.<br/>I finished with a "template" which CD#.bat duplicated as CD#.TMP<br/>The template was identical to CD#.BAT excepting the final line was "CD " without any CRLF or CtrlZ,<br/>then CD#.bat invoked<br/>CD >> CD#.TMP<br/>Then CD#.BAT deleted itself and renamed CD#.TMP as CD#.BAT<br/>That at least was the result I wanted, but achieving the result involved the use of intermediate scripts.<br/><br/>The purpose of C:\z\z\z\z\z\z\z etc was that the first z\ held the first stage of a software product,<br/>and successive z\ held the previous plus a new feature,<br/>and the final z\ held the final product with all features.<br/>If a feature was found to be broken it was easy to test its operation at each z\ to identify what broke it and then fix it.<br/>This was when CVS etc version control was not available, and every man had to "roll his own".<br/><br/>I remember the aggravation when the I.T. dept changed to an anti-virus which deleted my master template.<br/>It assumed that any *.BAT was up to no good and had to be deleted if it had a final command without a terminating CRLF<br/><br/>I remember the wonderful feeling of revenge when the IT department went round one night with a FAST application to detect any unlicensed software,<br/>and the FAST application choked when it <a href="https://interviewquestions.tuteehub.com/tag/encountered-446037" style="font-weight:bold;" target="_blank" title="Click to know more about ENCOUNTERED">ENCOUNTERED</a> my path with 80 or more 'z\' in a line.<br/>(I forget what the character length was for a command.com command, but I took my path up to the limit)<br/>This is a memory worth keeping ! !<br/><br/>Regards<br/>Alan<br/></body></html> | |