Append filename with %USERNAME%

Hewlett-packard / Iq505
February 2, 2009 at 06:58:57
Specs: Windows Vista , Dual Core/4GB
Hello again batch friends. I hope I haven't been missed too much. I need help renaming all files in a folder by appending the filename with the currently logged in username and the date.

For example. If I have a file called red.txt in a folder C:\Test
I need the new file name to be red_%Logged-in-USERNAME%.txt

I have looked at the environment variables but I can't get the %USERNAME% value to "stick" to the rename operation in my batch file.

Please help. Thanks.

See More: Append filename with %USERNAME%

Report •

February 2, 2009 at 07:34:03
for %f in (*) do ren "%f" "%~nf_%username%%~xf"

(Replace %f with %%f if you stick the above in a batch file, or just run as-is from the command prompt.)

Report •

February 2, 2009 at 13:25:40
Hi Klint. Thanks for the quick reply. I tried to use your script but unfortunately I am getting a very strange error. When I run the script the actual content of the batch file gets changed. I changed the code as you suggested for a batch script as follows....
for %%f in (*) do ren "%%f" "%~nf_%username%%~xf"
Please advise. Thanks again.

Report •

February 3, 2009 at 02:50:09
Sorry, I meant to say all variations of %f, so not just

for %%f in (*) do ren "%%f" "%~nf_%username%%~xf"

but the full treatment:

for %%f in (*) do ren "%%f" "%%~nf_%username%%%~xf"

Of course, I'd advise not to keep the batch file in the same directory or it would also get renamed.

Report •

Related Solutions

February 3, 2009 at 07:23:16
Thanks a bunch Klint, the revision works like a charmed one. :)

Report •

Ask Question