How to find cause of error from "CD /D %yourPath%"

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

How to find cause of error from "CD /D %yourPath%"

#1 Post by alan_b » 01 Jul 2010 11:31

I have used, but found an unfortunate side effect (that it may permanently add folders), with
MD %yourPath%" & RD %yourPath%"

I would greatly appreciate a similarly simple test without any side effect.

My un-installation script has a long list of folder structures, and for each a set of files etc that need deleting. My script reports each item it seeks, and whether it is already ABSENT, or if it is now DELETED, or if it is FROZEN by permissions issues and needs manual intervention.

My script processes each of the folder structures in turn, using "CD /D %yourPath%",
and when that fails I wish it to detect whether the path is ABSENT,
or FROZEN by permission issues. Thus far I have tested the errorlevel after
MD %yourPath%
If that succeeds then the path was ABSENT - otherwise manual intervention is needed.
I wish to "DO NO HARM" and to leave no side-effects, such as an unused folder that was not previously present, therefore I follow with
RD %yourPath%

I now find that because RD only removes the LAST folder of a multi-folder path,
it does not UNDO any earlier folders added by the CD.
e.g.
Starting with
%ALLUSERSPROFILE\Application Data\
MD %yourPath% may add 3 folders \Suite-A\Group-B\Version-C\, resulting in
%ALLUSERSPROFILE\Application Data\Suite-A\Group-B\Version-C\
Unfortunately RD %yourPath% only removes the last item, leaving
%ALLUSERSPROFILE\Application Data\Suite-A\Group-B\

I would appreciate a SIMPLE way for the script to determine ABSENT or FROZEN.
It must run on foreign language systems so cannot search error messages for "access is denied".
There may be up to one thousand lines of items showing ABSENT, FROZEN, etc,
so trying to catch an "access is denied" message is NOT convenient, and it needs to be classified and added to relevant sub-totals that are reported on the final screen.

My script has far too much complexity due to REG.EXE telling lies when asked to delete keys,
and I would rather "DO HARM" by leaving two redundant unwanted folders rather than have the extra pain of "walking through the path" to determine how much is new and needs to be removed - but I would like a simple solution if possible.

I fear my request is impossible - but you have always come through for me before ! !

AAAGGGHHHH - Immediately after typing everything and previewing, just before the SUBMIT,
I suddenly thought CACLS - the thing that has so much potential for disaster I prefer not to think about it.

I now find that "CACLS %yourPath%" will set %ERRORLEVEL% to :-
zero if %yourPath% exists regardless of whether I have access; or
two if %yourPath% does not exist at all regardless of permissions.
I have only tested one path I know is missing, one path to which I have access, and one path to which I have no access.

PLEASE WARN ME if I have overlooked something, or if there is a better way.

Regards
Alan

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to find cause of error from "CD /D %yourPath%"

#2 Post by aGerman » 01 Jul 2010 15:10

Alan

Unfortunately I'm not that familar with permissions. But using CACLS sounds good in case you would work on a local computer. Sorry that I can't help more.

It must run on foreign language systems so cannot search error messages for "access is denied".

Indeed this is more complicated.
%ALLUSERSPROFILE%\Application Data\
does not exist for my German settings. It's
%ALLUSERSPROFILE%\Anwendungsdaten\
Maybe you should read the registry:

Code: Select all

@echo off &setlocal
for /f "tokens=2* delims=   " %%a in ('reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Common AppData"') do set "CommonAppData=%%b"
echo %CommonAppData%
pause

Attention! The delimiter is shown wrong in the forum (like 3 spaces). It has to be a TAB character!


Unfortunately RD %yourPath% only removes the last item

Hmm, what about

Code: Select all

for /f "delims=\" %%a in ("%yourPath%") do rd /s /q "%%a"


Regards
aGerman

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Re: How to find cause of error from "CD /D %yourPath%"

#3 Post by alan_b » 01 Jul 2010 15:44

Thanks for feedback and information.

I am working on an application that is installed/removed locally by the user.
I do not envisage any central rollout/deployment.

I released a "Beta" version of my script a few months ago,
and a German (not you) reported a problem due to my searching for "Access is Denied".
Hence my current intention to be language independent.

I was pleasantly surprised that although the error messages were not in the language I use,
the Registry Keys that I tried to delete have the same spelling as I am used to.

I was thinking that the Registry Keys that are most difficult to get rid of,
and when I first heard that the Swedish XP has something different for "DeskTop" I thought
"nothing I can do about that some-one else can fix that problem".

Please advise me, is there a Registry Key with a fixed name that will contain as a value the variable word "Application Data" on "English" Windows, and "Anwendungsdaten" on "German" Windows, or is there some other way you can advise which will tell me the "foreign translation" for the major folder names ?

for /f "delims=\" %%a in ("%yourPath%") do rd /s /q "%%a"
I fear it will not know when to stop ! !
i.e. the target path might for example be a chain of 10 folders,
and if only 7 folders exist before I test and as a result append 3 more,
then only those last 3 should be deleted to achieve "normality".

Regards
Alan

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to find cause of error from "CD /D %yourPath%"

#4 Post by aGerman » 01 Jul 2010 15:57

alan_b wrote:Please advise me, is there a Registry Key with a fixed name that will contain as a value the variable word "Application Data" on "English" Windows, and "Anwendungsdaten" on "German" Windows, or is there some other way you can advise which will tell me the "foreign translation" for the major folder names ?


Well, it's like my example above shows.
Have a look at
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders
for "All Users" special folders and
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders
for the current users special folders. The value names should be the same for each language.

Regards
aGerman

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Re: How to find cause of error from "CD /D %yourPath%"

#5 Post by alan_b » 02 Jul 2010 02:14

Many thanks

I will finish and release my script in English - more testing needed,
Then I will try to incorporate your registry information in version 2.

Regards
Alan

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to find cause of error from "CD /D %yourPath%"

#6 Post by aGerman » 02 Jul 2010 11:27

alan_b wrote:Then I will try to incorporate your registry information in version 2.

Beware of ANSI/ASCII !!

In
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Nls\CodePage
you will find OEMCP for the default ASCII codepage (the cmd works with)
and ACP for the default ANSI codepage (used for data into the registry).

You should always change the codepage to ANSI before reading data in the registry and change back to ASCII after.
The reason is that you will find nonstandard ASCII characters. A German example:
Startmenü

Regards
aGerman

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

Re: How to find cause of error from "CD /D %yourPath%"

#7 Post by avery_larry » 02 Jul 2010 11:42

Wouldn't a simple "if exist" work?

Code: Select all

rd "whatever folder" >nul 2>nul
if errorlevel 1 (
   if exist "whatever folder" (
      echo rd failed and the folder exists -- FROZEN
      ) else (
         echo rd failed and folder doesn't exist -- ABSENT
   )
)

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Re: How to find cause of error from "CD /D %yourPath%"

#8 Post by alan_b » 02 Jul 2010 15:05

aGerman wrote:In
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Nls\CodePage
you will find OEMCP for the default ASCII codepage (the cmd works with)
and ACP for the default ANSI codepage (used for data into the registry).

You should always change the codepage to ANSI before reading data in the registry and change back to ASCII after.
The reason is that you will find nonstandard ASCII characters. A German example:
Startmenü

Regards
aGerman


OUCH - I DID NOT WANT TO KNOW THAT ! ! !

I remember using DOS 3.??? and DIXONS / CURRYS started selling P.C.s as a sideline to their T.V. business.
I tried DOS 4.??? in their local showroom, and found that that DOS 4 had lost the ability for "Pipe" the output of one program to another. I decided DOS 4 was a total loss.

Somewhat later I learnt that when I typed the magic "|" character, DOS read a different character because that P.C. was using a different codepage and none of the characters on the keyboard would give the desired character.
About that time I also learnt that striking the number pad sequence 1 2 4 whilst holding down the Alt key would give the required "|"

Also at that time M.S. had moved onto DOS 6 and into controversy.
They had DoubleSpace Disc compression which gained a reputation for losing all the disc contents.
I decided that DOS 3.??? would suit me for a few more years !

The only happy memory from that sorry saga was that Microsoft were found guilty of IP theft from Stag Electronics Disc Compression software. Big payments were made.
Now the poacher has become the gamekeeper with WGA parasitic whistle blowers and vigilant prosecution of anyone that "borrows" M.S. software. Hypocrites ! !

I did not want to know of this codepage aggravation, but thanks for the "Heads Up"

Regards
Alan

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Re: How to find cause of error from "CD /D %yourPath%"

#9 Post by alan_b » 02 Jul 2010 15:28

avery_larry wrote:Wouldn't a simple "if exist" work?

Code: Select all

rd "whatever folder" >nul 2>nul
if errorlevel 1 (
   if exist "whatever folder" (
      echo rd failed and the folder exists -- FROZEN
      ) else (
         echo rd failed and folder doesn't exist -- ABSENT
   )
)


Before I was given the secret of using "%%%%ALLUSERSPROFILE%%%%\MyPath" I used
CALL :SET_CD ".
And my :SET_CD code accepted the argument "%ALLUSERSPROFILE%\MyPath" as %1
That code used the test
IF EXIST %1 (ECHO FROZEN) ELSE (ECHO ABSENT) and only had partial success
I cannot now remember how it failed but only 2 of my 3 test cases announced FROZEN

I have just put your code into my test harness and am surprised to find it correctly reports FROZEN for all 3 test cases.
Either my previous tests were not quite correct,
or there is something special about using
IF EXIST %YOURPATH%
instead of the previous
IF EXIST %1

Thanks for bringing me back to "IF EXIST",
so much safer than a CACLS which is always ready to execute any stray extra arguments !

Regards
Alan

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to find cause of error from "CD /D %yourPath%"

#10 Post by aGerman » 02 Jul 2010 16:26

alan_b wrote:I did not want to know of this codepage aggravation, but thanks for the "Heads Up"

Don't be shy to read into the registry and to change the codepage. I did it so many times and it worked fine always.
You could call a subroutine to read the values that you need.

Code: Select all

@echo off &setlocal
call :RegRead

cd /d "%CommonAppData%"
echo %cd%

pause
goto :eof

::~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
:RegRead
for /f "tokens=3 delims=   " %%i in ('reg query "HKLM\SYSTEM\CurrentControlSet\Control\Nls\CodePage" /v "ACP"') do set /a ACP=%%i
for /f "tokens=3 delims=   " %%i in ('reg query "HKLM\SYSTEM\CurrentControlSet\Control\Nls\CodePage" /v "OEMCP"') do set /a OEMCP=%%i
chcp %ACP% >nul

for /f "tokens=3 delims=   " %%a in ('reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Common AppData"') do set "CommonAppData=%%a"

chcp %OEMCP% >nul
goto :eof


[Damn! This triple space behind delims= is wrong! Isn't there a way to display a TAB character the right way?]


Regards
aGerman

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Re: How to find cause of error from "CD /D %yourPath%"

#11 Post by alan_b » 03 Jul 2010 05:28

Thanks for that script.

Regards
Alan

Post Reply