Link to home
Start Free TrialLog in
Avatar of tqtclipper
tqtclipperFlag for United States of America

asked on

Create NEW Folder from Script

I have some folders on a server (2003) with XP client. I need to create 2000 folders with "last name, first name and employee number (unique)" as the file name. Is there anyway to write a script to create the folders?

Any advice on this is greatly appreciated.

Thanks
TCLIP
Avatar of tqtclipper
tqtclipper
Flag of United States of America image

ASKER

I guess I should add that i'm not sure if i ever created a script..
ASKER CERTIFIED SOLUTION
Avatar of Lee W, MVP
Lee W, MVP
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That was another book that I did not finish:):):)

Ok we established that I have at least read on the subject... I llike many other earn by doing so when I get over my head (which doesn't take long) or if I run out of time I move on to something else.... I just recently changed departments from IT to HRIT.. So, I get to do more stuff with data...

Anyway, I used pivot table to get the data that I needed. I now have a txt file with 1293 names:

last name, first name and emplID

What's next??
When I enter...
C:\Documents and Settings\hbc3284.XXXXX\Desktop>FOR /f "tokens=1,2,3" in %a (fo
lders.txt) do md %a-%b-%c

I get.....

C:\Documents and Settings\hbc3284.XXXXX\Desktop>FOR /f "tokens=1,2,3" in %a (fo
lders.txt) do md %a-%b-%c
in was unexpected at this time.

C:\Documents and Settings\hbc3284.XXXXXX\Desktop>

Also, where is it going to create the folders; on my desktop???

Please advise...

Thanks,
TCLIP


Here what the data looks like:

Able Sally 306945
I found another issue...

Some of the user have the  following format:

Baker Peter Brent 335591

b/c they are using a middle name. What do I do to get around this issue?

Thanls,
TCLIP
Hello EEers,

I am still looking for a solution to my problem with the develpoment of a script to create new folders. I was getting some excellent feedback from BillDL and leew; however I not received a reply in an hour . I fixed all the issues with my data and now every row is in the same format:

Albaugh David 324381

I saved the file to my desktop and from the command prompt i typed:

FOR /f "tokens=1,2,3" in %a (users.txt) do md %a-%b-%c

Nothing is happening that I can tell. Where is this script suppose to create the folders?

Please help...

Thanks,
TCLIP
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It works perfect thanks for the quick response. It put a "-" (dash) in each file name. I can live with it if I have to but is there any way to get rid of it...

Thanks Again!!!!!!!!

I'm a happy EE'er....

TCLIP:):)
Avatar of Wallsy
Wallsy

Hi there,

You can put in anything you want - I separated them with a dash because that was the example above.

In the command
md %a-%b-%c

%a is the first bit of information, %b and %c the second and third. So you can put %a%b%c to get them all together, %a %b %c for spaces, and so on.

It's up to you!

HTH,
Wallsy
Wallsy, good tip -

I was just on my way to bed when I posted my comment... don't know why I switched the "in" and "%a"
No Problem Guys...

The help was greatly appreciated... I was dealing with a deadline and I was stressing. But you guys saved the day again!!!
Hey, that's excellent.  I have to admit that hadn't spotted the transposition of the %a and "in", unlike eagle-eyed Wallsy.

1,293 new sub-folders from one command line.   That's going to be one helluva directory to navigate in Windows Explorer :-)

As a matter of interest, how long did it take the command to create all the folders?
under 1 minute
Windows XP might not be running alongside DOS, but it certainly shows the power of the command line.  Who was it that said "DOS is dead"?  I believe it was Bill Gates himself :-)
sorry.........i would settle for a GUI version anytime
No way - the command line is king!!

:-)

Wallsy
Wallsy,

I received ( I saw this coming...) a request to change the file naming sturcture from:

Baker-Phillip-335591

to
Baker, Phillip 335591

I can to ge the command to give me the correct results.... When I type:

C:\Documents and Settings\New Folder>FOR /f "tokens=1,2,3
" %a in (clss.txt) do md %a,%b %c the following folders:

317166  or Connie or Wilson

for some reason comma is causing a problem.... Please help

Thanks,
TCLIP
Try putting the %a...%c in quotes like this:


C:\Documents and Settings\New Folder>FOR /f "tokens=1,2,3" %a in (clss.txt) do md "%a,%b %c"

 the following folders:
Thanks that fixed the concern for me....

TCLIP
Thank you, tqtclipper.  Wasn't expecting that since my attempts failed :-)
I have another issue with my project that I wanted to run by the group to see if I can create a script to help with this situation or continue doing it manually.......

Here's my issue:

I am trying to standardize the  files on my file server to a standard that is more clutter free that what is being used currently. I created my standard for folders and naming conventions for ALL file. Moving forward, everyone is ready to use the new folders and the new naming conventions.

My next issue is to move the legacy data into the newly created folders. I have been doing this task manually..
In this particular folder I have about 2000 pdf files that were saved with the following naming convention:

lastname, first name (date)

example
Little, Rueben    (7-1-05).pdf

or

lastname, first name

I thought I could place the files and the folders in the same directory and do a sort, thinking that it would at least place the file next to directory with the same name next to each other however sort keeps file and directories separate....


BTW: I will post this a new question with points also...

Any advice would be greatly appreciated..


Thanks,
TCLIP
Interesting point.  I've never had the need or desire to "ungroup" the folders in Windows Explorer before.  Once again, the command line comes up.  The DIR /og option would "group directories first", while /o-g would show them last, but CMD.EXE isn't involved with the Windows Explorer display, it's a combination of the shell dll's and registry settings that control the views.

The default behaviour is possibly to do with the command options used to open explorer.exe, but I'm not sure.

See this question, and my comments relating to the option /idlist

https://www.experts-exchange.com/questions/21186040/After-IE6sp1-install-WIndows-Explorer-does-not-open-correctly-any-more-in-Windows-98SE.html

More here (click on the icon against "explorer" to expand it):

http://www.mvps.org/serenitymacros/winprogs.html

It could simply be because the "File Folder" file type doesn't really have the same "properties" that files do, and may therefore be exempt from the Windows Explorer view option to group "by type".  You can make the folders appear below the files or above them, but I can't see an immediate way of including the folders in the "sort by name" view setting without delving deep into the registry, or by modifying the internal resources of dll files.

I look forward to seeing someone find an answer to this one :-)

On the question of copying the files to the matching directories, there MIGHT be a batch method to make this easier.  I'll bet you that leew and Wallsy will come up with a simple one-line command :-)

As a starting point (I'll consider the remainder in due course):

dir *.pdf /on /s /b > filelist.txt
dir /on /ad /b > folderlist.txt

That gives you two sorted lists where the filenames should match the corresponding lines in the list of folders.  You can use a long-winded method of:

1. Creating a text file with the first part of a command
2. Use list processing to strip the first line off the top of the file list and send that line to a temporary text file
3. Do the same with the first line of the folder list, and send it to a 2nd temporary file
4. Concatenate the content and send to a temporary batch file:
copy /a  file1+file2+file3  temp.bat
5. Execute the batch file to perform the copying, and then loop back for the 2nd line of each data file.

Just a thought, but my methods are too long-winded and the other experts will no doubt come up with a FOR and DO sigle-liner that needs no other temp files.
Thanks BillDL. I am not sure I completely follow your instructions. I am willing to give it a try. Can you describe the process with more details?

I would hope that there is a simple way to do it but if nothing else come up.. the long winded method is the way to go!!

Thanks,
TCLIP
You could try:
(This may need a bit of tweaking - it's vapourware!)
For /F "Tokens=1,2,*" %%i In ('Dir /B *.pdf') Do Move "%%i %%j %%*" "%%i %%j\%%i %%j %%*"

I would put a bit of error checking in though, because you could hit problems if there are any "non-standard" PDFs.

So:

For /F "Tokens=1,2,*" %%i In ('Dir /B *.pdf') Do If Exist "%%i %%j\." Move "%%i %%j %%*" "%%i %%j\%%i %%j %%*"

This way, if a matching directory name isn't found, the file is skipped, and you can manually clean them up later.

HTH,

Wallsy

PS For troubleshooting purposes, you can always change the command from "Move..." to "Echo Move..." so that you can see the generated command line.
Wallsy's suggest should work, though * would just ensure I only get files and add a /a-d to the dir command as follows:

for /F "Tokens=1,2,*" %%i In ('Dir /a-d /B *.pdf') Do If Exist "%%i %%j\." Move "%%i %%j %%*" "%%i %%j\%%i %%j %%*"

(It is possible, though probably unlikely, that there's a folder with a .pdf extension - /a-d says list folder contents where the item is NOT a directory).
C:\Documents and Settings\Desktop\New Folder\New Folder>For /F "T
okens=1,2,*" %%i In ('Dir /B *.pdf') Do If Exist "%%i %%j\." Move "%%i %%j %%*"
"%%i %%j\%%i %%j %%*"
%%i was unexpected at this time.

I am getting the "i was unexpected at this time" error similar to the error msg that I received with the first script... Does this mean that there is something in the wrong order?

Thanks,
TCLIP
As a single command, you reference %i, not %%i - in a batch file, you reference %%i

Try this:

For /F "Tokens=1,2,*" %i In ('Dir /B *.pdf') Do If Exist "%i %%j\." Move "%i %j %*" "%i %j\%i %j %*"
The script runs without an error however i do not see the results. The pdf files are in the same place the directories are all in the same place.

Am I missing something?

TCLIP
This is what it looks like


C:\Documents and Settings\Desktop\New Folder\New Folder>If Exist
"Brown, %Morris\." Echo Move "Brown, Morris %*" "Brown, Morris\Brown, Morris
 %*"

C:\Documents and Settings\Desktop\New Folder\New Folder>If Exist
"Brown, %Morris\." Echo Move "Brown, Morris %*" "Brown, Morris\Brown, Morris
 %*"

C:\Documents and Settings\Desktop\New Folder\New Folder>If Exist
"Brown, %Morris\." Echo Move "Brown, Morris %*" "Brown, Morris\Brown, Morris
 %*"

C:\Documents and Settings\Desktop\New Folder\New Folder>If Exist
"Brown, %Reggie\." Echo Move "Brown, Reggie %*" "Brown, Reggie\Brown, Reggie

In the above example, the corresponding directories are:


Brown, Martha 312529

Brown, Morris 291216

Boffo, Christopher 321271

Boffo, Joseph 325372

From the example above the Brown, Morris pdf should have moved to the Brown, Morris 291216 directory but the directory is empty..

Thanks,
TCLIP
My long-winded method wouldn't have worked with folder names containing a comma.  In fact, I have always stuck with what others always regard as some old-fashioned naming convention using underscores rather than spaces or any other characters. eg. Brown_Martha_312529.

I like using excel (or rather csv text files opened in excel) for a lot of my dirty work.  Because I tend to use long-handed "list"-type batch files where I can actually SEE everything that will be executed.  I find it incredibly easy to use "find and replace", and to create new columns and paste a new value that will convert to a comma-separated field value in between something else.

For instance, say you created two alphabetically sorted DIR listings of
(a) pdf file names
(b) folder names
as two separate txt files.  Notepad is a bit of a bummer due to its max file size limitations where it insists on opening Wordpad, but you can easily do an "Edit > Select All > Edit > Copy" in Notepad or wordpad, and then paste the contents into a blank column in excel.
paste the list of files into column B, and the folder names into column C.  Type the command COPY into cell 1A, and then drag it down to the last row to copy that text into each cell in column A.
Optionally, add an IF EXIST before the copy or the redirection to NUL ( > nul - suppresses output) in column D.
Save the file as "anyname.csv" and then rename as a .txt file.
Open in Wordpad, Metapad, or other text editor with the Find and Replace function that retains plain text formatting.
Find all commas, and replace with a space.
Save as a .bat file, and then just tidy it up in Notepad/Wordpad with an @echo off at the start if you wish.

Personally, that's how I would approach the task you have in hand (in my "dinosaur-like" way), but the presence of the commas would mess things up UNLESS you first used the Find and Replace on your two .txt listings and replaced the commas in the file/folder names with another symbol like # or ~ prior to pasting into excel.  After you create the final batch file, use the Find and Replace to convert all instances of # or ~ back to commas again before saving and running the batch file.
Use Excel to create new columns with the enclosing " " symbols in columns before and after those file/folder names to allow it to work with the spaces in the names.

Example of csv file after it has been saved and renamed as a txt file, but before using find and replace to change the commas to spaces:

copy,"Brown# Martha 312529.pdf","Brown# Martha 312529",>nul
copy,"Brown# Morris 291216.pdf","Brown# Morris 291216",>nul
copy,"Boffo# Christopher 321271.pdf","Brown# Morris 291216",>nul
copy,"Boffo# Joseph 325372.pdf","Brown# Morris 291216",>nul

Results after replacing commas with spaces, and then replacing #'s with commas:

copy "Brown, Martha 312529.pdf" "Brown, Martha 312529" >nul
copy "Brown, Morris 291216.pdf" "Brown, Morris 291216" >nul
copy "Boffo, Christopher 321271.pdf" "Brown, Morris 291216" >nul
copy "Boffo, Joseph 325372.pdf" "Brown, Morris 291216" >nul

It took me 10 minutes to create a batch file from two separate listings of 2,000 file and folder names.  Of course, I didn't have matching folders for the files I listed with a DIR command, so I just copied the list of filenames and used find and replace to remove the extensions.

How long it would take to execute I don't know, but COPYING files is a better method than moving them in case something goes wrong.  You can always delete all *.pdf's in the master folder after you are happy they copied OK.

On second thoughts, just stick with the conventional DOS wisdom of leew and Wallsy  :-)
Whoops, Morris Brown's pdf's would have been copied to the Boffo brothers' folders there :-/  You get the general idea though.  That's the danger of copy and paste when giving examples.  I obviously wasn't using real source files for the content.  Here's the corrected text content, just for accuracy:

Before:

copy,"Brown# Martha 312529.pdf","Brown# Martha 312529",>nul
copy,"Brown# Morris 291216.pdf","Brown# Morris 291216",>nul
copy,"Boffo# Christopher 321271.pdf","Boffo# Christopher 321271",>nul
copy,"Boffo# Joseph 325372.pdf","Boffo# Joseph 325372",>nul

After:

copy "Brown, Martha 312529.pdf" "Brown, Martha 312529" >nul
copy "Brown, Morris 291216.pdf" "Brown, Morris 291216" >nul
copy "Boffo, Christopher 321271.pdf" "Boffo, Christopher 321271" >nul
copy "Boffo, Joseph 325372.pdf" "Boffo, Joseph 325372" >nul
tqtclipper,

A thousand apologies - I was treated to a very long "lunch" yesterday, and for the hour I spent at work afterwards I obviously wasn't paying as much attention as I should have...

From the command line you could try this:
For /F "Tokens=1,2,*" %i In ('Dir /B *.pdf') Do @If Exist "%i %j\." Move "%i %j %k" "%i %j\%i %j %k"

(I had put %* in the command line instead of %k, obviously alcohol related!!)

Again, you could test it with the echo:
For /F "Tokens=1,2,*" %i In ('Dir /B *.pdf') Do @If Exist "%i %j\." Echo Move "%i %j %k" "%i %j\%i %j %k"

Again, sorry for the delay and the bad advice.

HTH,
Wallsy
Still no luck... the files are not moving to the respective folders. Am I missing something... see results below...

C:\Documents and Settings\hbc3284.KBRSAT\Desktop\New Folder\New Folder>For /F "T
okens=1,2,*" %i In ('Dir /B *.pdf') Do @If Exist "%i %j\." Echo Move "%i %j %k"
"%i %j\%i %j %k"
Move "ang.pdf  " "ang.pdf \ang.pdf  "
Move "Baskin Robin(07-11-05).pdf " "Baskin Robin(07-11-05).pdf\Baskin Robin(07-11-05).pdf "
Move "BurnsMister(07-08-05).pdf  " "BurnsMister(07-08-05).pdf \BurnsMister(07-08-05).pdf  "


Thanks,
TCLIP
When I run the command without the "Echo Move" I get....

C:\Documents and Settings\hbc3284.KBRSAT\Desktop\New Folder\New Folder>For /F "T
okens=1,2,*" %i In ('Dir /B *.pdf') Do @If Exist "%i %j\." Move "%i %j %k" "%i %
j\%i %j %k"
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
There isn't a space between the surname and the bracket....
Try this!

For /F "Tokens=1,* Delims=(" %i In ('Dir /B *.pdf') Do @If Exist "%i\." Move "%i(%j" "%i\%i(%j"

HTH,

Wallsy
Still no luck... the files are not moving to the respective folders. Am I missing something... see results below...

Move "Altzheimer, Ann 317257 06AUG05.pdf(" "Altzheimer, Ann 317257 06AUG05.pdf\Altzheimer, Ann 317257 06AUG05.pdf("
Move "Altzheimer, Ann 317257 LWOP 29JUN05.pdf(" "Altzheimer, Ann 317257 LWOP 29JUN05
.pdf\Altzheimer, Ann 317257 LWOP 29JUN05.pdf("

*sensitive data removed*

TCLIP

BTW the directory names are in the following format:

Burnsley, Bob 307629

Burnsworth, Barney 287044
Okay then... we're not making much progress moving the files, as the names aren't always consistent. So let's try this another way - start with directories and move the matching files.

Copy this in to a batch file and run it:

For /F "Tokens=*" %%i In ('Dir /B /AD') Do Call :MoveFiles %%i
Goto :EOF
:MoveFiles
For /F "Tokens=*" %%j In ('Dir /B /A-D "%**.PDF"') Do Move "%%j" "%*"
Goto :EOF


Let me know what happens....

HTH,

Wallsy
It works!! Sort of....

Here are a few examples of the files that  moved:

Ariba, Juan 316989 05AUG05 CHANGE.pdf
Ariba, Juan 316989 03AUG05.pdf
Aballone, Justin 323306 01JUL05.pdf


Here are a few examples of files that did "NOT" move:

Baby, Roger (10-20-04).pdf
Alozie, Chris (CHANGE 10-27-04).pdf
Boyd, William LWOP Change 11-05-04).pdf
Bordeaux, Wendy (LWOP 01-27-05).pdf

It look like it did not touch a file that did not contain the EMPLID is there a way to fix this?

TCLIP
This should do the trick...

For /F "Tokens=1,2,*" %%i In ('Dir /B /AD') Do Call :MoveFiles %%i %%j %%k
Goto :EOF
:MoveFiles
For /F "Tokens=*" %%j In ('Dir /B /A-D "%1 %2*.PDF"') Do Move "%%j" "%*"
Goto :EOF
no luck... the files are not moving to the respective folders.

I ran a test and the new script will not move any flies.

Ariba, Juan 316989 05AUG05 CHANGE.pdf (file types)

or

Boyd, William LWOP Change 11-05-04).pdf  (file types)

Remember the distinction being a file name with an employee ID verses a file name without an employee id. The first script will still move a file with lname. fname emplid to the respective folder. I forgot to mention duplicates, i'm not sure what I want to do when you get to a duplicat lname, fname. Whatever is easier i guess there should not be that many. My first thought would be to put the file in all folders with the same lname, fname... That way we can delete the eronneous one as we get to them... I did not this this would be a big deal. I really appreciate your help on this one!!!!

TCLIP
Right - this one works with those names...

For /F "Tokens=1,2,*" %%i In ('Dir /B /AD') Do Call :MoveFiles "%%i %%j" "%%i %%j %%k"
Goto :EOF
:MoveFiles
For /F "Tokens=*" %%j In ('Dir /B /A-D %1*.PDF') Do Move "%%j" %2
Goto :EOF


FYI, the script was passing the parameters as
      Call :MoveFiles Ariva, Juan 316989
But then running them as
      Ariba Juan
ie leaving the comma off, which is why it was failing.

Have another go!!  :)

HTH,

Wallsy
I ran the script against 4632 files and it moved 2482 files. I posted the question to a different link and got a second solution. At first I ran both scripts on my test folder to make sure there were no surprises...

after it completed I ran Zamba1's script on the file i did a spot check noticed a file or two that did not move but there was an appropriate folder. I ran Wallsy's script and it picked up 30 additional files.

BEFORE

4/29/2005  09:58 AM           752,149 Zoran, Markof      (6-24-05).pdf
1/29/2005  07:15 PM           362,667 Zwickl, Chickie (03-14-05).pdf
           4632 File(s)  2,227,812,337 bytes
           1511 Dir(s)  688,069,697,536 bytes free


AFTER

07/16/2005  10:08 PM    <DIR>          Zimmerman, Robert 306568
04/29/2005  09:58 AM           752,149 Zoran, Markof     (6-24-05).pdf
01/29/2005  07:15 PM           362,667 Zwickl, Chickie (03-14-05).pdf
            2150 File(s)    960,366,924 bytes


AFTER WALLSY

2005  10:08 PM    <DIR>          Zeller, Helter 315
/2005  10:08 PM    <DIR>          Zimmerman, Robert 3
/2005  09:58 AM           752,149 Zoran, Markof
/2005  07:15 PM           362,667 Zwickl, Chickie (03-1
       2119 File(s)    947,708,500 bytes
       1511 Dir(s)  688,042,639,360 bytes free

I am calling it quits for now but I will continue to analyze the remaining files. Most of them did not move because there was no appropriate folder, some b/c there was a typo. In both cases the scripts did what they were suppose to do... I am greatly appreciative for the help and forever in your debt.. Wallsy please go to the link below so that I can split the points...

https://www.experts-exchange.com/questions/21491502/Create-NEW-Script-to-move-files-to-the-respective-folders.html#14458409

I will probably need a day or two to analyze the 2K files... But for now this issue is resolved... A BIG THANKS TO Wallsy, Zamba1  and anyone that I missed. "It can't get any better than EE"!!!


TCLIP
TCLIP

You stated a while back:

>>>
"I received (I saw this coming...) a request to change the file naming structure from:
<lastname>-<firstname>-<ID_No>
to
<lastname>, <firstname> <ID_No>"

"...everyone is ready to use the new folders and the new naming conventions".
<<<

In real terms, what you have is the naming convention:
<lastname>, <firstname> <ID_No or status> <date_of_change>

Just an observation from some of ther listings you gave.  Somebody has not been adhering to the choice of file naming convention chosen:

 315423 08JUL05.pdf - no comma separator
 315147 08AUG05.pdf - no comma separator
 320683 21JUN05  change.pdf - no comma separator
 334862 27JUL05.pdf - no comma separator
 327648 19AUG05.pdf - full stop and a comma separator
 335548 20JUN05.pdf - no comma separator
 302878 19JUN05  change.pdf - no comma + double-space after 05
 336646 05AUG05.pdf - double-space after comma
- is there a Robert LastName also?
 - is there a Robert LastNome also?
 LWOP Change 11-05-04).pdf - missing the opening bracket (

I assume the records are kept in a database.  Perhaps you should look carefully at the form used to input new records, and ensure that the input fields are subjected to data validation to prevent non-conventional records from being created.  You have spent a lot of time getting this right, and all it takes are a few renegades to screw it up.