Help with batch code

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
1+2=46
Posts: 25
Joined: 23 Mar 2010 14:19

Help with batch code

#1 Post by 1+2=46 » 23 Mar 2010 14:32

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.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Help with batch code

#2 Post by aGerman » 23 Mar 2010 14:43

Double the percent signs for the echo redirection, like %%random%%.

Regards
aGerman

1+2=46
Posts: 25
Joined: 23 Mar 2010 14:19

Re: Help with batch code

#3 Post by 1+2=46 » 23 Mar 2010 15:06

aGerman wrote:Double the percent signs for the echo redirection, like %%random%%.

Regards
aGerman


It worked! Thanks alot!

1+2=46
Posts: 25
Joined: 23 Mar 2010 14:19

Re: Help with batch code

#4 Post by 1+2=46 » 23 Mar 2010 15:38

Can you also tell me how can I do a batch file to create a file with a random file extension?

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Help with batch code

#5 Post by aGerman » 23 Mar 2010 15:55

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

1+2=46
Posts: 25
Joined: 23 Mar 2010 14:19

Re: Help with batch code

#6 Post by 1+2=46 » 23 Mar 2010 16:14

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)?

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Help with batch code

#7 Post by aGerman » 23 Mar 2010 16:31

No.

1+2=46
Posts: 25
Joined: 23 Mar 2010 14:19

Re: Help with batch code

#8 Post by 1+2=46 » 23 Mar 2010 16:36

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.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Help with batch code

#9 Post by aGerman » 23 Mar 2010 17:04

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.

1+2=46
Posts: 25
Joined: 23 Mar 2010 14:19

Re: Help with batch code

#10 Post by 1+2=46 » 23 Mar 2010 17:11

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.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Help with batch code

#11 Post by aGerman » 23 Mar 2010 17:29

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

BatMaster
Posts: 28
Joined: 22 Dec 2010 12:53

Re: Help with batch code

#12 Post by BatMaster » 28 Dec 2010 14:44

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

Post Reply