Please register or login. There are 0 registered and 1440 anonymous users currently online. Current bandwidth usage: 326.30 kbit/s December 15 - 08:35pm EST 
Hardware Analysis
      
Forums Product Prices
  Contents 
 
 

  Latest Topics 
 

More >>
 

    
 
 

  You Are Here: 
 
/ Forums / Programming /
 

  Creating sequentially named folders with a batch file 
 
 Author 
 Date Written 
 Tools 
Continue Reading on Page: 1, 2, Next >>
Reason   Mar 31, 2009, 05:48am EDT Reply - Quote - Report Abuse
Private Message - Add to Buddy List

Edited: Mar 31, 2009, 05:54am EDT

Replies: 23 - Views: 25393
Like the title says, I'm using a batch file to create a number of sequentially named folders, id, Folder 01, Folder 02, etc. I want to be able to specify the name, the starting number, and the ending number, so if I want to create NewFolders 45 thru 63, I just enter

NewFolders
45
63

Here's my .bat so far, edited for readability:

@echo off
cls
set /p n= [Directory name] ## Folder name, pretty obvious

set /p s= [Starting number] ## Also pretty obvious

set /p f= [Finishing number] ## Ditto

set /a counter=%s% ## Sets the starting number to the starting number that was input (can I do this in one step?)

:numbers ## labeled line for the loop

md %n%" #"%counter% ## Makes a Directory in format Foldername #X, where X is the current count

set /a counter=%counter%+1 ## Increases the count by one

if %counter% ==%f%+1 (goto :eof) else (goto :numbers) ## *Should* determine if it's the last number in the sequence; if so, End Of File, if not, go back to the line labeled :numbers


After creating 25,000 empty (but correctly named) folders... twice... I put in a failsafe:
if %counter% ==100 (goto :eof)

If I put this after the last line, it doesn't work because the logic dictates it will never ask if it's 100, only if it's %counter% +1. If I put it before the last line, it just goes to the failsafe number. It's like it only sees one "if" line. What am I doing wrong here, besides trying to be a 1337 h/-\><0R at 3 AM?

Here's the code not edited/commented so you can copy/paste.

@echo off
cls
set /p n= [Directory name]
set /p s= [Starting number]
set /p f= [Finishing number]
set /a counter=%s%
:numbers
md %n%" #"%counter%
set /a counter=%counter%+1
if %counter% ==%f%+1 (goto :eof) else (goto :numbers)


Also, is there a way to include preliminary 0s in the Folder Name, ie, 01, 02 or 001, 002, etc?


_________________
Ultima Ratio Regum
Want to enjoy fewer advertisements and more features? Click here to become a Hardware Analysis registered user.
Mar 31, 2009, 12:34pm EDT Reply - Quote - Report Abuse
Private Message - Add to Buddy List  
>> Re: Creating sequentially named folders with a batch file
Try this:

@ECHO OFF && CLS

REM get folder prefix, start and end count from parameters
REM we add one to the end counter now so we don't have to later
SET prefix=%1
SET /A count=%2
SET /A end=%3 + 1


REM start the loop
:MKDIR

REM make the directory
MKDIR %prefix%" #"%count%

REM increment by 1
SET /A count += 1

REM if we're at the end, return
REM start the loop over otherwise
IF %count%==%end% (GOTO :EOF) ELSE (GOTO :MKDIR)


Then just call it with parameters, eg:

seqfold.bat ORLY 5 7

gives:
ORLY #5
ORLY #6
ORLY #7

I assume this is what you want?

If you want to use a space in the prefix (eg. My Stuffs #1337) make sure you use quotes:

seqfolds.bat "My Stuffs" 1336 1339

As for padding with zeros, I could set that up with a FOR loop, but I feel like that's a hack. I know you can pad with spaces (but that wouldn't do you any good here), I'll look around and see what I can find out padding with zeros. This is why I use PowerShell or Cygwin most of the time. :P

Reason   Mar 31, 2009, 12:52pm EDT Reply - Quote - Report Abuse
Private Message - Add to Buddy List  
>> Re: Creating sequentially named folders with a batch file
Yeah, that helps a lot. Thank you!

_________________
Ultima Ratio Regum
Mar 31, 2009, 02:48pm EDT Reply - Quote - Report Abuse
Private Message - Add to Buddy List

Edited: Mar 31, 2009, 02:49pm EDT

 
>> Re: Creating sequentially named folders with a batch file
Okay, try this for padding:

EDIT: DAMNED SMILEY! This is why we need [code]!

@ECHO OFF && CLS
SETLOCAL EnableDelayedExpansion

REM get folder prefix, start and end count from parameters
REM we add one to the end count now so we don't have to later
SET prefix=%1
SET /A count=%2
SET /A end=%3 + 1

REM find out how many digits are in the end count, so we can pad with zeros
CALL :STRLEN %3
SET /A endlen=%length%

REM start the loop
:MKDIR

REM compare the length of the current count string to the end
CALL :STRLEN %count%
SET /A thislen=%length%
SET /A padlen=%endlen%-%thislen%
SET paddedcount=%count%

REM pad as appropriate
FOR /L %%I in (1,1,%padlen%) DO SET paddedcount=0!paddedcount!

REM make the directory
MKDIR %prefix%" #"%paddedcount%

REM increment by 1
SET /A count += 1

REM if we're at the end, return
REM start the loop over otherwise
IF %count%==%end% (GOTO :EOF) ELSE (GOTO :MKDIR)


REM gets the length of a string
GOTO :EOF
:STRLEN
SET someString=%*
SET length=0
:STRLENLP
IF NOT "!someString:~%length%,1!"=="" (
SET /A length += 1
GOTO :STRLENLP
)
GOTO :EOF


seqfolds.bat fart 1 5 gives:
fart #1
fart #2
fart #3
fart #4
fart #5

seqfolds.bat orly 1 11 gives:
orly #01
orly #02
orly #03
...
orly #09
orly #10
orly #11

etc., so it only pads as much as you need. Let me know if I fux0red something up. :P

Apr 02, 2009, 02:54am EDT Reply - Quote - Report Abuse
Private Message - Add to Buddy List

Edited: Apr 02, 2009, 05:12pm EDT

 
>> Re: Creating sequentially named folders with a batch file
Well, in a true testament as to how bored I really am, I implemented this as a C program for you (source is included). :P It's a lot faster when you're creating a large number of folders. It still does the same thing, although I added an optional --nopound parameter if you just want to have folder 001, etc. Zero-padding behavior is the same.

I compiled with MinGW GCC 4.3.0 with the -ansi flag, and compressed with UPX 3.02 (I didn't mangle the headers or anything, so it can be readily decompressed, if you don't want to recompile).

Let me know how it works for you (I'm a bit new to C), and enjoy. :)

download: http://www.fileden.com/files/2009/4/2/2388438/seqfolds-1.1.zip
md5sum (zip): 2f8eee0bf06bb13060f7897a11967bc9

Reason   Apr 02, 2009, 03:21am EDT Reply - Quote - Report Abuse
Private Message - Add to Buddy List  
>> Re: Creating sequentially named folders with a batch file
Friggin sweet, dude, thanks! Works perfect! Man, now I want to make something. I should really learn some basic coding.

_________________
Ultima Ratio Regum
Apr 07, 2009, 06:50pm EDT Reply - Quote - Report Abuse
Private Message - Add to Buddy List  
>> Re: Creating sequentially named folders with a batch file
Hard Drive (21280) said on Apr 02, 2009 at 12:21am PDT:
Friggin sweet, dude, thanks! Works perfect! Man, now I want to make something. I should really learn some basic coding.

Glad it works! It was a fun practice in C for me. :_)

Raymond Mohler Nov 04, 2012, 01:03am EDT Reply - Quote - Report Abuse
Private Message - Add to Buddy List  
>> Re: Creating sequentially named folders with a batch file
I click on your link http://www.fileden.com/files/2009/4/2/2388438/seqfolds-1.1.zip
and got a 404 message. Is this app still avaibile(sp) from you? If so I would sure like to try it out. Thank You in advance.

Reason   Nov 04, 2012, 09:09pm EST Reply - Quote - Report Abuse
Private Message - Add to Buddy List  
>> Re: Creating sequentially named folders with a batch file
The guy that actually wrote the app isn't around anymore. I still have it but it's on a different PC. I got a lot of stuff going on this week but if I have time I'll try to ping him to see if he has any issues with putting it out there. Since the (now broken) link was public I doubt it'll be an issue but you never know.

_________________
Ultima Ratio Regum
Reason   Nov 07, 2012, 12:05am EST Reply - Quote - Report Abuse
Private Message - Add to Buddy List  
>> Re: Creating sequentially named folders with a batch file
Raymond, PM me if you are still interested.

_________________
Ultima Ratio Regum
Raymond Mohler Nov 08, 2012, 02:17pm EST Reply - Quote - Report Abuse
Private Message - Add to Buddy List  
>> Re: Creating sequentially named folders with a batch file
I am still interested in getting the zip file that was posted and any help in getting will be greatly appreched(sp) Thanks ReM

Reason   Nov 08, 2012, 02:28pm EST Reply - Quote - Report Abuse
Private Message - Add to Buddy List  
>> Re: Creating sequentially named folders with a batch file
OK, i'll upload it, give me a couple days, got a ton going on.

_________________
Ultima Ratio Regum
Raymond Mohler Nov 28, 2012, 01:53pm EST Reply - Quote - Report Abuse
Private Message - Add to Buddy List  
>> Re: Creating sequentially named folders with a batch file

Raymond Mohler Nov 28, 2012, 01:56pm EST Reply - Quote - Report Abuse
Private Message - Add to Buddy List  
>> Re: Creating sequentially named folders with a batch file
Work load load must be big it's been 20 days since I last heard from you. No hurry every 20 days or so reguards REM

Reason   Nov 29, 2012, 05:31pm EST Reply - Quote - Report Abuse
Private Message - Add to Buddy List  
>> Re: Creating sequentially named folders with a batch file
Sorry about the delay. Bought a house and every spare second is going into getting the repairs done before we move in. The PC that has the app is still in storage. Probably going to be a minimum of a week but could be longer.

_________________
Ultima Ratio Regum
Dr. Peaceful Nov 30, 2012, 09:16am EST Reply - Quote - Report Abuse
Private Message - Add to Buddy List  
>> Re: Creating sequentially named folders with a batch file
Raymond, all this time, you could basically write your own code. It's a pretty straight forward coding, plus McFly and Reason already gave lots clues above on how to code this. You don't have to do this in C. You can write in Powershell, which works in all current versions of Windows. With Powershell, you can do this in even fewer lines than in Win commands.

Raymond Mohler Nov 30, 2012, 03:39pm EST Reply - Quote - Report Abuse
Private Message - Add to Buddy List  
>> Re: Creating sequentially named folders with a batch file
I still would like to see the C application and I don't know powershell. I've seen multiple variations of my request and would like to pick or combile the best. I can wait for the man to get it out of storage as I know how hard time becomes when moving sooo I'm not to concerned about time. thanks for your concern REM

Dr. Peaceful Dec 03, 2012, 07:46am EST Reply - Quote - Report Abuse
Private Message - Add to Buddy List

Edited: Dec 03, 2012, 12:55pm EST

 
>> Re: Creating sequentially named folders with a batch file
Ok, for the thrill of coding here's how you do seq folders in Powershell.

One Liner:
0..999 | foreach $_{ New-Item -ItemType directory -Name $("folder" + $_.ToString("000")) }

This will create seq folders from 0 to 999 with padding zeors under the current directory. Adjust the start and end numbers, and folder prefix as you want. Go to Start -> Run -> type Powershell, then type in the above in the prompt, done! But if you're too lazy to type this long string all the time, below is a program that takes in parameters.

Full parameters passing program:
#-- Pass in parameters, define prefix, range and path --
param ([String]$name, [validaterange(0,99999)][Int]$num1, [validaterange(0,99999)][Int]$num2, [ValidateScript({Test-Path $_ -PathType 'Container'})][String]$path )

#-- Parameters handling --
If (! $name) { Exit }

If ($num1 -lt $num2) { $start = $num1; $end = $num2;
} elseif ($num1 -gt $num2) { $start = $num2; $end = $num1;
} else { Exit }

If (! $path) { $path = "." }

#-- Determine Padding --
For ($i=0; $i -lt $($end.ToString()).Length; $i++) { $pad = $pad + "0" }

#-- Making folders --
For ($i=$start; $i -le $end; $i++) { New-Item -ItemType directory -Path $path -Name $($name + $i.ToString($pad)) }

Save this in a file, say "seqdir.ps1". Run it in a Powershell prompt, e.g. ".\seqdir.ps1 folder 0 999 c:\"

Raymond Mohler Dec 10, 2012, 09:00pm EST Reply - Quote - Report Abuse
Private Message - Add to Buddy List  
>> Re: Creating sequentially named folders with a batch file
Thank You for the powershell. How would one put in sub directories??

Reason   Dec 10, 2012, 10:48pm EST Reply - Quote - Report Abuse
Private Message - Add to Buddy List  
>> Re: Creating sequentially named folders with a batch file
http://www.freefilehosting.net/seqfolds-10

That's the original zip file provided. The email for support in the Readme no longer works.

_________________
Ultima Ratio Regum
Dr. Peaceful Dec 11, 2012, 12:45pm EST Reply - Quote - Report Abuse
Private Message - Add to Buddy List

Edited: Dec 11, 2012, 12:55pm EST

 
>> Re: Creating sequentially named folders with a batch file
Just looked at McFly's codes, pretty well written. Support email and website?! LOL, what is he thinking?! XD

The Powershell program in my post above, provided few more things than McFly's codes:
1. Validating the strings and numbers send in from the arguments.
2. Doesn't matter the order of the start / end numbers provided from the arguments. (You can do ".\Seqdir.ps1 folder 0 999" or ".\Seqdir.ps1 folder 999 0", both will be the same.)
3. User can provide a path to where the folders will be created (default to current folder).
The beauty of scripting language is that you can pack a lot more instructions with lesser codes.

McFly's program however does few things that mine does not.
1. Provides more comment feed backs to users, such the --help section.
2. Able to add "#" or not to in the filename. (I don't really care about that one, you can always specify a "#" in the filename preffix anyway.)
3. It can be compiled into a standalone application. That, Powershell can't do.


Write a Reply >>

Continue Reading on Page: 1, 2, Next >>

 

    
 
 

  Topic Tools 
 
RSS UpdatesRSS Updates
 

  Related Articles 
 
 

  Newsletter 
 
A weekly newsletter featuring an editorial and a roundup of the latest articles, news and other interesting topics.

Please enter your email address below and click Subscribe.