1.

Solve : Set sub-variable...??

Answer»

I was RUNNING into some problems with set. I was trying to replace the word "Desktop" (without the quotes) with

"%userprofile%\Desktop (the quote at the front is needed).

Here is my code:
Code: [Select]set /p command=
"delims= " %%a in ("%command%") do set comma=%%a
if /i %comma%==copy set command=%command:desktop="%userprofile%\Desktop%
if /i %comma%==copy set command=%command%"
%command%
When I tried doing Copy Desktop\Messages.txt (which DOES exist) I get a file not found ERROR...

What am I doing wrong?

For /f missing from line 2.

Insert Echo %command% as line 4 to see what %command% is being set to, it looks slightly dodgy.

Try this as an alternative:
Code: [Select]Set /p command=Enter Command Path and Filename:

For /f %%a in ("%command%") do set comma=%%a

if %comma%==copy pushd %userprofile%

%command%

popd
Quote from: Dusty on April 10, 2009, 02:50:17 AM

For /f missing from line 2.

Insert Echo %command% as line 4 to see what %command% is being set to, it looks slightly dodgy.

Try this as an alternative:
Code: [Select]Set /p command=Enter Command Path and Filename:

For /f %%a in ("%command%") do set comma=%%a

if %comma%==copy pushd %userprofile%

%command%

popd
When I used FOR while doing the other commands, it worked just fine...But how does your code work dusty?Quote
But how does your code work dusty?

Well, I hope it works.

When it's run you would respond to the Set /p with copy desktop\messages.txt

What the destination path\filename is didn't appear in your original post.

The NEXT line simply sets %%a to the first entry in %command% which would be copy

The If command tests %comma% for copy and if true Pushd's the default directory to %userprofile% which is C:\Documents and Settings\Username

Then %command% is executed

Popd then returns the default directory to where it was before Pushd was executed.

Hope this helps and that I haven't made a FOOL of myself again Quote from: Dusty on April 10, 2009, 07:20:52 AM
Well, I hope it works.

When it's run you would respond to the Set /p with copy desktop\messages.txt

What the destination path\filename is didn't appear in your original post.

The next line simply sets %%a to the first entry in %command% which would be copy

The If command tests %comma% for copy and if true Pushd's the default directory to %userprofile% which is C:\Documents and Settings\Username

Then %command% is executed

Popd then returns the default directory to where it was before Pushd was executed.

Hope this helps and that I haven't made a fool of myself again
I'll try. But why did you change the FOR loop? Did I do it wrong.

EDIT: Doesn't work...I think it's because %userprofile% has spaces in it. I think the only way around it is for a sub-variable...Quote from: Helpmeh
EDIT: Doesn't work...I think it's because %userprofile% has spaces in it. I think the only way around it is for a sub-variable...

I don't understand why it's not working for you. Any time I run the script and enter Copy Desktop\Messages.txt the file is copied to the default directory which is C:\Documents and Settings\Dusty

Any time I run the script and enter Copy Desktop\Messages.txt d:\temp\ the file is copied to d:\temp\

Do you get the advisory message "1 File(s) copied"?

Quote from: Helpmeh
But why did you change the FOR loop? Did I do it wrong.

I think the only change I made to the For loop was to not include the "delims= ". Space is one of the default delimiters and therefore does not have to be specified. No, you didn't do it wrong, both methods produce the same result.

Returning to your original script Quote
set /p command=
"for /f delims= " %%a in ("%command%") do set comma=%%a
if /i %comma%==copy set command=%command:desktop="%userprofile%\Desktop%
if /i %comma%==copy set command=%command%"
%command%

This set Command to Code: [Select]"\messages.txtuserprofile" whereas what was wanted was Code: [Select]copy "c:\documents and settings\user\desktop\messages.txt"
Change the first If command line to
Code: [Select]If /i %comma%==copy set command=%comma% "%userprofile%\%command:~5%"
The second If command line is not required.

The entire script could now be:Code: [Select]set /p command=Enter Command Path\Filename

for /f "delims= " %%a in ("%command%") do set comma=%%a

if /i %comma%==copy set command=%comma% "%userprofile%\%command:~5%"

%command%
A shorter version could be:
Code: [Select]set /p command=Enter Command Path\Filename
if /i %command:~0,4%==copy set command=%command:~0,4% "%userprofile%\%command:~5%"
%command%




Quote from: Dusty on April 12, 2009, 10:42:53 PM
Returning to your original script
This set Command to Code: [Select]"\messages.txtuserprofile" whereas what was wanted was Code: [Select]copy "c:\documents and settings\user\desktop\messages.txt"
Change the first If command line to
Code: [Select]If /i %comma%==copy set command=%comma% "%userprofile%\%command:~5%"
The second If command line is not required.

The entire script could now be:Code: [Select]set /p command=Enter Command Path\Filename

for /f "delims= " %%a in ("%command%") do set comma=%%a

if /i %comma%==copy set command=%comma% "%userprofile%\%command:~5%"

%command%
A shorter version could be:
Code: [Select]set /p command=Enter Command Path\Filename
if /i %command:~0,4%==copy set command=%command:~0,4% "%userprofile%\%command:~5%"
%command%






What does the ~5 do in this case...I saw an example script when I first started scripting in batch using ~ and a number to cut off certain parts of a variable...and the file won't have the same length...Quote from: Dusty on April 10, 2009, 10:40:51 PM
I don't understand why it's not working for you. Any time I run the script and enter Copy Desktop\Messages.txt the file is copied to the default directory which is C:\Documents and Settings\Dusty

Any time I run the script and enter Copy Desktop\Messages.txt d:\temp\ the file is copied to d:\temp\

Do you get the advisory message "1 File(s) copied"?

I think the only change I made to the For loop was to not include the "delims= ". Space is one of the default delimiters and therefore does not have to be specified. No, you didn't do it wrong, both methods produce the same result.



I got a message stating that the file can't be found, secondly: Thanks, I didn't know that, and FINALLY: Sorry for the double-post...even though we both did Dusty


Edit: THANKS! It works GREAT!!!


Discussion

No Comment Found