Avatar of Simon336697
Simon336697
Flag for Australia asked on

Batch file having an issue with "&"

Hi guys,
Im having a slight problem with a batch file.
It is having issues with lines in a text file that it acts against (groups.txt) where the lines have an "&" in the line.
For these lines, i get an error like the following:

eg. the following is the line in groups.txt which is "TESTING R&C GLO"

Processing TESTING R   <-- see how it has trouble reading the "& as in R&C GLO
"C is not recognized as an internal or external command, operable program or batch file."

The script continues though.
I just dont want to see these error messages.

Here is the batch file in the code snippet.

When the batch file creates a file for each group, it returns the usernames correctly, but the filenames are wrong, that is....it creates the filenames but does not include the "&" and everything following it.
Basically,
1) I want to avoid the errors when running the batch file from a command line
2) I want the & to be includes as part of the filename
3) I dont know why when i run the batch file, that the script has an issue with lines having "&".

Any help greatly appreciated.

@echo off
setlocal
set InputFile=groups.txt
for /f "delims=" %%a in ('type "%InputFile%"') do (
  set Group=%%~a
  call :process
)
echo Done.
goto :eof
 
:process
echo Processing %Group% ...
set GroupFile=%Group%.txt
if exist "%GroupFile%" del "%GroupFile%"
for /f "tokens=2 delims=,=" %%a in ('dsquery group -name "%Group%" ^| dsget group -members') do (
  >>"%GroupFile%" echo %%a
)
goto :eof

Open in new window

Windows BatchVB Script

Avatar of undefined
Last Comment
Simon336697

8/22/2022 - Mon
SOLUTION
sirbounty

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
sirbounty

You might also need to place the filename in quotes:

set GroupFile="%Group%.txt"
Simon336697

ASKER
Hi sirbounty hope you are well mate.
I changed my script to be the following:

@echo off
setlocal
set InputFile=groups.txt
for /f "delims=" %%a in ('type "%InputFile%"') do (
 set data=%%~a
 set Group=%data:&=^&%
  call :process
)
echo Done.
goto :eof
 
:process
echo Processing %Group% ...
set GroupFile="%Group%.txt"
if exist "%GroupFile%" del "%GroupFile%"
for /f "tokens=2 delims=,=" %%a in ('dsquery group -name "%Group%" ^| dsget group -members') do (
  >>"%GroupFile%" echo %%a
)
goto :eof


and this time:

I dont get any new files created eg. one for each group (I should be)

When running, it get the following:

D:\gold>test.bat
'&' is not recognized as an internal or external command,
operable program or batch file.
Processing  ...
dsquery failed:The search filter cannot be recognized.
type dsquery /? for help.dsget failed:`Target object for this command' is missing.
type dsget /? for help.'&' is not recognized as an internal or external command,

ChristopherDunn

Try this.
@echo off
setlocal
set InputFile=groups.txt
for /f "delims=" %%a in ('type "%InputFile%"') do (
  set Group="%%~a"
  call :process
)
echo Done.
goto :eof
 
:process
echo Processing %Group:~1,-1% ...
set GroupFile=%Group:0,-1%.txt"
if exist %GroupFile% del %GroupFile%
for /f "tokens=2 delims=,=" %%a in ('dsquery group -name %Group% ^| dsget group -members') do (
  >>%GroupFile% echo %%a
)
goto :eof

Open in new window

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Simon336697

ASKER
Hi Chris, thanks for your help along with sirbounty on this.

Chris, the groups.txt file has the following format:

------------------------------------------------------------------------------------- groups.txt
"au testing R&C test"
"au testing ssokay test002"
"au testing R&C testsss"

So, what is happening is the following:

Processing au testing R
'C' is not recognized as an internal or external command, etc....
ASKER CERTIFIED SOLUTION
ChristopherDunn

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
PWeerakoon

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Simon336697

ASKER
Hi Chris,
MATE THAT WORKS NOW.

Here is the successful script that works with your latest modifications:

-----------------------------------------------------------------------------------------------------
@echo off
setlocal
set InputFile=groups.txt
for /f "delims=" %%a in ('type "%InputFile%"') do (
  set Group="%%~a"
  call :process
)
echo Done.
goto :eof
 
:process
echo Processing %Group% ...
set GroupFile=%Group:~0,-1%.txt"
if exist %GroupFile% del %GroupFile%
for /f "tokens=2 delims=,=" %%a in ('dsquery group -name %Group% ^| dsget

group -members') do (
  >>%GroupFile% echo %%a
)
goto :eof
----------------------------------------------------------------------

THANKS SO MUCH AND THANKS TO ALL WHO SPENT THEIR KIND TIME TO HELP ME OUT :>)
Youre all champions :>)