Batch files

April 5, 2009 at 04:51:07
Specs: Windows XP
I am trying to create a batch file that does two things
1. determine the user that is currently logged on
2.Check to see if a directory exists (of which the user variable forms part) and if not, the directory must be created.
This is the code that I'm currently using, but doesn't appear to work as expected.

@echo off
for /f “tokens=3 delims=\” %%i in (”%USERPROFILE%”) do (set user=%%i) 2>&1
echo “logged in user:%user%”
if not exist C:\Documents and Settings\%user%\My Documents\My Music\Jazz MD C:\Documents and Settings\%user%\My Documents\My Music\Jazz

any help will be greatly appreciated.

See More: Batch files

Report •

April 5, 2009 at 05:29:12
You must surround paths that contain spaces with double quotes:

if not exist "C:\Documents and Settings\%user%\My Documents\My Music\Jazz" MD "C:\Documents and Settings\%user%\My Documents\My Music\Jazz"

You may also be interested the system variable %username%.


On top of that you should be able to use:

if not exist "%userprofile%\My Documents\My Music\Jazz" MD "%userprofile%\My Documents\My Music\Jazz"

Report •

April 5, 2009 at 06:11:03
Thanks. The first part about the quotes seem to work, but I couldn't quite understand what you ment about the userprofile detention code. Could you write that part of the code out for me the way I would use it on top of the file detection/creation code.

Report •

April 5, 2009 at 15:05:45
%userprofile% is a system variable that expands to the current users profile folder (ie c:\documents and settings\<user>). %username% expands to the current username.

Report •

Related Solutions

Ask Question