Loop in Set

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
john924xps
Posts: 65
Joined: 08 Jun 2012 07:48

Loop in Set

#1 Post by john924xps » 07 Nov 2012 21:38

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?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Loop in Set

#2 Post by foxidrive » 07 Nov 2012 23:16

How do I count the number of lines in a text file?


find /c /v "" <file.txt

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Loop in Set

#3 Post by Aacini » 08 Nov 2012 00:02

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?

Code: Select all

_save[%%G]


Antonio
Last edited by Aacini on 08 Nov 2012 13:54, edited 1 time in total.

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Loop in Set

#4 Post by Aacini » 08 Nov 2012 01:08

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 :?: :shock:

Antonio

jeb
Expert
Posts: 1062
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Loop in Set

#5 Post by jeb » 08 Nov 2012 04:42

The edit notice does not appear as foxidrive is also a moderator.

jeb

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Loop in Set

#6 Post by Aacini » 08 Nov 2012 14:00

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 :?: :shock:

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Loop in Set

#7 Post by foxidrive » 08 Nov 2012 16:14

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...

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Loop in Set

#8 Post by Ed Dyreen » 08 Nov 2012 16:59

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 :oops:

Post Reply