Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1164
  • Last Modified:

DOS batch script to copy a file to users in a group

I need a DOS batch scrIpt that will do the following:-

A copy a file which is in D:\Frontline\BCS.INI to \\cslwindat01\homes\%username%\WINDOWS but to only users that are in a group called GG_FrontlineUsers
0
rakkad
Asked:
rakkad
  • 4
  • 4
  • 4
1 Solution
 
Bill PrewCommented:
This should be a good start, save as a BAT file and give a try. You will need to get a copy of ADFIND utility from:

http://www.joeware.net/freetools/tools/adfind/

@echo off
setlocal

REM Define all variable info needed
set GroupName=GG_FrontlineUsers
set ShareName=\\cslwindat01\homes
set FolderName=WINDOWS
set CopyFile=D:\Frontline\BCS.INI
set TempFile=%TEMP%\_adfind_.txt

REM List all members of the group, and get the account names, save to a work file
adfind -default -f name="%GroupName%" member -list | adfind samaccountname -list > "%TempFile%"

REM Process the account list file
for /F "usebackq tokens=*" %%U in ("%TempFile%") do (

  REM See if this users folder exists
  if exist "%ShareName%\%%~U\%FolderName%" (

    REM Copy the master fiel to this users area
    copy /Y "%CopyFile%" "%ShareName%\%%~U\%FolderName%"

  )

)

REM Remove the work file
if exist "%TempFile%" del "%TempFile%"

Open in new window

~bp
0
 
rakkadAuthor Commented:
This script worked excellent and produced the results the way I expected.

I do want to extend the script, if possibly and would like it to do the following:-

The file being copied which is the BCS.INI file has a AdapterUDPPortNumber=3000, I want the number to be incremented to one each time the file is copied

Many thanks in your accurate responses that you have provided.
0
 
Steve KnightIT ConsultancyCommented:
What else is in the bcs.ini?  Would suggest leave this line out of it if order doesn't matter then adding an extra line in batch above is easier.

you want something like (this is most Bill's code of course just added a few bits, points to him!)

Steve

echo off
setlocal enabledelayedexpansion

REM Define all variable info needed
set GroupName=GG_FrontlineUsers
set ShareName=\\cslwindat01\homes
set FolderName=WINDOWS
set CopyFile=D:\Frontline\BCS.INI
set TempFile=%TEMP%\_adfind_.txt
Set port=3000

REM List all members of the group, and get the account names, save to a work file
adfind -default -f name="%GroupName%" member -list | adfind samaccountname -list > "%TempFile%"

REM Process the account list file
for /F "usebackq tokens=*" %%U in ("%TempFile%") do (

  REM See if this users folder exists
  if exist "%ShareName%\%%~U\%FolderName%" (

    REM Copy the master fiel to this users area
    copy /Y "%CopyFile%" "%ShareName%\%%~U\%FolderName%"
    echo AdapterUDPPortNumber=!port! >>"%ShareName%\%%~U\%FolderName%"
    set /a port=port+1

  )

)

REM Remove the work file
if exist "%TempFile%" del "%TempFile%"

Open in new window



Steve
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Bill PrewCommented:
Yes, what are the format requirements of the INI file, can you show an example?

~bp
0
 
rakkadAuthor Commented:
Yep sure, the BCS.INI file should be outlined like this:-

[Setup]
AdapterUDPPortNumber=
LocalJournalPath=
mdbPath=
JournalArchivePath=
JournalLogPath=
TAWorkstation=False

[ComSettings]
Commport=1
Settings=9600,e,7,2

[Diagnostics]
ChequesReceived=False

[Print Layout Database]
PrintMDB=

[BCSLink Summit Parameters]
IPAddress=
Port=


So, when the AdapterUDPPortNumber entry is appended to the BCS.INI, the entry should appear under [Setup] section

OR

Alternatively, if the BCS.INI file has an entry already AdapterUDPPortNumber, could that entry just be appended with the port number

Thanks
0
 
Steve KnightIT ConsultancyCommented:
If the application will take it move the whole setup section to the end and leave that entry out then all you need to do is append  to the file which is like I suggested above and much easier than having to replace the line, processing it line by line.

Steve
0
 
Bill PrewCommented:
Give this a try, I think it should meet the job:

@echo off
setlocal EnableDelayedExpansion

REM Define all variable info needed
set GroupName=GG_FrontlineUsers
set ShareName=\\cslwindat01\homes
set FolderName=WINDOWS
set CopyFile=D:\Frontline\BCS.INI
set TempFile=%TEMP%\_adfind_.txt
Set Port=3000

REM List all members of the group, and get the account names, save to a work file
adfind -default -f name="%GroupName%" member -list | adfind samaccountname -list > "%TempFile%"

REM Process the account list file
for /F "usebackq tokens=*" %%U in ("%TempFile%") do (

  REM See if this users folder exists
  if exist "%ShareName%\%%~U\%FolderName%" (

    REM Copy the master file to this users area, incrementing the port number each time
    (
      for /F "usebackq tokens=*" %%L in ("%CopyFile%") do (
        set Line=%%L
        if /I "!Line:~0,21!" EQU "AdapterUDPPortNumber=" (
          set Line=AdapterUDPPortNumber=!Port!
          set /A Port+=1
        )
        echo.!Line!
      )
    )>"%ShareName%\%%~U\%FolderName%"

  )

)

REM Remove the work file
if exist "%TempFile%" del "%TempFile%"

Open in new window

~bp
0
 
rakkadAuthor Commented:
I used the BCS.INI file which is the following:-

[Setup]
AdapterUDPPortNumber=
LocalJournalPath=
mdbPath=
JournalArchivePath=
JournalLogPath=
TAWorkstation=False

[ComSettings]
Commport=1
Settings=9600,e,7,2

[Diagnostics]
ChequesReceived=False

[Print Layout Database]
PrintMDB=

[BCSLink Summit Parameters]
IPAddress=
Port=

I changed your code slightly to:-

@echo on
setlocal EnableDelayedExpansion

REM Define all variable info needed
set GroupName=GG_FrontlineUsers_Training
set ShareName=\\cslwindat01\ts_profiles$
set FolderName=WINDOWS
set CopyFile=C:\BCS.INI
set TempFile=C:\_adfind_.txt
Set Port=3000

REM List all members of the group, and get the account names, save to a work file
adfind -default -f name="%GroupName%" member -list | adfind samaccountname -list > "%TempFile%"

REM Process the account list file
for /F "usebackq tokens=*" %%U in ("%TempFile%") do (

  REM See if this users folder exists
  if exist "%ShareName%\%%~U.NBS\%FolderName%" (

    REM Copy the master file to this users area, incrementing the port number each time
    (
      for /F "usebackq tokens=*" %%L in ("%CopyFile%") do (
        set Line=%%L
        if /I "!Line:~0,21!" EQU "AdapterUDPPortNumber=" (
          set Line=AdapterUDPPortNumber=!Port!
          set /A Port+=1
        )
        echo.!Line!
      )
    )>"%ShareName%\%%~U.NBS\%FolderName%\BCS.INI"

  )

)

REM Remove the work file
REM if exist "%TempFile%" del "%TempFile%"

But am getting the following output results:-

C:\Temp>(
set Line=[Setup]  
 if /I "!Line:~0,21!" EQU "AdapterUDPPortNumber=" (
set Line=AdapterUDPPortNumber=!Port!  
 set /A Port+=1
)  
 echo.!Line!
)
[Setup]

C:\Temp>(
set Line=AdapterUDPPortNumber=  
 if /I "!Line:~0,21!" EQU "AdapterUDPPortNumber=" (
set Line=AdapterUDPPortNumber=!Port!  
 set /A Port+=1
)  
 echo.!Line!
)
AdapterUDPPortNumber=3044

C:\Temp>(
set Line=LocalJournalPath=  
 if /I "!Line:~0,21!" EQU "AdapterUDPPortNumber=" (
set Line=AdapterUDPPortNumber=!Port!  
 set /A Port+=1
)  
 echo.!Line!
)
LocalJournalPath=

C:\Temp>(
set Line=mdbPath=  
 if /I "!Line:~0,21!" EQU "AdapterUDPPortNumber=" (
set Line=AdapterUDPPortNumber=!Port!  
 set /A Port+=1
)  
 echo.!Line!
)
mdbPath=

C:\Temp>(
set Line=JournalArchivePath=  
 if /I "!Line:~0,21!" EQU "AdapterUDPPortNumber=" (
set Line=AdapterUDPPortNumber=!Port!  
 set /A Port+=1
)  
 echo.!Line!
)
JournalArchivePath=

C:\Temp>(
set Line=JournalLogPath=  
 if /I "!Line:~0,21!" EQU "AdapterUDPPortNumber=" (
set Line=AdapterUDPPortNumber=!Port!  
 set /A Port+=1
)  
 echo.!Line!
)
JournalLogPath=

C:\Temp>(
set Line=TAWorkstation=False  
 if /I "!Line:~0,21!" EQU "AdapterUDPPortNumber=" (
set Line=AdapterUDPPortNumber=!Port!  
 set /A Port+=1
)  
 echo.!Line!
)
TAWorkstation=False

C:\Temp>(
set Line=[ComSettings]  
 if /I "!Line:~0,21!" EQU "AdapterUDPPortNumber=" (
set Line=AdapterUDPPortNumber=!Port!  
 set /A Port+=1
)  
 echo.!Line!
)
[ComSettings]

C:\Temp>(
set Line=Commport=1  
 if /I "!Line:~0,21!" EQU "AdapterUDPPortNumber=" (
set Line=AdapterUDPPortNumber=!Port!  
 set /A Port+=1
)  
 echo.!Line!
)
Commport=1

C:\Temp>(
set Line=Settings=9600,e,7,2  
 if /I "!Line:~0,21!" EQU "AdapterUDPPortNumber=" (
set Line=AdapterUDPPortNumber=!Port!  
 set /A Port+=1
)  
 echo.!Line!
)
Settings=9600,e,7,2

C:\Temp>(
set Line=[Diagnostics]  
 if /I "!Line:~0,21!" EQU "AdapterUDPPortNumber=" (
set Line=AdapterUDPPortNumber=!Port!  
 set /A Port+=1
)  
 echo.!Line!
)
[Diagnostics]

C:\Temp>(
set Line=ChequesReceived=False  
 if /I "!Line:~0,21!" EQU "AdapterUDPPortNumber=" (
set Line=AdapterUDPPortNumber=!Port!  
 set /A Port+=1
)  
 echo.!Line!
)
ChequesReceived=False

C:\Temp>(
set Line=[Print Layout Database]  
 if /I "!Line:~0,21!" EQU "AdapterUDPPortNumber=" (
set Line=AdapterUDPPortNumber=!Port!  
 set /A Port+=1
)  
 echo.!Line!
)
[Print Layout Database]

C:\Temp>(
set Line=PrintMDB=  
 if /I "!Line:~0,21!" EQU "AdapterUDPPortNumber=" (
set Line=AdapterUDPPortNumber=!Port!  
 set /A Port+=1
)  
 echo.!Line!
)
PrintMDB=

C:\Temp>(
set Line=[BCSLink Summit Parameters]  
 if /I "!Line:~0,21!" EQU "AdapterUDPPortNumber=" (
set Line=AdapterUDPPortNumber=!Port!  
 set /A Port+=1
)  
 echo.!Line!
)
[BCSLink Summit Parameters]

C:\Temp>(
set Line=IPAddress=  
 if /I "!Line:~0,21!" EQU "AdapterUDPPortNumber=" (
set Line=AdapterUDPPortNumber=!Port!  
 set /A Port+=1
)  
 echo.!Line!
)
IPAddress=

C:\Temp>(
set Line=Port=  
 if /I "!Line:~0,21!" EQU "AdapterUDPPortNumber=" (
set Line=AdapterUDPPortNumber=!Port!  
 set /A Port+=1
)  
 echo.!Line!
)
Port=


So, the AdapterUDPPortNumber hasn't changed.  Can you help me on this?

Thanks
0
 
Steve KnightIT ConsultancyCommented:
It looks like it is 3044 in this example?
0
 
rakkadAuthor Commented:
I don't really understand your comment how you come to this?
0
 
Steve KnightIT ConsultancyCommented:
It shows in the output you give:

AdapterUDPPortNumber=3044

Does the bcs.ini file get created but without a port, all the same, or are the bcs.ini files not appearing?

Steve
0
 
Bill PrewCommented:
Agreed, it looks like you grabbed the 44th set of outputs to paste up here, right?  I assume there were at least 44 users found?  Also, where is the rest of the echo lines, I don't see the copy command line?

So, were the new BCS.INI files created in the destination folders?  If so, what did they contain?

~bp
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now