Skip navigation

JSI Tip 0537 - Environment Variable string substitiution.

You can replace a character in an Environment Variable using the following syntax:

set VariableName=%VariableName:<ReplaceCharacter>=<ReplacementCharacter>%

If you want to rename a file from filename

.ext to UserName.YYYYMMDD where YYYYMMDD is todays date and any spaces in UserName are replaced with an _,
the following batch file would work for a USA locale: ( call JSIUsrDT <Drive:>\Directory\filename.ext)

for /f "tokens=1,2,3,4* delims=/ " %%i in ('date /t') do set JSIUsrDT=%UserName%

set JSIUsrDT=%JSIUsrDT: =_%
pushd %~dp1
rename %1 %JSIUsrDT%

Note:There is a space after the : in the set JSIUsrDT=%JSIUsrDT: =_% statement.

See tip 494 for the %~dp1 syntax.

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.