write DEL 0% to other batch to delete itself

January 4, 2011 at 14:01:45
Specs: Windows XP
Hi.

I created c:\x.bat for testing purposes with following lines:

echo start c:\monday.html >> c:\test.bat
echo del 0% >> c:\test.bat

but when opening test.bat second line is
del "C:\x.bat"
not
del 0%


what's wrong with the syntax?

thanks


See More: write DEL 0% to other batch to delete itself

Report •

#1
January 4, 2011 at 14:46:24
0% is a system variable. It represents the first command you put right after the execution command of a the batch file.

What is 0% any ways? This is not a proper name for a file. What is it exactly that you are trying to do?

The DEL command is used for deleting files you know. Is it that you are trying to remove all "0%" characters out of the newly created batch file?

OR

is it that you are trying to make a batch file that looks like this when you are done...

start c:\monday.html >> c:\test.bat
del 0% >> c:\test.bat

If so why are you using a batch file to create a batch file?


Report •

#2
January 4, 2011 at 14:51:57
well the idea was to make so that test.bat will delete itself after runing monday.html

Report •

#3
January 4, 2011 at 14:53:49
then do this...

echo start c:\monday.html >> c:\test.bat
echo del c:\test.bat >> c:\test.bat

don't know why you are making it so hard?

You could also do something like...

Set CreateFile=c:\test.bat
echo start c:\monday.html >> %CreateFile%
echo del %CreateFile% >> %CreateFile%


Report •

Related Solutions

#4
January 4, 2011 at 15:15:16
ace_omega 0% is a system variable. It represents the first command you put right after the execution command of a the batch file.

0% isn't anything. %0 on the other hand is the current batch file being run. %1 will be the first variable unless the shift command is used.

echo start c:\monday.html >> c:\test.bat
echo del ^%0 >> c:\test.bat

Can I put a question here?


Report •

#5
January 4, 2011 at 15:15:29
welll....

i needed to add some crap to other batch files and make so that after running it will delete itself. This was the first idea but will try yours

echo @echo off >> W:\TTT\alfa1.bat
echo cls >> W:\TTT\alfa1.bat
echo color 04 >> W:\TTT\alfa1.bat
echo NET USE P: /DELETE /Y >> W:\TTT\alfa1.bat
echo NET USE P: \\Ser5\public\TTT\1\mnt\disk_u\HOME >> W:\TTT\alfa1.bat
echo if exist "P:\c_data.DA1" goto copy >> W:\TTT\alfa1.bat
echo goto loop >> W:\TTT\alfa1.bat
echo :copy >> W:\TTT\alfa1.bat
echo rmdir T:\ >> W:\TTT\alfa1.bat
echo mkdir T:\ >> W:\TTT\alfa1.bat
echo copy /Y /Z P:\c_data.DA1 T:\\c_data.DA1 >> W:\TTT\alfa1.bat
echo if errorlevel 1 goto loop >> W:\TTT\alfa1.bat
echo move /y T:\HOME\c_data.DA1 T:\HOME\copy\ >> W:\TTT\alfa1.bat
echo move /Y T:\\c_data.DA1 T:\HOME\ >> W:\TTT\alfa1.bat
echo rmdir T:\ >> W:\TTT\alfa1.bat
echo goto end >> W:\TTT\alfa1.bat
echo :copy2 >> W:\TTT\alfa1.bat
echo copy /Y /Z P:\c_data.DA1 T:\\c_data.DA1 >> W:\TTT\alfa1.bat
echo if errorlevel 1 goto loop2 >> W:\TTT\alfa1.bat
echo move /Y T:\\c_data.DA1 T:\HOME\ >> W:\TTT\alfa1.bat
echo rmdir T:\ >> W:\TTT\alfa1.bat
echo goto end >> W:\TTT\alfa1.bat
echo :end >> W:\TTT\alfa1.bat
echo del 0% >> W:\TTT\alfa1.bat
echo exit >> W:\TTT\alfa1.bat
echo :loop >> W:\TTT\alfa1.bat
echo set LogPath=W:\TTT\ >> W:\TTT\alfa1.bat
echo set LogFileExt=.log >> W:\TTT\alfa1.bat
echo set LogFileName=ALFA%LogFileExt% >> W:\TTT\alfa1.bat
echo set MyLogFile=%date% >> W:\TTT\alfa1.bat
echo set MyLogFile=%MyLogFile:/=-% >> W:\TTT\alfa1.bat
echo set MyLogFile=%LogPath%%MyLogFile%_%LogFileName% >> W:\TTT\alfa1.bat
echo echo.%Date% >>"%MyLogFile%" >> W:\TTT\alfa1.bat
echo echo.%Time% >>"%MyLogFile%" >> W:\TTT\alfa1.bat
echo echo.ALFA !!!no connection!!! >>"%MyLogFile%" >> W:\TTT\alfa1.bat
echo goto loop2 >> W:\TTT\alfa1.bat
echo :loop2 >> W:\TTT\alfa1.bat
echo ping -n 5 127.0.0.1 > nul >> W:\TTT\alfa1.bat
echo goto copy2 >> W:\TTT\alfa1.bat


Report •

#6
January 4, 2011 at 15:32:44
You're still using "0%" which is incorrect. The variable you want is "%0", however you will need to escape the percent sign (^%0).

Can I put a question here?


Report •

#7
January 4, 2011 at 15:36:15
C:\>copy con a.cmd
@echo ^%0^Z
        1 file(s) copied.

C:\>a
a

C:\>
Try %% instead.

How To Ask Questions The Smart Way


Report •

#8
January 4, 2011 at 15:52:23
Razor2.3 Wouldn't escaping it and doubling it up have the same effect in this case? As it's being echoed into a new file to be used there, right?

Can I put a question here?


Report •

#9
January 4, 2011 at 16:00:28
C:\>type a.cmd
@echo ^%0
C:\>

Unless you mean the redirection somehow alters the behavior of echo?

How To Ask Questions The Smart Way


Report •

#10
January 4, 2011 at 16:06:09
Oh, yeah, I see what you are saying. Thanks, sorry for the confusion.

EDIT: After seeing your edit, what I meant was wouldn't escaping the % cause it to no longer be recognized as a variable. So, instead of echoing "C:\..." it would just be "%0". But, I now realize that isn't the case.

Sorry if I'm not making any sense, having some trouble explaining it. But I understand now.

Can I put a question here?


Report •

#11
January 4, 2011 at 16:17:44
Many ways to skin the cat. surprised no one has used DEBUG example yet, BUT my point is the OP is making it way harder than he has to. If he would just use subroutines in his code he would not need to make all of the self destructive batch files.

I will try to rewrite but this way of coding needs some cleaning up.

BUT

the simpilist solution is this.

echo :end >> W:\TTT\alfa1.bat
echo del W:\TTT\alfa1.bat >> W:\TTT\alfa1.bat
echo exit >> W:\TTT\alfa1.bat

done.

P.S. I looked at the code and it will go into an endless loop if the file does not exist on the P: drive.


Report •

#12
January 4, 2011 at 17:16:39
Here you go much simpler...

@echo off 
cls 
color 04

Set Day=%Date:~-7,2%
Set Month=%Date:~-10,2%
Set Year=%Date:~-4,4%
Set TodaysDate=%Year%-%Day%-%Month%
Set LogPath=W:\TTT\ 
Set LogFileExt=.log
Set LogFileName=ALFA
Set MyLogFile=%LogPath%%TodaysDate%_%LogFileName%%LogFileExt%

NET USE P: /DELETE /Y 
NET USE P: \\Ser5\public\TTT\1\mnt\disk_u\HOME 

if exist "P:\c_data.DA1" ( 
  Call copyfile
)else(
  echo File Does not exist. >> %MyLogFile%
)
goto :EOF

:copyfile
move /Y T:\HOME\c_data.DA1 T:\HOME\copy\
copy /Y /Z P:\c_data.DA1 T:\HOME\c_data.DA1
if errorlevel 1 (
  Call :Errorlog
  copy /Y /Z P:\c_data.DA1 T:\HOME\c_data.DA1
  if errorlevel 1 (
    Call :Errorlog
    move /Y T:\HOME\copy\c_data.DA1 T:\HOME\c_data.DA1 
    )
  )
goto :EOF

:errorlog
  echo.%Date% >>"%MyLogFile%"
  echo.%Time% >>"%MyLogFile%" 
  echo.ALFA !!!no connection!!! >>"%MyLogFile%" 
  call :waitfor
goto :EOF

:waitfor
  ping -n 5 127.0.0.1 > nul 
goto :EOF

NOT TESTED but you can see how the use of CALL will make your code simpler. Look the command up and it will help you with scripting.

http://www.robvanderwoude.com/call.php

I am sure some one will take this code and simplify it even more.


Report •

#13
January 4, 2011 at 23:22:47
ace_omega thats the point to go into endless loop untill it's copied! those network drives are maped places all around city using VPN and files must be copied so basicly if connection is lost it goes into endless loop and waits


btw thanks for replies! will test tham


Report •

#14
January 5, 2011 at 07:08:57
You should never do this. It eats up CPU cycles and bandwidth. Not only that you are writing events to a log file. If you put it into an endless loop your log file will grow in size very fast causing disk space issues.

You should put this in a MS-Schedule event to run once every 5 minutes or so. You should also delete the log file once in a while too. You should look into file replication because it will do what you are trying to do automatically. I use it between my remote offices and it works well.

Another option is to use XXCOPY or ROBOCOPY which will do this for you. It has the ability to only copy the file if it is newer than the one on the other computer. I use this in my backup scripts. I know ROBOCOPY has the ability to specify WAIT times and RETRY times which would replace all of this code. It also has built in logging. The problem with ROBOCOPY over XXCOPY is it only comes with the expansion pack. See link below...

http://www.xxcopy.com/xxcopy30.htm

http://en.wikipedia.org/wiki/Window...

If you want to do this on your network I guess it is yours to do with as you like. Here is the code with an endless loop.

@echo off 
cls 
color 04

Set Day=%Date:~-7,2%
Set Month=%Date:~-10,2%
Set Year=%Date:~-4,4%
Set TodaysDate=%Year%-%Day%-%Month%
Set LogPath=W:\TTT\ 
Set LogFileExt=.log
Set LogFileName=ALFA
Set MyLogFile=%LogPath%%TodaysDate%_%LogFileName%%LogFileExt%

NET USE P: /DELETE /Y 
NET USE P: \\Ser5\public\TTT\1\mnt\disk_u\HOME 

if exist "P:\c_data.DA1" ( 
  Call :copyfile
)else(
  echo File Does not exist. >> %MyLogFile%
)
goto :EOF

:copyfile
move /Y T:\HOME\c_data.DA1 T:\HOME\copy\
copy /Y /Z P:\c_data.DA1 T:\HOME\c_data.DA1
if errorlevel 1 (
  Call :Errorlog
  copy /Y /Z P:\c_data.DA1 T:\HOME\c_data.DA1
  goto :copyfile
  )
goto :EOF

:errorlog
  echo.%Date% >>"%MyLogFile%"
  echo.%Time% >>"%MyLogFile%" 
  echo.ALFA !!!no connection!!! >>"%MyLogFile%" 
  call :waitfor
goto :EOF

:waitfor
  ping -n 5 127.0.0.1 > nul 
goto :EOF


Report •

Ask Question