I’m trying to build a batch file but for an unknown reason when the process is executed the variable %UT:~47% stay empty and only gets filled after a second execution. If I execute This once the echo result is ~47 but on the second time the result result show Yes or NO (Oui or Non in french). Anyone know why?
The first condition works perfect, If no parameter. But If I execute my batch with a username in parameter it check if the user account is activated or not. I put the line with the word ”Compte” who actually stand for account in english, in the file C:\user.txt but the set /p seems buggy cause my echo command give me ~47 as empty but the file c:\user.txt contain
(Compte : actif Oui or Non)
In English it’s
(Account : active Yes or No)
@echo off
::if no parameter
if "%~1" == "" echo Please Indicate an User to delete ! & exit /b 0
for %%U in (%*) do (
net user %%U | find "Compte">c:\user.txt || echo can't find user %%U
set /p UT=<c:\user.txt
echo %UT:~47%
)
This is because you are setting the variable “UT” in the for loop, search for “batch delayed expansion” for more info. Also, you probably don’t need to redirect the output to a file first, you can use
for /f "tokens=* eol=" %%a in ('net user add %%U')to directly capture the output in the variable.