create folders from a list and move files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
sifar786
Posts: 27
Joined: 15 May 2013 01:26

Re: create folders from a list and move files

#16 Post by sifar786 » 17 May 2013 00:12

Thanks Endoro,

this is nice and now i am able to get the output.

one thing : \t does not work, so i have maintained "CSVDelim = ".

1] How do i take the path and list as parameters:

e.g.

Code: Select all

test14.bat "C:\users\sifar786\desktop"   "ClassID_ID_List.txt"   "Out.csv" 


and pass it to 'Startfolder' inside code?
2] How do i redirect the output to a tab-delimited text file for further processing?

3] Going back to my original query, How do i create ClassID folders, ID sub-folders and xcopy all the different filetypes into their respective ID folders?
e.g.

Code: Select all

test14.bat "C:\users\sifar786\desktop"   "ClassID_ID_List.txt"   "Out.csv" 


4] Lastly, is there a good website where i can learn advance DOS programming?

Thanks. I really appreciate your help.

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: create folders from a list and move files

#17 Post by Endoro » 17 May 2013 02:14

here we go:

  1. is in the code now
  2. added output to file
  3. please provide "ClassID_ID_List.txt"
  4. I would recommend this website :)

Code: Select all

@echo off&setlocal
set "startfolder=C:\users\sifar786\desktop"
set "CSVfile=out.csv"
set "CSVdelim=   " &rem, delimiter for output, default <tab>
rem            ^---< put your <tabspace> there

cd /d "%startfolder%"
for /f "delims=" %%f in ('dir /b/a-d "*"') do set /a %%~xf=%%~xf + 1 2>nul
((for /f "delims==." %%i in ('set "."') do <nul set/p"=%%i%CSVdelim%")&echo(
(for /f "tokens=2delims==" %%i in ('set "."') do <nul set/p"=%%i%CSVdelim%")&echo(
)>"%CSVfile%"
type "%CSVfile%"


sifar786
Posts: 27
Joined: 15 May 2013 01:26

Re: create folders from a list and move files

#18 Post by sifar786 » 17 May 2013 04:15

Hi Endoro,

1] So, that means i cannot specify parameters on command line when running the bat file like this?

Code: Select all

test14.bat "C:\users\sifar786\desktop"   "ClassID_ID_List.txt"   "Out.csv" 

3] e.g ClassID_ID_List.txt OR ClassID_ID_List.xls to create folders and their subfolders and move their respective files into them.

Code: Select all

ClassID   ID
1     10001
1    10004
2     10002
3     10003

Please note i am unable to attach the file as there is no option for attaching files.
only the files containing the respective ID's in their names, found in the above folder, would be moved into their respective ID folders.

4] on this same website you specified, is there an advanced tutorial section or tutorials available?

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: create folders from a list and move files

#19 Post by Endoro » 17 May 2013 06:59

sifar786 wrote:4] on this same website you specified, is there an advanced tutorial section or tutorials available?


You point to an interesting issue. I have some bookmarks in my browser folder, but no link to a tutorial site. Some weeks ago I did an intensive search, but found not much. Overall there are lot of good threads and postings here, on stackoverflow, ss64 and few foreign (non English) forums. Many other batch postings and code examples are of low quality. Maybe we should have a link archive to really good topics here. If there is such a library already, I have not found it.

sifar786
Posts: 27
Joined: 15 May 2013 01:26

Re: create folders from a list and move files

#20 Post by sifar786 » 17 May 2013 07:15

Well, it would be really great to have experts (DOS Ninja) like you write tutorials picking up one example solution that you provided and explaining the code in detail (also putting some reference links to refer for further information for certain commands). It would act as a great Learning reference if we have such Learning Cases on the forum. :D

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: create folders from a list and move files

#21 Post by Endoro » 17 May 2013 10:40

sifar786 wrote:Please note i am unable to attach the file as there is no option for attaching files.
only the files containing the respective ID's in their names, found in the above folder, would be moved into their respective ID folders.


I'm not sure about this file names. You can upload longer lists to Pastebin or a file hoster and post the link here.


Parameters:

Code: Select all

@echo off&setlocal
set "startfolder=%~1" &rem parameter #1
set "ClassID_ID_List=%~2" &rem parameter #2
set "CSVfile=%~3" &rem parameter #3
set "CSVdelim=   " &rem, delimiter for output, default <tab>
rem            ^---< put your <tabspace> there

cd /d "%startfolder%"
for /f "delims=" %%f in ('dir /b/a-d "*"') do set /a %%~xf=%%~xf + 1 2>nul
((for /f "delims==." %%i in ('set "."') do <nul set/p"=%%i%CSVdelim%")&echo(
(for /f "tokens=2delims==" %%i in ('set "."') do <nul set/p"=%%i%CSVdelim%")&echo(
)>"%CSVfile%"
type "%CSVfile%"

sifar786
Posts: 27
Joined: 15 May 2013 01:26

Re: create folders from a list and move files

#22 Post by sifar786 » 17 May 2013 13:11

Hi Endoro,

Here's the pastebin link:

http://pastebin.com/4i5xKuVU

sifar786
Posts: 27
Joined: 15 May 2013 01:26

Re: create folders from a list and move files

#23 Post by sifar786 » 17 May 2013 13:23

cool 8)

this code accepts parameters from the command line.

Code: Select all

C:\Users\sifar786\Desktop>test15.bat "C:\users\sifar786\desktop"   "ClassID_ID_List.txt"   "Out.csv"

bat   csv   docx   ini   lnk   pdf   pptx   txt   xls   xlsm   xlsx   zip
16   1   1   1   3   1   1   2   3   3   3   4

C:\Users\sifar786\Desktop>


now the major daunting task of creating folders-subfolders from the ClassID_ID_List.txt and copying or moving files into them based on the ID's in their filenames. some files may have to be created for test purpose containing the ID's e.g.

10001_amv.jpg
10002_pvr.mdb
10001_cnn.doc
....
etc

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: create folders from a list and move files

#24 Post by Endoro » 17 May 2013 13:28

Code: Select all

ClassID   ID
1   10001
1   10004
2   10002
3   10003

Yes, I understand this- but where are the file names? Do you have a list with names or a list of assignments?

sifar786
Posts: 27
Joined: 15 May 2013 01:26

Re: create folders from a list and move files

#25 Post by sifar786 » 17 May 2013 13:34

ome files may have to be created for test purpose containing the ID's e.g.

10001_amv.jpg
10002_pvr.mdb
10001_cnn.doc
....
etc

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: create folders from a list and move files

#26 Post by Endoro » 17 May 2013 13:52

the code for this I posted here alredy.
and later you postet other file names.
Therefore I changed the code for all filenames.
So you can now use my earlier code again.

sifar786
Posts: 27
Joined: 15 May 2013 01:26

Re: create folders from a list and move files

#27 Post by sifar786 » 17 May 2013 14:15

hi,

that is because i had removed the files on next run as code was not creating folders and sub-folders and i was afraid that these imp files may get deleted or corrupted. sorry. :(

how do i club the two codes together?

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: create folders from a list and move files

#28 Post by Endoro » 17 May 2013 15:30

at first, limiting the "dir" output:

Code: Select all

@echo off&setlocal
set "startfolder=%~1" &rem parameter #1
set "ClassID_ID_List=%~2" &rem parameter #2
set "CSVfile=%~3" &rem parameter #3
set "CSVdelim=   " &rem, delimiter for output, default <tab>
rem            ^---< put your <tabspace> there

cd /d "%startfolder%"
for /f "delims=" %%f in ('dir /b/a-d "*_*"') do set /a %%~xf=%%~xf + 1 2>nul
((for /f "delims==." %%i in ('set "."') do <nul set/p"=%%i%CSVdelim%")&echo(
(for /f "tokens=2delims==" %%i in ('set "."') do <nul set/p"=%%i%CSVdelim%")&echo(
)>"%CSVfile%"
type "%CSVfile%"

sifar786
Posts: 27
Joined: 15 May 2013 01:26

Re: create folders from a list and move files

#29 Post by sifar786 » 17 May 2013 23:43

Hi Endoro,

this is the same earlier code, but this does not create the folders, subfolders and copy (move) the files to their respective folders.

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

Re: create folders from a list and move files

#30 Post by foxidrive » 18 May 2013 00:46

@sifar786: it seems to me that you have not provided complete details, even when asked for them.

If you want a batch file that works, you have to provide real details. It matters in many cases - we have been in such situations before and have had to rewrite the code when correct details are provided, after the code doesn't work.

Post Reply