0

I'm looking for a way to differentiate between letters and numbers when reading user account names in Windows 7 using batch scripting. For example, I want to distinguish between User and User1234. The purpose is because I'm using an account deletion script to remove old accounts, but I only want to remove those accounts with numbers in them.

I've been researching this for a while, but the closest thing I've been able to find is here: http://www.dostips.com/?t=Experimental.StringDiff

Another option, maybe, is to be able to read the last 4 of the user name, and if they equal 1-9 then to add them to the deletion group. I'm not sure how to do this, however, so - again - any help is appreciated.

Thanks

clines
  • 725
  • 1
  • 6
  • 10

4 Answers4

2
@echo off
setlocal EnableDelayedExpansion

set user=User1234

set digitsInUser=
for /L %%i in (0,1,9) do (
    if "!user:%%i=!" neq "%user%" set digitsInUser=1
)

if defined digitsInUser (
    echo The user "%user%" have digits
)
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • This will execute only when numbers are on the trailing edge of the `username1234` which may be the case in the question. It's a neat solution, and won't affect a username like `june.2.smith` – foxidrive Jun 05 '13 at 18:15
1

Windows shell Regex power

@echo off&setlocal
rem some testing
for %%i in (
    "User1234"
    "AAA111"
    "AAA"
    "111"
    "aaa"
    "a"
    "1"
    "me & you"
    "1 + 1"
    ""
    ) do (
  call:differentiate "%%~i"
  )
goto:eof

:differentiate "string"
set "String=%~1"
setlocal enabledelayedexpansion
echo("!string!"|findstr "^.[0-9][0-9]*.$" >nul&& echo "!string!": only numbers || echo "!string!": NOT all numbers
echo("!string!"|findstr "^.[a-z][a-z]*.$" >nul&& echo "!string!": only letters || echo "!string!": NOT all letters
echo(
endlocal
goto:eof

.. output is:

"User1234": NOT all numbers
"User1234": NOT all letters

"AAA111": NOT all numbers
"AAA111": NOT all letters

"AAA": NOT all numbers
"AAA": only letters

"111": only numbers
"111": NOT all letters

"aaa": NOT all numbers
"aaa": only letters

"a": NOT all numbers
"a": only letters

"1": only numbers
"1": NOT all letters

"me & you": NOT all numbers
"me & you": NOT all letters

"1 + 1": NOT all numbers
"1 + 1": NOT all letters

"": NOT all numbers
"": NOT all letters
Community
  • 1
  • 1
Endoro
  • 37,015
  • 8
  • 50
  • 63
  • 1
    +1 Wow, regular expressions IN BATCH!!! I thought they can only be used in a "real" scripting language... ;-) What do you think: is this solution "horrible" or "trivial"? – Aacini Jun 04 '13 at 20:50
  • Yes, I can see this. It's a shame :(( – Endoro Jun 04 '13 at 20:53
  • 1
    BTW, have you seen my comment in [this question](http://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script)? – Aacini Jun 04 '13 at 21:00
  • 1
    You rock! Thanks so much - and to everyone who answered as well! I'm marking this as answer. – clines Jun 04 '13 at 21:15
  • @Aacini -- Yes, you are right! I know this also from other forums. And this belongs always only batch **and** no one of the critics has a bit well-foundet knowledge. "Take Powershell, install Cygwin, why not Java ..." Imo the people believe, they must hate Microsoft and batch is it's representative on earth. – Endoro Jun 04 '13 at 21:18
1

Here's an alternative.

@echo off
for /f "delims=" %%a in (user.txt) do (
 for /f "delims=0123456789" %%b in ("text-%%a") do (
   if not "text-%%a"=="%%b" (
      echo execute your routine here "%%a" has numbers
  )
 )
)
foxidrive
  • 40,353
  • 10
  • 53
  • 68
  • I marked this as an answer. It's brilliant, and short. I think I'll use this one. Thank you so much for the help! – clines Jun 05 '13 at 15:11
  • Glad it helps. Just be aware that (as it was) if the username is ALL numbers the the routine will not execute for that username. I've fixed that in the edit above. – foxidrive Jun 05 '13 at 18:06
  • We don't have any user names that are all numbers, so it shouldn't be a problem. – clines Jun 05 '13 at 22:00
  • So in my initial testing, this worked fine. However, the computers that I need to use the full script on will have several user names with numbers that need to be rooted out. What I'm finding with this script is that it will remove ONE of those accounts-with-numbers, but not all of them. I think that the issue may be more with me (who am not very saavy with scripting) rather than the script itself. However, any insight would certainly be appreciated. – clines Jun 06 '13 at 20:01
  • What is in user.txt? Which line is being ignored that should be processed? Perhaps it's the way you have implemented the script, as you said... – foxidrive Jun 07 '13 at 02:13
  • Here's the contents of the output. The first contains all of the user accounts, and the second *should* have all of the number-containing accounts removed: ADMINI~1 PC Public test123 test234 test3455 Here is the second file: ADMINI~1 PC Public test123 test234 I've repeated the test a number of times with the same result. – clines Jun 07 '13 at 16:21
  • I'm sorry, I broke the code with my additions and didn't test it. I've tested it now and the code above works here with your sample. NOTE that a username of 123test will be flagged too, if that is an issue. It doesn't check for trailing numbers. – foxidrive Jun 07 '13 at 16:38
  • Okay, yeah, your code works brilliantly now! Thank you so much for your help. – clines Jun 07 '13 at 21:24
-3

What you want is a regular expression. You might look into rewriting your script with PowerShell -- or perl, if you can get away with installing it. Either one is a major initial learning curve relative to sticking with batch files, but it'll pay off lavishly over time. An lot of tasks that are horrible in a batch file are trivial in a "real" scripting language like either one of those.

  • Windows Command Shell Scrip Language (WCSSL) has the Regex power to differentiate between numbers and letters. Your answer is boorish and silly. – Endoro Jun 04 '13 at 17:03