News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Batch commands on NT based computers

Started by zemtex, June 17, 2011, 07:45:19 PM

Previous topic - Next topic

zemtex

Batch scripting is probably not very related to assembly but since it is very common to use batch scripts when coding assembly I guess I will ask a few questions here.

The way I understand it, NT based batch scripting is different from Ms-Dos batch scripts. Whenever I lookup a tutorial on the web there seems to be more or less incompatible, and all sorts of errors appear.

I want to achieve this on a NT based computer:

1. A batch script should check and verify that commandline parameters 1 - 6 is present and is not empty, if it is not it should goto a usage label and print some text. I've tried this and it sometimes worked and sometimes not, the problem is probably that NT based scripting is a bit different?

2. I need a way to fetch the current folder name. If the current folder is x:\folderA\folderB then I need the "folderB" part without the root dir and the extra characters because I will work directly on the folder name. How can I fetch that name, preferably fetch every single folder name in the current folder in some sort of a loop so that I can work on all folders?

Thanks to anyone who is familiar with this  :dance:
I have been puzzling with lego bricks all my life. I know how to do this. When Peter, at age 6 is competing with me, I find it extremely neccessary to show him that I can puzzle bricks better than him, because he is so damn talented that all that is called rational has gone haywire.

jj2007

One good description comes actually from Microsoft  :clap:

MasmBasic fans may study \masm32\RichMasm\Res\bldallRM.bax - you can find many tricks there.
Example:
IF %oDelDef%S==1S (IF EXIST TmpDllEx.def del TmpDllEx.def)
The "S" can be any other character, it just avoids a problem with empty variables. Try it:

@echo off
IF %3S==S (
echo there is no arg 3
) else (
echo arg 3 is %3
)
pause

Magnum

zemtex,

The folks here are gurus with nt and dos batch files.

alt.msdos.batch.­nt
Have a great day,
                         Andy

Magnum

Have a great day,
                         Andy

zemtex

Alright thanks. I am seriously considering doing an executable to do the work.
I have been puzzling with lego bricks all my life. I know how to do this. When Peter, at age 6 is competing with me, I find it extremely neccessary to show him that I can puzzle bricks better than him, because he is so damn talented that all that is called rational has gone haywire.

FORTRANS

Hi,

   Not too sure how to do the directory / folder name as string
processing never seemed to go well in a BATch file.  But here
is the start of the arguments test.

Cheers,

Steve N.


@ECHO OFF
IF "%1" == "" GOTO OneBad

GOTO TryTwo
:OneBad
ECHO "Argument one is missing."
GOTO EndBAT

:TryTwo
IF "%2" == "" GOTO TwoBad
GOTO EndBAT

:TwoBad
ECHO "Argument two is missing."
GOTO EndBAT
:EndBAT

mineiro

this shows you a bit, save it hello.bat .This one assumes you have "find" ms-dos command in windows .
The cool thing of bat files, is that you can generate usefull scripts and redirect it to debug.exe. Debug then can save or create a program to you.

@echo off
echo.
set output=hello
if not errorlevel 0 goto end
if "%1"=="" goto usage2
if "%1"=="/?" goto usage
if "%1"=="path" goto path
if "%1"=="number" goto number
if "%output%"=="hello" goto start
goto end

:start
echo %output% %1
goto end

:usage2
echo type hello /?
goto end

:usage
echo type "hello some_name"
echo or type "hello path"
goto end

:path
dir > list.txt

set my_through= find /I "C:\" list.txt
echo This command uses %my_through%
%my_through%
find "número" list.txt /C
if not errorlevel 0 goto english
echo your operation system is in portuguese
goto next
:english
echo your O.S. is in english
goto next

:next
del list.txt
echo %OS% %SystemDrive% %PROMPT%
ECHO %NUMBER_OF_PROCESSORS% %username%
:end
echo you are in the %CD% folder
@pause

To see more, type "set /?" in prompt.

dedndave

you can also use the FOR batch command to step through the parameters
you might find some useful tips here...

http://www.masm32.com/board/index.php?topic=13771.msg108337#msg108337

zemtex

Quote from: FORTRANS on June 17, 2011, 10:04:55 PM
Hi,

   Not too sure how to do the directory / folder name as string
processing never seemed to go well in a BATch file.  But here
is the start of the arguments test.

Cheers,

Steve N.


@ECHO OFF
IF "%1" == "" GOTO OneBad

GOTO TryTwo
:OneBad
ECHO "Argument one is missing."
GOTO EndBAT

:TryTwo
IF "%2" == "" GOTO TwoBad
GOTO EndBAT

:TwoBad
ECHO "Argument two is missing."
GOTO EndBAT
:EndBAT


The funny thing I did just that, exactly the same syntax,  the odd thing it worked a few times and then suddenly it didnt work anymore. It said something about "goto is unexpected here".

Btw, you can avvoid the :EndBAT label by using "goto :eof" and it will jump to the end of file.  :U
I have been puzzling with lego bricks all my life. I know how to do this. When Peter, at age 6 is competing with me, I find it extremely neccessary to show him that I can puzzle bricks better than him, because he is so damn talented that all that is called rational has gone haywire.

clive

It could be a random act of randomness. Those happen a lot as well.

zemtex

FORTRANS: I figured out why my script failed, despite being the same as yours. When I echo some text out, I forgot to put it inside quotes (""), so it parsed some special characters as script delimiters and so the whole thing became corrupt. I now put quotes on my string outputs. But the bad thing is that the quotes is also printed to the screen, can i avvoid this somehow  :snooty:

Thanks clive.
I have been puzzling with lego bricks all my life. I know how to do this. When Peter, at age 6 is competing with me, I find it extremely neccessary to show him that I can puzzle bricks better than him, because he is so damn talented that all that is called rational has gone haywire.

dedndave

you can set the current directory string into a temporary environment variable, then expand it as required

SET CurDir=%CD%
.
.
.
ren %CurDir%\somefile.1 somefile.2


of course, you may not need to set it into a variable

ren %CD%\somefile.1 somefile.2

it depends on what you are doing, i suppose

Quote"goto is unexpected here"

that usually happens when you have some sort of syntax error

zemtex

Thanks dedndave.

By the way, is there a way I can get long filenames to work as parameters to batch files without complex parsing? So far it only works with short filenames. I can post the script, the script is a winrar script intended to make it easier to compress many files and folders inside a folder and then split the archive into pieces of any given size. The plan is to make a script so that I can automate this process for many folders at the same time. When you store many programs, games, music and video over time, some of it is uncompressed, some is badly compressed and some are very large files which make it very hard to sustain a healthy fragmentation on the drive.

My plan is to simply script it so I can re-compress everything that is inside a folder, but the new archive should re-use the old archive/folder-names so that I can recognize the stuff again.
You can do that in WinRar already but i'd rather have a script that automatate this for me.

Here is the script so far: (make changes as you please, the main problem now is getting the script to accept long filenames and path without complex parsing)


@echo off

SET winrar="c:\Program Files\WinRAR\Rar.exe"
SET addfiles=a
SET priority=-ri
SET compression=-m
SET originaldates=-tk
SET splitsize=-v
SET threadnum=-mt
SET recursedirs=-r
SET datepolicy=-tk
SET datelatest=-tl
SET password=-p
SET computethis=

cls
echo.
echo --------------------------------------------------------------------------------
echo                               WinRar batch script.
echo --------------------------------------------------------------------------------

if "%1"=="" goto usage
if "%2"=="" goto usage
if "%3"=="" goto usage
if "%4"=="" goto usage
if "%5"=="" goto usage
if "%6"=="" goto usage
if "%7"=="" goto usage
if "%8"=="" goto usage
if "%9"=="" goto usage

SET p_source=%1
shift
SET p_dest=%1
shift
SET p_threadnum=%1
shift
SET p_priority=%1
shift
SET p_sleepms=%1
shift
SET p_compression=%1
shift
SET p_splitsize=%1
shift
SET p_datepolicy=%1
shift
SET p_password=%1

if "%p_datepolicy%"=="1" set "datepolicy=%datelatest%"
set "computethis=%winrar% %addfiles% %priority%%p_priority%:%p_sleepms% %compression%%p_compression% %splitsize%%p_splitsize%b %threadnum%%p_threadnum% %recursedirs% %datepolicy%"
if "%p_password%" NEQ "0" set "computethis=%computethis% %password%%p_password%"
set "computethis=%computethis% %p_dest% %p_source%"

%computethis%

:done
echo .
echo done.
goto :eof

:usage
echo.
echo  Usage:
echo.
echo   ---------
echo   rarit.bat
echo   ---------
echo   "<sourcefile/sourcedir>  -  Choose what file or folder to compress"
echo   "<destfile.rar>          -  Choose where to store the archive"
echo   "<threadnum>             -  How many threads to use during compression"
echo   "<priority>              -  Execution priority. 0-15. 0=Standard 1=Lowest 15=Highest"
echo   "<SleepMS>               -  How many Milliseconds to pass to the OS between read/write"
echo   "<Compression>           -  Compression level 0-5 where 5 is maximum
echo   "<Splitsize>             -  Split file size. Use multiple of 4096 to avvoid disk waste.
echo.  "<DatePolicy>            -  0=Prevent changes to archive date 1=Set archive time to newest file inside archive
echo.  "<Password>              -  Set archive password. Set to 0 if you do not want to use password
echo.
echo.

I have been puzzling with lego bricks all my life. I know how to do this. When Peter, at age 6 is competing with me, I find it extremely neccessary to show him that I can puzzle bricks better than him, because he is so damn talented that all that is called rational has gone haywire.

zemtex

To show you what I mean by that. Consider this diagram:



So, basically I want to be able to do many WinRar-related activities on all of my files, issued at the same time. If I want to recompress all folders into a different split size, or whether I want to set a different password on thousand of archives, or it could simply be recompressing all archives with a different and better compression.

There are many things I could do, but that is basically the idea.
I have been puzzling with lego bricks all my life. I know how to do this. When Peter, at age 6 is competing with me, I find it extremely neccessary to show him that I can puzzle bricks better than him, because he is so damn talented that all that is called rational has gone haywire.

mineiro

Hello Sr zemtex,
In your example you can commit the "C:" of rar path, do only '\Program Files\WinRAR\Rar.exe', but again, 'program files folder' is a default, this environment variable is "%ProgramFiles%".
type in prompt only "set" to you see the default variables. Type something like below in prompt: That one will redirect the default variable in environment to text.txt, and after call the notepad to open it.
echo %ProgramFiles% > text.txt | notepad text.txt
follow, not your question, but maybe can be usefull to do something, a simple messagebox. Take a look at the generated code one.asm
Save it as dot bat file.

@echo off
echo include \masm32\include\masm32rt.inc > one.asm
echo .data >> one.asm
echo mypath db "%CD%",00h >> one.asm
echo .code >> one.asm
echo start: >> one.asm
echo invoke MessageBox,0,addr mypath,addr mypath,MB_YESNO >> one.asm
echo .if eax == IDYES >> one.asm
echo mov eax,1 >> one.asm
echo .else >> one.asm
echo mov eax,0 >> one.asm
echo .endif >> one.asm
echo invoke ExitProcess,eax >> one.asm
echo end start >> one.asm
echo.
set PATH=%PATH%;\masm32\bin;\masm32\include;\masm32\lib
ml /c /Cp /coff one.asm
Link /SUBSYSTEM:WINDOWS one.obj
one
if errorlevel 1 goto yes
echo you have pressed no in messagebox
goto end
:yes
echo you have pressed yes in messagebox
:end
@pause

Sorry about my english.