Page 1 of 1
Loop in Set
Posted: 07 Nov 2012 21:38
by john924xps
Code: Select all
@echo off
:save
set stringload=
set new=
if exist "%appdata%\BackupSaves.txt" (
echo How many strings do you want to load?
set /p stringload=::
cd "%appdata%"
<BackupSaves.txt (
for /l %%G in (0,1,%stringload%) do (
set /p _save%%G=)))
set _save
pause
The text file BackupSaves is full of paths to certain files and folders, one path on each line.
This program is meant to ask for a number of strings/lines to load, then... load it.
But it won't display it. There's always some sort of an error. I'm just using nested loops... a
for loop inside of one of those <file.txt loops... Help?
How do I count the number of lines in a text file?
Re: Loop in Set
Posted: 07 Nov 2012 23:16
by foxidrive
How do I count the number of lines in a text file?
find /c /v "" <file.txt
Re: Loop in Set
Posted: 08 Nov 2012 00:02
by Aacini
foxidrive wrote:john924xps wrote:@echo off
:save
set stringload=
set new=
if exist "%appdata%\BackupSaves.txt" (
echo How many strings do you want to load?
set /p stringload=::
cd "%appdata%"
<BackupSaves.txt (
for /l %%G in (0,1,%stringload%) do (
set /p _save%%G=)))
set _save
pause
This part
%%G is wrong and cause an error, you must use delayed expansion.
That is not true. Anyway, you don't explain how to use Delayed Expansion to solve the problem...
The original code read stringload variable inside if exist code block, so it needs Delayed Expansion to correctly expand it:
john924xps wrote:@echo off
setlocal EnableDelayedExpansion
:save
set stringload=
set new=
if exist "%appdata%\BackupSaves.txt" (
echo How many strings do you want to load?
set /p stringload=::
cd "%appdata%"
<BackupSaves.txt (
for /l %%G in (0,1,!stringload!) do (
set /p _save%%G=)))
set _save
pause
Also, note that first loaded variable is index 0, not 1, and that it loads N+1 variables.
May I suggest you to use array notation for your variables?
Antonio
Re: Loop in Set
Posted: 08 Nov 2012 01:08
by Aacini
When I posted my above answer, previous foxidrive's answer had a listing of original OP's code with %%G part in red. A couple minutes later there were two answers from billrich, the first one edited and empty. However, foxidrive's answer had changed but it have NOT the usual "edited 1 times" text, and first billrich answer had been deleted.
FAQ » How do I edit or delete a post? wrote:Unless you are a board administrator or moderator, you can only edit or delete your own posts. You can edit a post by clicking the edit button for the relevant post, sometimes for only a limited time after the post was made. If someone has already replied to the post, you will find a small piece of text output below the post when you return to the topic which lists the number of times you edited it along with the date and time. This will only appear if someone has made a reply; it will not appear if a moderator or administrator edited the post, though they may leave a note as to why they’ve edited the post at their own discretion. Please note that normal users cannot delete a post once someone has replied.
What happened in this case
Antonio
Re: Loop in Set
Posted: 08 Nov 2012 04:42
by jeb
The edit notice does not appear as foxidrive is also a moderator.
jeb
Re: Loop in Set
Posted: 08 Nov 2012 14:00
by Aacini
jeb wrote:The edit notice does not appear as foxidrive is also a moderator.
Wow, this is very interesting! This mean that moderators may modify their own posts (or anyone's posts, for that matter) with no trace of what they did
Yes, I know that you will reply that moderators
supposedly should not do that without a good reason, but anyway they can conceal their own errors in their posts with no trace of what they did (and normal users can't).
To illustrate what I am talking about, I recovered from my memory the deleted part of foxidrive's message and included it in my first reply above in the way I originally would included it. I am pretty sure that the text is practically the same as the original because I used it to assemble my own reply.
A similar situation happened to me in
this reply. A previous post from other user asked me for an explanation of my code and show the result, but that post was deleted so my reply looks somewhat incoherent now.
I wonder if I should quote certain texts in my future replies to avoid these situations...
Antonio
Re: Loop in Set
Posted: 08 Nov 2012 16:14
by foxidrive
Aacini wrote:but that post was deleted so my reply looks somewhat incoherent now.
You've now changed the context too. My info was clearly wrong so I removed it. Simple.
There's no skullduggery behind my motive...
Re: Loop in Set
Posted: 08 Nov 2012 16:59
by Ed Dyreen
Aacini wrote:Yes, I know that you will reply that moderators supposedly should not do that without a good reason, but anyway they can conceal their own errors in their posts with no trace of what they did (and normal users can't).
You simply replied while foxi was editing his post, it happens to me all the time...
Aacini wrote:I wonder if I should quote certain texts in my future replies to avoid these situations...
Some always do.
Aacini wrote:A similar situation happened to me in
this reply. A previous post from other user asked me for an explanation of my code and show the result, but that post was deleted so my reply looks somewhat incoherent now.
You responded to a user ( aka billrich ) who got himself banned, his post deleted.
The OP's question has been answered, we're so off-topic, chances are all our posts will be removed now
