Solved

Batch File

Posted on 2013-05-20
10
367 Views
Last Modified: 2013-06-22
Hello could you please take a look at the batch file below and let me know why it's not working?    First part is suppose to create directories under all users and the second part is just copying the link to all users.  Does not seem to be working for me.  


@echo off

md "%USERPROFILE%\AppData\LocalLow" > nul 2>&1
md "%USERPROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs" > nul 2>&1

copy /y "D:\Program Files\ClearJavaCache\Clear Java Cache.lnk" "%USERPROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\" > nul 2>&1
0
Comment
Question by:SDJ_1
  • 5
  • 4
10 Comments
 
LVL 26

Expert Comment

by:pony10us
ID: 39182302
%USERPROFILE% refers to the currently logged on user and therefore will only apply the commands to that user's profile.
0
 

Author Comment

by:SDJ_1
ID: 39182841
Thanks for response.  This is how I set it up and it's doing what I need.

@echo off

cd C:\Users

md "%USERPROFILE%\AppData\LocalLow" > nul 2>&1
md ""%USERPROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs" > nul 2>&1

md ""%USERPROFILE%\AppData\LocalLow" > nul 2>&1
md ""%USERPROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs" > nul 2>&1

md ""%USERPROFILE%\AppData\LocalLow" > nul 2>&1
md ""%USERPROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs" > nul 2>&1


copy /y "D:\Program Files\ClearJavaCache\Clear Java Cache.lnk" ""%USERPROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\" > nul 2>&1
copy /y "D:\Program Files\ClearJavaCache\Clear Java Cache.lnk" ""%USERPROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\" > nul 2>&1
copy /y "D:\Program Files\ClearJavaCache\Clear Java Cache.lnk" ""%USERPROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\" > nul 2>&1


Exit

Can you give me a suggestion on how I can 'echo' the results to a .txt flle in another directory?
0
 
LVL 26

Expert Comment

by:pony10us
ID: 39182872
Create a folder in the root of C: (for example) called results. If you want the program to do it for you I would suggest doing an IF EXISTS, I can help with that if you need.



Anywhere you have:

 > nul 2>&1

change to:

>> c:\updates\results.txt

Except for the first time it appears.  Change that one to:

> c:\updates\results.txt
0
 

Author Comment

by:SDJ_1
ID: 39182950
Thanks.  Can you help me with the 'IF EXISTS'  option?    

The name of the the file I need the program to create is ClearJavaCache.txt and the location will be   C:\XXXLogs\
0
 
LVL 26

Expert Comment

by:pony10us
ID: 39184580
Untested:

@echo off

@For /F "tokens=1,2,3 delims=/ " %%A in ('Date /t') do @( 
 Set Day=%%A
 Set Month=%%B
 Set Year=%%C
 Set All=%%C%%B%%A
 )
 
REM  ***  Check for existance of folder - assumption that XXX is not a variable  ***

if not exist C:\XXXLogs\ mkdir C:\XXXLogs\

REM  *** If the file ClearJavaCache.txt exists rename it so that it appends the date at the end.  ***

if exist C:\XXXLogs\ClearJavaCache.txt ren C:\XXXLogs\ClearJavaCache.txt C:\XXXLogs\ClearJavaCache%All%.txt

cd C:\Users

md "%USERPROFILE%\AppData\LocalLow" > C:\XXXLogs\ClearJavaCache.txt
md ""%USERPROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs" >> C:\XXXLogs\ClearJavaCache.txt

md ""%USERPROFILE%\AppData\LocalLow" >> C:\XXXLogs\ClearJavaCache.txt
md ""%USERPROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs" >> C:\XXXLogs\ClearJavaCache.txt

md ""%USERPROFILE%\AppData\LocalLow" >> C:\XXXLogs\ClearJavaCache.txt
md ""%USERPROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs" >> C:\XXXLogs\ClearJavaCache.txt


copy /y "D:\Program Files\ClearJavaCache\Clear Java Cache.lnk" ""%USERPROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\" >> C:\XXXLogs\ClearJavaCache.txt
copy /y "D:\Program Files\ClearJavaCache\Clear Java Cache.lnk" ""%USERPROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\" >> C:\XXXLogs\ClearJavaCache.txt
copy /y "D:\Program Files\ClearJavaCache\Clear Java Cache.lnk" ""%USERPROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\" >> C:\XXXLogs\ClearJavaCache.txt


Exit

Open in new window


I provided some code to keep versions of the C:\XXXLogs\ClearJavaCache.txt by appending the date at the end of the file name.  prior to creating a new one.  

This code makes some assumptions:

1. The program only runs once per day
2. You realize that the date appended would be the day AFTER the file was created.
3. The XXX in the folder is not a variable
4. You will manually police the C:\XXXLogs folder and remove older files no longer needed

All of this can be handled however.

Always test in a non production environment and as usual, there are probably cleaner ways to accomplish what I have done.  :)
0
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

Author Comment

by:SDJ_1
ID: 39185157
ok thanks.  is there a way to change the wording in the .txt file to something like. "Script Complete?   And\or can it show date & time script started and ended?    Think I'm making this to complicated for myself.
0
 
LVL 26

Expert Comment

by:pony10us
ID: 39185205
Try this:


@echo off

@For /F "tokens=1,2,3 delims=/ " %%A in ('Date /t') do @( 
 Set Day=%%A
 Set Month=%%B
 Set Year=%%C
 Set All=%%C%%B%%A
 )
 
REM  ***  Check for existance of folder - assumption that XXX is not a variable  ***

if not exist C:\XXXLogs\ mkdir C:\XXXLogs\

REM  *** If the file ClearJavaCache.txt exists rename it so that it appends the date at the end.  ***

if exist C:\XXXLogs\ClearJavaCache.txt ren C:\XXXLogs\ClearJavaCache.txt C:\XXXLogs\ClearJavaCache%All%.txt

cd C:\Users

echo Started: %date% %time% > C:\XXXLogs\ClearJavaCache.txt

md "%USERPROFILE%\AppData\LocalLow" >> C:\XXXLogs\ClearJavaCache.txt
md ""%USERPROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs" >> C:\XXXLogs\ClearJavaCache.txt

md ""%USERPROFILE%\AppData\LocalLow" >> C:\XXXLogs\ClearJavaCache.txt
md ""%USERPROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs" >> C:\XXXLogs\ClearJavaCache.txt

md ""%USERPROFILE%\AppData\LocalLow" >> C:\XXXLogs\ClearJavaCache.txt
md ""%USERPROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs" >> C:\XXXLogs\ClearJavaCache.txt


copy /y "D:\Program Files\ClearJavaCache\Clear Java Cache.lnk" ""%USERPROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\" >> C:\XXXLogs\ClearJavaCache.txt
copy /y "D:\Program Files\ClearJavaCache\Clear Java Cache.lnk" ""%USERPROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\" >> C:\XXXLogs\ClearJavaCache.txt
copy /y "D:\Program Files\ClearJavaCache\Clear Java Cache.lnk" ""%USERPROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\" >> C:\XXXLogs\ClearJavaCache.txt

echo Ended: %date% %time% >> C:\XXXLogs\ClearJavaCache.txt

Exit

Open in new window



Added the two "echo" lines to provide date/time start/end
0
 

Author Comment

by:SDJ_1
ID: 39185518
Good aftenoon, thanks for all your help with this!
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 39214936
You are doing the same operations three times? And using "" (doubled double quote) doesn't make sense.
You are also creating the LocalLow folder (why), but create and use Roaming (which is correct).

If you just use
xcopy /y "D:\Program Files\ClearJavaCache\Clear Java Cache.lnk" "%USERPROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\" >> C:\XXXLogs\ClearJavaCache.txt

Open in new window

the Start Menu folder (and the whole path) will be created if necessary, so no MD needed.
0
 
LVL 26

Accepted Solution

by:
pony10us earned 500 total points
ID: 39216342
@Qlemo

Thank you, I just looked at what I posted and was shocked at how poorly it was done.  I didn't even notice that I had done the same operation 3 times.  Guess I was just following what the author had.

@echo off

@For /F "tokens=1,2,3 delims=/ " %%A in ('Date /t') do @( 
 Set Day=%%A
 Set Month=%%B
 Set Year=%%C
 Set All=%%C%%B%%A
 )
 
REM  ***  Check for existance of folder - assumption that XXX is not a variable  ***

if not exist C:\XXXLogs\ mkdir C:\XXXLogs\

REM  *** If the file ClearJavaCache.txt exists rename it so that it appends the date at the end.  ***

if exist C:\XXXLogs\ClearJavaCache.txt ren C:\XXXLogs\ClearJavaCache.txt C:\XXXLogs\ClearJavaCache%All%.txt

cd C:\Users

echo Started: %date% %time% > C:\XXXLogs\ClearJavaCache.txt

md "%USERPROFILE%\AppData\LocalLow" >> C:\XXXLogs\ClearJavaCache.txt
md ""%USERPROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs" >> C:\XXXLogs\ClearJavaCache.txt

copy /y "D:\Program Files\ClearJavaCache\Clear Java Cache.lnk" ""%USERPROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\" >> C:\XXXLogs\ClearJavaCache.txt

echo Ended: %date% %time% >> C:\XXXLogs\ClearJavaCache.txt

Exit

Open in new window


I agree that you don't need to put in the MD, it is really just the way I was taught many years ago and it's a hard habit to break.  :)

In fact, it does become almost a wasted step after the first time unless the folder gets removed for some reason.
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

To effectively work with Diskpart on a Server Core, it is necessary to write some small batch script's, because you can't execute diskpart in a remote powershell session. To get startet, place the Diskpart batch script's into a share on your loca…
I was supporting a handful of Windows 2008 (non-R2) 2 node clusters with shared quorum disks. Some had SQL 2008 installed and some were just a vendor application that we supported. For the purposes of this article it doesn’t really matter which so w…
This tutorial will walk an individual through locating and launching the BEUtility application and how to execute it on the appropriate database. Log onto the server running the Backup Exec database. In a larger environment, this would generally be …
To efficiently enable the rotation of USB drives for backups, storage pools need to be created. This way no matter which USB drive is installed, the backups will successfully write without any administrative intervention. Multiple USB devices need t…

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now