Page 1 of 1

String Manipulation on environment variables

Posted: 28 Jan 2011 14:15
by milos78
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

Re: String Manipulation on environment variables

Posted: 28 Jan 2011 14:20
by aGerman
Try

Code: Select all

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

Regards
aGerman

Re: String Manipulation on environment variables

Posted: 28 Jan 2011 14:39
by milos78
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

Re: String Manipulation on environment variables

Posted: 28 Jan 2011 14:51
by aGerman
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

Re: String Manipulation on environment variables

Posted: 28 Jan 2011 16:40
by milos78
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%

Re: String Manipulation on environment variables

Posted: 28 Jan 2011 17:55
by aGerman
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

Re: String Manipulation on environment variables

Posted: 29 Jan 2011 03:51
by milos78
Thank you very much!
In Win7 and in Vista yes, but the problem is Win XP, that is why I must to this.

Re: String Manipulation on environment variables

Posted: 29 Jan 2011 18:10
by aGerman
I see. But reading the registry value will work for XP, Vista and Win7. Thats why I would prefer this.

Regards
aGerman