Page 1 of 1

Help with batch code

Posted: 23 Mar 2010 14:32
by 1+2=46
Hi! I'm new here.
I am making a batch file where I need it (the batch file) to create another file to do something.

Here is the code I have made so far:

Code: Select all

Set /a R1=%random% * 505000 / 32767 + 800000
Set /a R2=%random% * 505000 / 32767 + 800000
REM
Echo @echo off> "%HomePath%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\%R1%.txt"
Echo Rename "%HomePath%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\*.bat" "%random%.bat">> "%HomePath%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\%R1%.txt"


My problem is: when I open the first batch file, the second one is created in the place that it's made to be, but when I go to edit the second file, the code looks like this:

Code: Select all

@echo off
Set /a R3=21626 * 505000 / 32767 + 800000
Rename "\Users\##NULLED##\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\*.bat" "8603.bat"


And I don't want it to be like that. The "21626" has to be "%random%" and the "8603" has also to be "%random%".


Can someone tell me what am I doing wrong?
Thanks.

Re: Help with batch code

Posted: 23 Mar 2010 14:43
by aGerman
Double the percent signs for the echo redirection, like %%random%%.

Regards
aGerman

Re: Help with batch code

Posted: 23 Mar 2010 15:06
by 1+2=46
aGerman wrote:Double the percent signs for the echo redirection, like %%random%%.

Regards
aGerman


It worked! Thanks alot!

Re: Help with batch code

Posted: 23 Mar 2010 15:38
by 1+2=46
Can you also tell me how can I do a batch file to create a file with a random file extension?

Re: Help with batch code

Posted: 23 Mar 2010 15:55
by aGerman
Makes no sense, but yes I can. Try

Code: Select all

@echo off &setlocal
set "charsource=abcdefghijklmnopqrstuvwxyz"
set /a x1=%random%%%26
set /a x2=%random%%%26
set /a x3=%random%%%26
call set "ext=.%%charsource:~%x1%,1%%%%charsource:~%x2%,1%%%%charsource:~%x3%,1%%"
echo %ext%
pause


Regards
aGerman

Re: Help with batch code

Posted: 23 Mar 2010 16:14
by 1+2=46
aGerman wrote:Makes no sense, but yes I can. Try

Code: Select all

@echo off &setlocal
set "charsource=abcdefghijklmnopqrstuvwxyz"
set /a x1=%random%%%26
set /a x2=%random%%%26
set /a x3=%random%%%26
call set "ext=.%%charsource:~%x1%,1%%%%charsource:~%x2%,1%%%%charsource:~%x3%,1%%"
echo %ext%
pause


Regards
aGerman


It does make all the sense for me.
It works too. You're the man. :D

I just have got one more question: Is it possible to make a batch file that when someone tries to delete it, it opens another file (but then the batch file it's deleted)?

Re: Help with batch code

Posted: 23 Mar 2010 16:31
by aGerman
No.

Re: Help with batch code

Posted: 23 Mar 2010 16:36
by 1+2=46
aGerman wrote:No.


Really? Gotta be a way to know if a file exists or not. If not, run the file, if it exists, it's all ok.

Re: Help with batch code

Posted: 23 Mar 2010 17:04
by aGerman
What ever you intend ... if you want to write something like a virus in batch, forget it - other guys had the same idea before.

Which file will test if a file exist? Means a deleted batch file can do nothing.
Further more ... Batch is a relic of the old DOS OS. What do you think which level you can achieve. Note it's not a programming language. You can't get at the unfathomable depths of your OS.

Re: Help with batch code

Posted: 23 Mar 2010 17:11
by 1+2=46
aGerman wrote:What ever you intend ... if you want to write something like a virus in batch, forget it - other guys had the same idea before.

Which file will test if a file exist? Means a deleted batch file can do nothing.
Further more ... Batch is a relic of the old DOS OS. What do you think which level you can achieve. Note it's not a programming language. You can't get at the unfathomable depths of your OS.


No. There will be two files. One text file and one batch file. I need a code to put on the batch file that do the "thing" I want it to do whenever the text file stops existing (it is deleted).

I though I could make some kinda of loop to check if the file exists, and whenever it stops existing (if stops), do the "thing" I want it to do.
And it's not a virus.

Re: Help with batch code

Posted: 23 Mar 2010 17:29
by aGerman
Haha, just for fun:

Code: Select all

@echo off &setlocal

:loop
if not exist "yourFile.txt" goto ahead
ping -n 2 localhost>nul
goto loop

:ahead
echo File was deleted.
pause


Next game...

Re: Help with batch code

Posted: 28 Dec 2010 14:44
by BatMaster
@aGerman

if u replace the second last line of your random extension batch file with this

Code: Select all

echo helloworld>hw%ext%

it creates a file with the extension your file made and if you rename it to a .txt
file it contains the text to the left of the >
the name of the file is to the right of the >