We help IT Professionals succeed at work.

BATCH FILE HELP

Hi,
Im having problems and need help with a batch file. This is a practice in our textbook for the final exam in two weeks and I want to be able to know what Im doing when I take the final. Ok so the batch file needs to allow the person who is running the script to input on or more user names on the command line then it has to clear out each specified user’s Temp Folder and the user’s temporary internet file.
So far this is what I have for the batch file. Its not really much of a start and I am very confused:
@Echo OFF
cls
Set /p username="Please enter a username:"

Im not even sure if this will allow me to enter multiple user names. I don’t know what to do next or where to even begin.
I NEED HELP PLEASE
Thanks
Comment
Watch Question

Bill PrewTest your restores, not your backups...
Expert of the Year 2019
Top Expert 2016

Commented:
I would say you'd want to only accept one name at a time, and then loop back and ask for another after you process it.  You will want to clear out the username variable before the set /p to be safe.  And then if they leave it blank maybe use that as a test to end processing.

What version of windows are you targeting?  You know that those files you need to clear out are in different named locations depending on windows version?  The temp folder is pretty easy, if you mean the folder referenced by the TEMP environment variable?

~bp

Author

Commented:
im using windows 7 and the problem says I have to allow for multiple user names to be entered. Im so lost right now.

Author

Commented:
I dont even know what to type at this point. Ive spent over 4 hours tonight alone on this

Commented:
This sounds like an problem to exercise the SHIFT command.
My first question would be does it have to clear the documents after all the names are entered, or can you enter a name, clear the temp files and then ask for the next name?

If its the first, then I would write each name to a text file, and loop while the name does not equal some value, say 'delete'. Then once the user types 'delete' the batch file loops through the text file with the user names and deletes the files.

If you can use the second method, then you can create your loop as something like (Using psuedocode as I can't give you the real info for your test ;)

Loop here
prompt for username
if username IS NOT endterm
for all files in Windows variable for temp files/username DEL

if username IS endterm
LOOP

Open in new window


Author

Commented:
ok so with what I have so far what would be the next lines I would type if i was using the shift command. They just introduced these batch files to us and its new to me.
Bill PrewTest your restores, not your backups...
Expert of the Year 2019
Top Expert 2016

Commented:
What's the course, DOS batch scripting not too popular these days.  And do be honest with us, if this is a homework assignment you need to submit, then the Experts will not want to do it for you, only assist when you get stuck.  But if this truly is studying for potential questions or techniques that's not quite as bad.

Okay, to get you thinking, save this as a BAT file and give it a run.

@echo off

:MainLoop
  set UserName=
  set /P UserName=Enter next username to clean, or leave blank to exit:
  if "%UserName%" == "" goto :Done

  echo Processing user = %UserName%
  REM Other logic could be added here...

  goto :MainLoop

:Done
  echo Done.

Open in new window

~bp

Author

Commented:
it has to clear the user temp folder and user temp internet folder after all the names are entered. And this isnt for the test. This is a practice problem from my book to help us understand and prepare for the test

Author

Commented:
No this isnt for a test this is simply to help us prepare and learn more for the upcoming test
Bill PrewTest your restores, not your backups...
Expert of the Year 2019
Top Expert 2016

Commented:
So, can you create the commands you would need to delete those files, if you knew a username?

~bp

Author

Commented:
ok i did that and It lets me enter the multiple user names now the trick is figuring out how to delete those files.

Author

Commented:
I would have to use the find command?

Commented:
Normally I'd agree with billprew: Do your own homework.

However, since he followed that with a working solution...  My solution using SHIFT is based on this part of the requirement: "input on[sic] or more user names on the command line"

Given that, I'd picture a command line like:

CleanTemp.bat user1 user2 user3

and a batch file like:

@echo off

:top

if "%1" == "" goto :done
echo Clean %1

shift
goto :top

:done

Author

Commented:
would it be del c:\users\temp or something like that. Because the book says that we shouldnt have to create new users on our computer just if we need or want to test it. so it has to be for anyone to use basically

Author

Commented:
i really dont understand how the shift command works and what I need to do to get it to work. Im just asking for help this isnt a graded assignment at all I just wanna be prepared and learn what to do.
Test your restores, not your backups...
Expert of the Year 2019
Top Expert 2016
Commented:
Here's an example of prompting for a list of users, and them using SHIFT to process each one.

@echo off

REM Clear out variable we will prompt for users into
set UserNames=

REM Prompt for one of more users to process (comma seperated)
set /P UserNames=Enter list of users to process, comma seperated:

REM If some users entered, call subroutine to process them
if "%UserNames%" NEQ "" call :ProcessUsers %UserNames%

REM Wrap up and exit
echo Done.
goto :EOF

REM Subroutine to process list of users, one at a time
:ProcessUsers [user-list]
  REM Get the next user from the list, if none then exit this loop
  if "%1" == "" goto :EOF

  REM Process this user
  echo Processing user = %1
  REM Other logic could be added here...

  REM Shift list of users left one position to get next entry
  shift

  REM Loop to process next user
  goto :ProcessUsers

Open in new window

~bp
Bill PrewTest your restores, not your backups...
Expert of the Year 2019
Top Expert 2016

Commented:
Here's some info on SHIFT:

http://www.computerhope.com/shift.htm

~bp
Bill PrewTest your restores, not your backups...
Expert of the Year 2019
Top Expert 2016

Commented:
Have you learned about environment variables?

~bp

Author

Commented:
verry little about enviorment variables. This is starting to make abit more sense im still confused on how to delete the temp folder or file
Bill PrewTest your restores, not your backups...
Expert of the Year 2019
Top Expert 2016

Commented:
You'll want to research environment variables, especially TEMP, and USERPROFILE.  If you go to a DOS command prompt, type the SET command and see all the system defined environmnt variables.  These help you know the folders where certain files are located, and make your scripts easier to code and maintain.

To remove files you are likely either using the DEL command, or the RD command,.  The first just deletes files, the second can remove whole folder trees, including subfolders.

~bp

Author

Commented:
im guessing under where you say processing user where you say other logic..... that is where i would put the deleting part. How would i make it so that that file path is for whatever the user types and not for one user
Bill PrewTest your restores, not your backups...
Expert of the Year 2019
Top Expert 2016

Commented:
You would reference the variables in the script.  So for example in my first example, one user entered at a time, you can do something like:

DEL "C:\Users\%UserName%\AppData\Local\Temp\*.*"

The %UserName% will be replaced by it's value before the DEL command is executed.

But that path is way different on Win7 and WinXP, so that gets complicated.

~bp

Author

Commented:
I know im having problems fining the path on windows 7
Bill PrewTest your restores, not your backups...
Expert of the Year 2019
Top Expert 2016

Commented:
Also, unless you are running with elevated access controls on Win7 I doubt you can delete files from another users profiles.  This feels like some non trivial BAT scripting for what it's worth, not basic stuff...

~bp

Author

Commented:
so thats all i have to do then is type in that path. I mean im getting a message that says it can find specified path but do i have to put a if or set before the del at all
Bill PrewTest your restores, not your backups...
Expert of the Year 2019
Top Expert 2016

Commented:
Would be useful to check that the folder exists first with IF EXIST, since they could enter a user that doesn't exist on the system.

~bp

Author

Commented:
it still wont delete the temp folder i found the path
Bill PrewTest your restores, not your backups...
Expert of the Year 2019
Top Expert 2016

Commented:
What code are you using?  Are you getting any errors?

~bp

Author

Commented:
del c:\users\%username%\appdata\temp   but i dont know how to make it delete the folder it just echos what i type i dont have echo in front of it though. Im really confused.what if code do i need because i thought that was it but i guess i need more code now
Bill PrewTest your restores, not your backups...
Expert of the Year 2019
Top Expert 2016

Commented:
Type the following at a command prompt, what does it display, exactly?

SET TEMP

~bp

Author

Commented:
@echo off
REM Clear out variable we will prompt for users into
set UserNames=
REM Prompt for one of more users to process (comma seperated)
set /P UserNames=Enter list of users to process, comma seperated:
REM If some users entered, call subroutine to process them
if "%UserNames%" NEQ "" call :ProcessUsers %UserNames%
REM Wrap up and exit
echo Done.
goto :EOF
REM Subroutine to process list of users, one at a time
:ProcessUsers [user-list]
REM Get the next user from the list, if none then exit this loop
if "%1" == "" goto :EOF
REM Process this user
echo Processing user = %1
If "%1" == "%username% goto del c:\users\%username%\AppData\local\temp
REM Other logic could be added here...
REM Shift list of users left one position to get next entry
shift
REM Loop to process next user
goto :ProcessUsers

This is what I have

Author

Commented:
ok here is what i typed
@echo off
REM Clear out variable we will prompt for users into
set UserNames=
REM Prompt for one of more users to process (comma seperated)
set /P UserNames=Enter list of users to process, comma seperated:
REM If some users entered, call subroutine to process them
if "%UserNames%" NEQ "" call :ProcessUsers %UserNames%
REM Wrap up and exit
echo Done.
goto :EOF
REM Subroutine to process list of users, one at a time
:ProcessUsers [user-list]
REM Get the next user from the list, if none then exit this loop
if "%1" == "" goto :EOF
REM Process this user
echo Processing user = %1
If "%1" == "%username%"
SET TEMP del c:\users\%username%\AppData\local\temp
REM Other logic could be added here...
REM Shift list of users left one position to get next entry
shift
REM Loop to process next user
goto :ProcessUsers
 now it says
Processing user= emelvin
The syntax of this command is incorrect
Bill PrewTest your restores, not your backups...
Expert of the Year 2019
Top Expert 2016

Commented:
This line has several problems:

If "%1" == "%username% goto del c:\users\%username%\AppData\local\temp

Replace by:

rd /s /q "c:\users\%username%\AppData\local\temp"

~bp
Bill PrewTest your restores, not your backups...
Expert of the Year 2019
Top Expert 2016

Commented:
I didn't want you to add that to the BAT file, just start a new DOS command window and type it there.

~bp

Author

Commented:
will that delete the folders because it says in my book that the folders have to be empty to use the rd command
Bill PrewTest your restores, not your backups...
Expert of the Year 2019
Top Expert 2016

Commented:
The /S option will also delete all files and subfolders from the directory, and then remove it too.

~bp

Author

Commented:
ok it still has the same error message syntax is wrong

Author

Commented:
ok i got it but it says for alot of the files: process cannot access the file because its being used by another process and the other files say that the directory is not empty

Author

Commented:
somehow it doesnt access emelvin temp folders it accesses admin temp folders

Author

Commented:
like the username is stuck on admin and wont go to a diff user name

Author

Commented:
@echo off
REM Clear out variable we will prompt for users into
set UserNames=
REM Prompt for one of more users to process (comma seperated)
set /P UserNames=Enter list of users to process, comma seperated:
REM If some users entered, call subroutine to process them
if "%UserNames%" NEQ "" call :ProcessUsers %UserNames%
REM Wrap up and exit
echo Done.
goto :EOF
REM Subroutine to process list of users, one at a time
:ProcessUsers [user-list]
REM Get the next user from the list, if none then exit this loop
if "%1" == "" goto :EOF
REM Process this user
echo Processing user = %1
rd /s /q "c:\users\%username%\AppData\local\temp"
REM Other logic could be added here...
REM Shift list of users left one position to get next entry
shift
REM Loop to process next user
goto :ProcessUsers
 this is what i have but why wont it go to that users folders

Author

Commented:
im so lost

Author

Commented:
ok i did the set temp thing and it came up with C:\users\admin\appdata\local\temp

Commented:
Do you understand the concept of a "variable?"  A variable gets assigned a value, and it keeps that value until a new value gets assigned.

Now, look at your code.  You are using a variable named %username%.  That is one of the variables that the OS assigns for you to make it easy for batch files figure out who is running them.

Since you never change the value of %username% (which is good, don't change the value of %username%), that means that every time you use that variable, it will always have the same value.

So, every time you call "rd /s /q "c:\users\%username%\AppData\local\temp""

%username% will always have the same value (apparently "admin").  So it should not be surprising that it always tries to remove the same directory.  

Look at your code again.  Do you have a *different* variable that changes over time?  One that will contain the names of each of the users you want to remove?

Author

Commented:
do i have to use a %1 %2 and so on

Author

Commented:
im sorry im really new to this and im trying to follow you

Commented:
Well, you print out the value of %1 each time it passes thru the loop (along with the words "Processing user").  When you look at what it is printing, is that what you want?

Author

Commented:
no thats not what i want so how would i go about fixing that do i need to make it so it changes from %1 to %2 and so on....

Commented:
Really?  What prints out on the lines that contain "Processing user?"

Author

Commented:
the user name

Commented:
The user name "admin"?  Or each of the user names that you entered when prompted for users, one per line?

Author

Commented:
but how would i make it so that the username that prints out on the line processing user to be the folder it accesses

Author

Commented:
each of the user names that I entered one per line
Commented:
Aha.  So, when you use %username%, and that gives you:

   rd /s /q "c:\users\admin\AppData\local\temp"
   rd /s /q "c:\users\admin\AppData\local\temp"
   rd /s /q "c:\users\admin\AppData\local\temp"

and when you use %1, that gives you

   Processing user = User1
   Processing user = User2
   Processing user = User3

and what you want is

   rd /s /q "c:\users\User1\AppData\local\temp"
   rd /s /q "c:\users\User2\AppData\local\temp"
   rd /s /q "c:\users\User3\AppData\local\temp"

What do you need to change?

Author

Commented:
i need to change the %username% in the line to %1?

Author

Commented:
but i tried putting %1 in place of %username% and it said syntax error

Author

Commented:
am i even close

Commented:
Show me your current code.  And tell me what you are entering for your usernames.

Author

Commented:
@echo off
REM Clear out variable we will prompt for users into
set UserNames=
REM Prompt for one of more users to process (comma seperated)
set /P UserNames=Enter list of users to process, comma seperated:
REM If some users entered, call subroutine to process them
if "%UserNames%" NEQ "" call :ProcessUsers %usernames%
REM Wrap up and exit
echo Done.
goto :EOF
REM Subroutine to process list of users, one at a time
:ProcessUsers [user-list]
REM Get the next user from the list, if none then exit this loop
if "%1" == "" goto :EOF
REM Process this user
echo Processing user = %1
rd /s /q "c:\users\%username%\AppData\local\temp"
REM Other logic could be added here...
REM Shift list of users left one position to get next entry
shift
REM Loop to process next user
goto :ProcessUsers

Im entering emelvin, admin

Commented:
That code doesn't use %1?

Author

Commented:
well when i did that I got that syntax error
I replaced the line:
rd /s /q "c:\users\%username%\AppData\local\temp" with
rd /s /q "c:\users\%1\AppData\Local\temp
now that is what I have

Commented:
There is no syntax error on that line.  Where does the syntax error message print out (in comparison to Done, Processing user, etc)?

And has this command successfully removed the 2 directories?

Author

Commented:
the system cannot find the file specified.    

Commented:
I'm sorry, I don't understand your response.  Does that mean that the 2 folders are gone?

And where does the syntax error message print out (in comparison to Done, Processing user, etc)?

Also, you should probably add this:

   md "c:\users\%1\AppData\local\temp

right after the rd command.  You will need that directory to exist.

Author

Commented:
well theres no folders that come up it says processing user= emelvin and then under that it says the system cannot find the file specified.

Author

Commented:
@echo off
REM Clear out variable we will prompt for users into
set UserNames=
REM Prompt for one of more users to process (comma seperated)
set /P UserNames=Enter list of users to process, comma seperated:
REM If some users entered, call subroutine to process them
if "%UserNames%" NEQ "" call :ProcessUsers %usernames%
REM Wrap up and exit
echo Done.
goto :EOF
REM Subroutine to process list of users, one at a time
:ProcessUsers [user-list]
REM Get the next user from the list, if none then exit this loop
if "%1" == "" goto :EOF
REM Process this user
echo Processing user = %1
rd /s /q "c:\users\%1\AppData\local\temp"
md "c:\users\%1\AppData\local\temp"
REM Other logic could be added here...
REM Shift list of users left one position to get next entry
shift
REM Loop to process next user
goto :ProcessUsers
 
this is what i have now

Author

Commented:
do i have to have something around the %1 like "" or something

Commented:
1) So, when you say you are getting a "syntax error," does that mean you are really getting an error that says "The system cannot find the file specified?"  Or do you really get some message that says "Syntax error?"
2) Have you added the line I suggested you add?  Did it make this problem go away?

Author

Commented:
im not getting the syntax error just the message that it cant find the file specified and yes i put it right under the line you told me too and still have the same exact outcome. I dk what im doing wrong.

Commented:
Run it again.  I'll bet it works this time.

To be thorough, I suppose you could change your RD to

   if exist "c:\users\%1\AppData\local\temp" rd /s /q "c:\users\%1\AppData\local\temp"

Author

Commented:
nope. got nothing for the emelvin and the same stuff for the admin. This is really frustrating i feel this is impossible but i dont wanna give up

Commented:
Even after you added the "if exist"?

Author

Commented:
@echo off
REM Clear out variable we will prompt for users into
set UserNames=
REM Prompt for one of more users to process (comma seperated)
set /P UserNames=Enter list of users to process, comma seperated:
REM If some users entered, call subroutine to process them
if "%UserNames%" NEQ "" call :ProcessUsers %usernames%
REM Wrap up and exit
echo Done.
goto :EOF
REM Subroutine to process list of users, one at a time
:ProcessUsers [user-list]
REM Get the next user from the list, if none then exit this loop
if "%1" == "" goto :EOF
REM Process this user
echo Processing user = %1
If exist "c:\users\%1\AppData\local\temp" rd /s /q "c:\users\%1\AppData\local\temp"  
md "c:\users\%1\AppData\local\temp"
REM Other logic could be added here...
REM Shift list of users left one position to get next entry
shift
REM Loop to process next user
goto :ProcessUsers

this is what i have now

Author

Commented:
yea it still didnt work

Commented:
And what does the output look like?  You can single-click on the command prompt logo and use Edit/Mark, then use the mouse to highlight the output.  Then single-click on the command prompt logo and use Edit/Copy.

Paste the results into your response.

Author

Commented:
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Users\Admin>cd c:\batchfiles

c:\Batchfiles>cleaner.bat
Enter list of users to process, comma seperated:emelvin, admin
Processing user = emelvin
Processing user = admin
c:\users\admin\AppData\local\temp\FXSAPIDebugLogFile.txt - The process cannot ac
cess the file because it is being used by another process.
c:\users\admin\AppData\local\temp\~DF20C1527ABD94AB76.TMP - Access is denied.
c:\users\admin\AppData\local\temp\~DF4130EFCE4AED8D70.TMP - Access is denied.
c:\users\admin\AppData\local\temp\~DFA4C95446AC476220.TMP - Access is denied.
c:\users\admin\AppData\local\temp\~DFD8F4E740E509A65D.TMP - Access is denied.
A subdirectory or file c:\users\admin\AppData\local\temp already exists.
Done.

c:\Batchfiles>

Commented:
Well, I'm done for now.  But before I go, let me have you ponder this thought:

I submit that the batch file is now correctly removing temp files (although not the temporary IE files which are stored in a different location).  Yes, even with the messages you are seeing.  

Ponder that.  Perhaps billprew will come back to add his thoughts.
Bill PrewTest your restores, not your backups...
Expert of the Year 2019
Top Expert 2016

Commented:
Just looking at this thread again this morning, it seems to have expanded a lot over night :-).

Seems like you are getting closer.  The "access is denied" messages you are seeing could be quite normal, there can be files in the TEMP folder that are currently in use, and therefore you will not be able to delete them.

~bp