String Manipulation on environment variables

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
milos78
Posts: 6
Joined: 28 Jan 2011 14:02

String Manipulation on environment variables

#1 Post by milos78 » 28 Jan 2011 14:15

Hi to all.

I want to extract a string from
C:\Documents and Settings\Administrator\Application Data
I need only the Application Data

environment variable %APPDATA% = C:\Documents and Settings\Administrator\Application Data
so I want to extract from it, since it is not the same on different windows language versions.
Example: Application data on German windows is C:\Dokumente und Einstellungen\All Users\Anwendungsdaten
so here I will need Anwendungsdaten

So basically I need the string from the end to the backslash "\"

I didn't find a way in DOS to search for string from EOF backwards to \ and then to delete the rest or to save from \ to EOF in a new variable.

If someone could help, I would be very grateful.

Milos

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

Re: String Manipulation on environment variables

#2 Post by aGerman » 28 Jan 2011 14:20

Try

Code: Select all

for /f "delims=" %%a in ("%appdata%") do echo %%~nxa

Regards
aGerman

milos78
Posts: 6
Joined: 28 Jan 2011 14:02

Re: String Manipulation on environment variables

#3 Post by milos78 » 28 Jan 2011 14:39

aGerman wrote:Try

Code: Select all

for /f "delims=" %%a in ("%appdata%") do echo %%~nxa

Regards
aGerman


thank you, I just typed this in cmd and I have an error:

%%a was unexpected at this time

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

Re: String Manipulation on environment variables

#4 Post by aGerman » 28 Jan 2011 14:51

Of course. This was made for a batch file.
For the command prompt use

Code: Select all

for /f "delims=" %a in ("%appdata%") do @echo %~nxa

Regards
aGerman

milos78
Posts: 6
Joined: 28 Jan 2011 14:02

Re: String Manipulation on environment variables

#5 Post by milos78 » 28 Jan 2011 16:40

aGerman wrote:Of course. This was made for a batch file.
For the command prompt use

Code: Select all

for /f "delims=" %a in ("%appdata%") do @echo %~nxa

Regards
aGerman


Thank you very very much !!!
Can you please make me a batch command for this.
I will assign a new variable to your command -let's call it %NEW%

Copy the file my.txt to C:\Documents and Settings\All Users\Application Data\

Meaning
%NEW% = for /f "delims=" %%a in ("%appdata%") do echo %%~nxa

copy "my.txt" %ALLUSERSPROFILE% \ %NEW%

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

Re: String Manipulation on environment variables

#6 Post by aGerman » 28 Jan 2011 17:55

Here you are

Code: Select all

for /f "delims=" %%a in ("%appdata%") do set "NEW=%%~nxa"
copy "my.txt" "%ALLUSERSPROFILE%\%NEW%\"

But note: There isn't such a path in Win7!

I suggest you should read the registry key instead

Code: Select all

for /f "tokens=2*" %%a in (
  'reg query "HKLM\SYSTEM\CurrentControlSet\Control\Nls\CodePage" /v "ACP" ^| find /i "ACP"'
) do chcp %%b >nul

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

for /f "tokens=2*" %%a in (
  'reg query "HKLM\SYSTEM\CurrentControlSet\Control\Nls\CodePage" /v "OEMCP" ^| find /i "OEMCP"'
) do chcp %%b >nul

copy "my.txt" "%CommonAppData%\"

It's slower and more code, but it's much safer.

Regards
aGerman

milos78
Posts: 6
Joined: 28 Jan 2011 14:02

Re: String Manipulation on environment variables

#7 Post by milos78 » 29 Jan 2011 03:51

Thank you very much!
In Win7 and in Vista yes, but the problem is Win XP, that is why I must to this.

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

Re: String Manipulation on environment variables

#8 Post by aGerman » 29 Jan 2011 18:10

I see. But reading the registry value will work for XP, Vista and Win7. Thats why I would prefer this.

Regards
aGerman

Post Reply