MS-Dos script to run command

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
kouliscon
Posts: 7
Joined: 04 Aug 2011 07:30

MS-Dos script to run command

#1 Post by kouliscon » 04 Aug 2011 07:34

Hello all

I need help by someone if possible for the following:

I need to run a command of copy at a .bat file only if the user which logged in that computer is the same name as the computername.

So something like that:

If %username% = %computername% then goto 1 if not 2

1
robocopy c:\docs d:\docs *.*

end

2
end

Can anybody help?

Thanks

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: MS-Dos script to run command

#2 Post by Ed Dyreen » 04 Aug 2011 08:10

'

Code: Select all

If /i ["%username%"] == ["%computername%"] (
   ::
   robocopy c:\docs d:\docs *.*

) else (
   ::
   echo.commands please...
)

Code: Select all

:main ()
::(
   If /i ["%username%"] == ["%computername%"] (
      ::
      call :function1 $parameters
   ) else call :function2 $parameters
   ::
   pause
::)
exit /b %$error%

:function1 ( $parameters )
::(
   robocopy c:\docs d:\docs *.*
::)
exit /b %$error%

:function2 ( $parameters )
::(
   echo.commands please...
::)
exit /b %$error%
Last edited by Ed Dyreen on 04 Aug 2011 11:27, edited 1 time in total.

kouliscon
Posts: 7
Joined: 04 Aug 2011 07:30

Re: MS-Dos script to run command

#3 Post by kouliscon » 04 Aug 2011 10:00

thx a lot i will try them and let you know.

i need another help pls.

at the command where is the robocopy i have:

robocopy c:\docs d:\docs *.* /log:d:\docs\log\%username%.log

is there a way to save this %username%.log each day a different file

So if Monday today to be
%username%Mon.log
if Tuesday
%username%Tue.log

the reason is i want to keep a weeks old the logs for verification.

Thx in advance :)

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: MS-Dos script to run command

#4 Post by Ed Dyreen » 04 Aug 2011 11:19

'
That's doable:

Code: Select all

@echo off

for /f "tokens=1 delims= " %%! in (
   "%date%"
) do set "$File=%username%_%%~!.LOG"

echo.$File=%$File%_

You are now ready to over/write this log :)
This could help:
http://www.dostips.com/DtTipsDateTime.php
Last edited by Ed Dyreen on 04 Aug 2011 11:39, edited 1 time in total.

kouliscon
Posts: 7
Joined: 04 Aug 2011 07:30

Re: MS-Dos script to run command

#5 Post by kouliscon » 04 Aug 2011 11:28

Hello

Thx for the fast reply.

I need this new log per day to be created when I run the robocopy command.

In other words

Robocopy c:\data d:\files /log:d:\data\log\%username%_%%~.log

You think you can modify it?

Thx for the link.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: MS-Dos script to run command

#6 Post by Ed Dyreen » 04 Aug 2011 11:33

'

Code: Select all

@echo off

for /f "tokens=1 delims= " %%! in (
   "%date%"
) do set "$LOG.File=%username%_%%~!.LOG"

If /i ["%username%"] == ["%computername%"] (
   ::
   Robocopy "c:\data" "d:\files" /log:"d:\data\log\%$LOG.File%"
)
I never use robocopy so I'm unsure /log: will accept quoted params, it should though...
I advice using quotes by default otherwise you'll get into problems when the path contains spaces.
Last edited by Ed Dyreen on 04 Aug 2011 11:46, edited 3 times in total.

kouliscon
Posts: 7
Joined: 04 Aug 2011 07:30

Re: MS-Dos script to run command

#7 Post by kouliscon » 04 Aug 2011 11:36

Thx a lot for all the help.

I will try them and let you know.

kouliscon
Posts: 7
Joined: 04 Aug 2011 07:30

Re: MS-Dos script to run command

#8 Post by kouliscon » 16 Aug 2011 06:33

Hello

Sorry for the late responce but i was away.

Out of the 4 scripts which you wrote below the first 2 are working ok.

the last one which i am interested for the log i can tell you the following:

1) it doesnt copy the copy from location 1 to location 2.
2) it does creat a log file at location /log:"d:\data\log\%$LOG.File%" but the name of the file is %$LOG.File% maybe that can point you where the mistake is.

Thx in advance

Post Reply